Embed Notice
HTML Code
Corresponding Notice
- Embed this notice@hazlin Well, '.method' just returns a lambda, right?
add5 = 5.method(:+)
puts add5.call(6) # .call and [] do the same thing in this case.
puts add5[7]
puts 5.method(:method)[:method][:method][:method] == 5.method(:method)
puts 5.class.instance_method(:+).bind(6)[7] # instance_method returns an unbound method, you have to bind it to an actual instance to call it. You can use this to get around shadowing by subclasses.
And 'methods' returns a list of symbols that represent the names of the methods. (It is also very useful.) In fact, this might answer some questions:
builtins = methods.sort
require 'ruby2d'
additions = methods.sort - builtins
puts *additions.map { |mname|
m = method(mname)
if m.nil?
"#{mname} is implemented in C."
else
"#{mname} ->\t#{m.source_location[0]}:#{m.source_location[1]}"
end
}