question about String methods...

0 views
Skip to first unread message

Sarah Allen

unread,
Oct 16, 2009, 3:13:35 PM10/16/09
to railsbridg...@googlegroups.com, Yehuda Katz
So, in the last workshop I was talking to Yehudah about good irb
examples for learning Ruby and he mentioned that it is neat to
demonstrate .methods and then subtract the Object methods thus
demonstrating a few concepts at once.

So I did this:

>> String.methods.sort
=> ["<", "<=", "<=>", "==", "===", "=~", ">", ">=", "__id__",
"__send__", "allocate", "ancestors", "autoload", "autoload?", "class",
"class_eval", "class_variable_defined?", "class_variables", "clone",
"const_defined?", "const_get", "const_missing", "const_set",
"constants", "display", "dup", "eql?", "equal?", "extend", "freeze",
"frozen?", "hash", "id", "include?", "included_modules", "inspect",
"instance_eval", "instance_method", "instance_methods",
"instance_of?", "instance_variable_defined?", "instance_variable_get",
"instance_variable_set", "instance_variables", "is_a?", "kind_of?",
"method", "method_defined?", "methods", "module_eval", "name", "new",
"nil?", "object_id", "private_class_method",
"private_instance_methods", "private_method_defined?",
"private_methods", "protected_instance_methods",
"protected_method_defined?", "protected_methods",
"public_class_method", "public_instance_methods",
"public_method_defined?", "public_methods", "respond_to?", "send",
"singleton_methods", "superclass", "taguri", "taguri=", "taint",
"tainted?", "to_a", "to_s", "to_yaml", "to_yaml_properties",
"to_yaml_style", "type", "untaint", "yaml_as", "yaml_new",
"yaml_tag_class_name", "yaml_tag_read_class", "yaml_tag_subclasses?"]
>> String.methods - Object.methods
=> ["yaml_new"]

Why am I not seeing the other string records that I know String has,
as demonstrated by:

>> "foo"
=> "foo"
>> "foo".class
=> String
>> "foo".upcase
=> "FOO"

Puzzled,

Sarah

Ethan Michaels

unread,
Oct 16, 2009, 3:31:33 PM10/16/09
to railsbridg...@googlegroups.com
On Fri, Oct 16, 2009 at 12:13 PM, Sarah Allen <sa...@ultrasaurus.com> wrote:
>
> So, in the last workshop I was talking to Yehudah about good irb
> examples for learning Ruby and he mentioned that it is neat to
> demonstrate .methods and then subtract the Object methods thus
> demonstrating a few concepts at once.
>
> So I did this:
>
>  >> String.methods.sort

I think it's because you're calling methods() on the String class, not
a String instance.

Try

>> 'A String'.methods.sort

E

Ethan Michaels

unread,
Oct 16, 2009, 3:33:48 PM10/16/09
to railsbridg...@googlegroups.com
Here's a thing you can do that's neat and handy for demos...

(Using the empty String here...)

>> ''.methods.grep /upcase/
=> ["upcase!", "upcase"]

Mike Gunderloy

unread,
Oct 16, 2009, 3:34:56 PM10/16/09
to railsbridg...@googlegroups.com
String.methods => methods of the String class
String.instance_methods => methods of an instance of the String class
"foo".methods => methods of this particular instance of the String class

Mike

Mike Gunderloy

unread,
Oct 16, 2009, 3:37:48 PM10/16/09
to railsbridg...@googlegroups.com
Oh, and you can also exclude inherited methods by passing false to the
method methods ...

ruby-1.8.7-p174 > Integer.methods
=> ["private_class_method", "inspect", "name", "tap", "clone",
"public_methods", "object_id", "__send__", "method_defined?",
"instance_variable_defined?", "equal?", "freeze", "extend", "send",
"const_defined?", "methods", "ancestors", "module_eval",
"instance_method", "hash", "autoload?", "dup", "to_enum",
"instance_methods", "public_method_defined?", "instance_variables",
"class_variable_defined?", "eql?", "constants", "instance_eval", "id",
"singleton_methods", "module_exec", "induced_from", "const_missing",
"taint", "instance_variable_get", "frozen?", "enum_for",
"private_method_defined?", "public_instance_methods", "display",
"instance_of?", "superclass", "method", "to_a", "included_modules",
"const_get", "instance_exec", "type", "<", "protected_methods", "<=>",
"class_eval", "==", "class_variables", ">", "===",
"instance_variable_set", "protected_instance_methods",
"protected_method_defined?", "respond_to?", "kind_of?", ">=",
"public_class_method", "to_s", "<=", "const_set", "allocate", "class",
"private_methods", "=~", "tainted?", "__id__", "class_exec",
"autoload", "untaint", "nil?", "private_instance_methods", "include?",
"is_a?"]
ruby-1.8.7-p174 > Integer.methods(false)
=> ["induced_from"]

And for bonus, you
have .public_methods, .private_methods, .protected_methods,
and .singleton_methods available too

Mike

Sarah Allen

unread,
Oct 16, 2009, 3:46:30 PM10/16/09
to railsbridg...@googlegroups.com
Thanks everyone. I think I wanted to write:

String.instance_methods.sort - Object.instance_methods

:)
http://www.ultrasaurus.com



Reply all
Reply to author
Forward
0 new messages