Saturday, December 24, 2016

Using 'relative_require' to separate functions, classes, instantiation

My goal is to create a main.rb file that does nothing but instantiate the classes and execute their methods.  As you can see below, this is possible, at least in a simple application, but is this good practice?

It feels neater to separate classes, functions, etc; but then again, this might be akin to separating vocabulary sheets by parts of speech -- tidy, but impracticable.

It might be better practice to group blocks of code that work on the same portion of the program.  Please let me know what works in your experience.

A simple function:

## demo_function.rb

def hello_world()
  puts "Hello world!"
end

A class that uses this function as a method:

## demo_class.rb

require_relative 'demo_function.rb'

class Demo
  def enter()
    hello_world()
  end
end

Finally, an instance of the class that executes its method:

## demo_main.rb
require_relative 'demo_class'

a_class = Demo.new()
a_class.enter()

Output:

Hello World! 


No comments:

Post a Comment

1,050 hours

It took me 13 working days to complete my first 100 "work" pomodoros as a Junior Software Tester at Profectus Group.  Much of ...