define_method method allows you to create methods on the fly. : define_method « Method « Ruby





define_method method allows you to create methods on the fly.


#!/usr/bin/env ruby

class Meta
  %w{ j e m k l }.each do |n|
    define_method(n) { puts "My name is #{n.capitalize} Bennet." }
  end
end

Meta.instance_methods - Object.instance_methods 

meta = Meta.new

meta.e 

 










Related examples in the same category

1.Add a new method with define_method
2.Metaprogramming with String Evaluations