ruby - How method name converts into symbol? -
i try understand ruby, , it's not clear me, how ruby converts name of method symbol?
in method definition give name meth
module mod def meth puts 'm' end end but if want check if method exists, pass symbol :meth parameter method_defined
mod.method_defined?(:meth) => true please, me understand, how work?
it's not clear me, how ruby converts name of method :symbol?
that's way method#name works, returns name of method symbol:
m = "foo".method(:size) #=> #<method: string#size> m.name #=> :size m.call #=> 3 all methods referencing other methods work way. example, object#methods returns array of method names:
"foo".methods #=> [:<=>, :==, :===, :eql?, :hash, :casecmp, :+, :*, ...] in method definition give name
meth... if want check, method exist, givemethod_definedsymbol:meth
meth reference variable or method, whereas :meth symbol:
meth = :foo mod.method_defined? meth #=> false, equivalent mod.method_defined? :foo mod.method_defined? :meth #=> true
Comments
Post a Comment