Create new method dynamically : method_missing « Reflection « Ruby






Create new method dynamically


class StringFactory
  def StringFactory.method_missing(m, *args)
    return String.new(m.to_s, *args)
  end
end

puts StringFactory.a_string                      # => "a_string"
puts StringFactory.another_string                # => "another_string"

 








Related examples in the same category

1.Convert all system command to all class method
2.Responding to Calls to Undefined Methods
3.Override method_missing method to provide meaningful error message
4.Create a new class and override the method_missing method
5.If method does not exist, call the default one