Home Learning metaprogramming
Post
Cancel

Learning metaprogramming

Dynamic Patch

the send method could be used to call methods

1
  obj.send(:my_method, params)

Dynamic Method

the define_method method could be used to create instance method

1
2
3
4
5
6
  class Foo do
    define_method(name) do
      info = @data_source.send "get_#{name}_info", @id price = @data_source.send "get_#{name}_price", @id result = "#{name.capitalize}: #{info} ($#{price})" return "* #{result}" if price >= 100
      result
    end
  end

the instance_method could be used to call an instance method

1
2
  f = Foo.new
  f.instance_method(:method_name)

Remove Method

the undef_method method could be used to remove method, it would remove both receiver’s methods and inherited methods

1
  undef_method method_name

the remove_method method is similar, but it leaves the inherited methods

1
  remove_method method_name