ruby - Why respond_to with initialize() returns false? -


why false when c.respond_to?(:initialize)

class c   def initialize;end   def meth;end end  c.respond_to?(:initialize) #=> false c.new.respond_to?(:meth) #=> true expected 

another variation

class c   def initialize;end    def meth     pmeth   end    private    def pmeth     respond_to?(:initialize)   end end 

this because #initialize not public method. if want check private,protected methods using #respond_to?, set second parameter true.

documentation saying

returns true if obj responds given method. private , protected methods included in search if optional second parameter evaluates true.

see below:

class c   def initialize;end   def meth;end end  c.respond_to?(:initialize,true) # => true c.new.respond_to?(:initialize) # => false c.new.respond_to?(:initialize,true) # => true c.private_methods(false).include?(:initialize) # => true c.new.private_methods(false).include?(:initialize) # => true 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -