Ruby best practice: working with classes

ruby

Solution

You could do this any number of ways, it's up to you. Here is a fun one...

class Test 
  def start 
    p "started"
  end
  new 
end.start

More seriously, your first example encapsulates everything in the one class. That's ok, for a script; it puts everything in your own namespace and mostly avoids the mysterious main object context. You can define nested classes if you need them.

However, the second approach is more conventional.

Problem

See the example below, i suppose it is best to use the second method but the first also works. Which method is best and what are the consquences of using the other ? ``` class Test def start p "started" end test = Test.new test.start end class Test2 def start p "started" end end test2 = Test2.new test2.start ```

Original source