←codingtales home

Class/Module Declaration must return the Constant in Ruby 2.1

Ruby 2.1 has a pretty nice new feature:

def sample
  puts "sample"
end
# => :sample

Method declaration returns the symbol for that method.

However, class or module declarations still return the value of the last expression. Most of these last expressions are methods. So, class/module declarations now return the symbol of some method within them.

class Sample
  def hello
    puts "Hello"
  end
end
#=> :hello

Previous versions of ruby returned nil because the method declarations returned nil. Ideally, I would want it to return the Constant that represents them.

class Sample
  def hello
    puts "Hello"
  end
end
#=> Sample

Divider

Divider