I have read http://www.ruby-doc.org/core-2.1.1/Method.html . I have some doubts on methods of Method
class:
name
and original_name
? source_loction
give nil
for ruby gem related Method
objects? receiver
would give the bound receiver of method object. What is the meaning of bound receiver? Is there anyway to distinguish between the method created by def <method_name> ..... end
and define_method(symbol){block}
?
What is the difference between name
and original_name
?
original_name
returns the original name for aliased methods :
def foo; end alias bar foo method(:bar).name #=> :bar method(:bar).original_name #=> :foo
Would source_loction
give nil
for ruby gem related Method
objects?
source_location
works for gems, too:
require 'rails' Rails.method(:version).source_location #=> [".../ruby/2.1.1/gems/railties-4.1.0/lib/rails/version.rb", 5]
It returns nil
for native methods, ie methods written in C:
method(:puts).source_location #=> nil
I have seen the receiver would give the bound receiver of method object. What is the meaning of bound receiver?
Bound methods are associated with a particular object (the receiver) and can be called, eg:
str = "abc" str.method(:upcase) #=> #<Method: String#upcase>
instance_method
returns the method as an unbound method:
String.instance_method(:upcase) #=> #<UnboundMethod: String#upcase>
UnboundMethod
doesn't have a receiver and can't be called (there's no string instance you could upcase).
Is there anyway to distinguish between the method created by def <method_name> ..... end
and define_method(symbol){block}
?
I don't think so.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.