1
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ A small thing caught my eye: the last three lines of the body of is_valid_input? would normally be written commands && INPUT_FUNCTION_MAPPER.key?(commands[0])) && (commands[0] != "ADD_CHILD" || commands.length == 4) (or the logical equivalent). \$\endgroup\$ Commented Feb 1, 2021 at 8:27

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.