String.methods.include?(:upcase) => false but '.upcase' is listed in ruby-doc.org -


i have been playing around strings bit (in irb), , found myself in trouble understanding meaning of following code:

string.methods => [:try_convert, :allocate, :new, :superclass, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :included_modules, :include?, :name, :ancestors, :instance_methods,  :public_instance_methods, :protected_instance_methods, :private_instance_methods,  :constants, :const_get, :const_set, :const_defined?, :const_missing, :class_variables,  :remove_class_variable, :class_variable_get, :class_variable_set,  :class_variable_defined?, :public_constant, :private_constant, :module_exec, :class_exec,  :module_eval, :class_eval, :method_defined?, :public_method_defined?,  :private_method_defined?, :protected_method_defined?, :public_class_method,  :private_class_method, :autoload, :autoload?, :instance_method, :public_instance_method,  :nil?, :=~, :!~, :eql?, :hash, :class, :singleton_class, :clone, :dup, :initialize_dup,  :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?,  :inspect, :methods, :singleton_methods, :protected_methods, :private_methods,  :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set,  :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send,  :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method,  :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=,  :instance_eval, :instance_exec, :__send__, :__id__]  

hence well-known method 'upcase' not included in output, tried receive way:

string.methods.include?(:upcase) => false                          # mother of god, shocked! 

but http://ruby-doc.org/core-2.0/string.html#method-i-upcase lists .upcase method method of class string.

and of course, in irb-sessions or editor, ruby understands execute

"whatdoiknow".upcase => "whatdoiknow" 

my questions are:

  1. what kind of methods output string.methods
  2. why .upcase method not listed in output
  3. how can literally list methods string (eg. when searching something)

strings have upcase method. string not string, class, , classes don't have upcase method.

if want know whether particular string object has upcase method, should ask string:

'foo'.methods.include?(:upcase) # => true 

or should ask string class whether has instance method include defined strings:

string.instance_methods.include?(:upcase) # => true 

remember: classes objects other. have methods, have instance variables, have class.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -