Should methods be inside classes? in Ruby

Posted on

Problem

I have coded more in Python but I am learning Ruby now. So I am trying to write a solution to create a family_tree and add and get relationships. The code below is of the file that contains the main function which is called from the command line. The below code works and solves the problem but I have written methods without classes. In python it is acceptable. Is is acceptable in ruby? Even though the code works is this how it is written in Ruby? Or does this need refactoring? If yes then please tell how?

#main_file.rb

require_relative 'family'
require_relative 'family_tree'
include FamilyTree
require_relative 'constants'
require_relative 'errors'


def is_valid_input(command)
  #User input Validation
  commands = command.split(' ')
  return false unless (commands && INPUT_FUNCTION_MAPPER.key?(commands[0]))
  return false if (commands[0] == "ADD_CHILD" && commands.length != 4)
  return true
end

def create_family_tree(family_tree)
  #Initializing the family
  family_name = family_tree[:name]
  members = family_tree[:members]

  family = Family.new(family_name)
  members.each do |name, gender, mother_name|
    family.add_member(name, gender, mother_name)
  end
  family
end

def execute_command(command, family)
  result = nil
  unless is_valid_input(command)
    raise InvalidParametersError, "Command is not valid"
  end
  command = command.split(" ")
  if command[0] == "ADD_CHILD"
    mother, name, gender = command[1..]
    result = family.add_member(name, gender, mother)
  end
  result
end

def main_method
  if ARGV.length != 1
    raise InvalidParametersError, "Please provide an input file"
  end
  input_file = ARGV[0]
  unless File.file?(input_file)
    raise InvalidParametersError, "Input file does not exist"
  end

  family = create_family_tree(FAMILYTREE)
  File.readlines(input_file).each do |line|
    line = line.strip
    puts execute_command(line, family)
  end
end

main_method()

This is executed as below:

ruby main_file.rb input.txt

Solution

This “global function” style works for small scripts but isn’t reusable.

A more practical problem with it is if you want to have state that is shared between functions, you don’t have anywhere to store it (well, you could use global variables ($var), but that’s prone to conflicts).

For a more encapsulated design, create a class that does the work and move the methods into the class. See for example definition and usage.

Leave a Reply

Your email address will not be published. Required fields are marked *