[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

argh! more undocumented mysteries: to_yaml

Tom Cloyd

3/16/2008 1:56:00 PM

Beginner woes, here, no doubt, but woes nevertheless.

I'm exploring the wonders of the Object class, and I see Object#to_yaml.
Seems pretty obvious what to expect here, which would seem to account
for the total lack of documentation in each of the gazillion instances
of this method which appear in core library. Still, let's test it...

irb(main):010:0> [2,5,3,6,'abc'].to_yaml
NoMethodError: undefined method `to_yaml' for [2, 5, 3, 6, "abc"]:Array
from (irb):10
from :0

Can someone explain what just happened?

Thanks!

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


28 Answers

WujcioL

3/16/2008 2:10:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

I'm beginner to but I understand it. You cannot use to_yaml for Array,
because there's no method to_yaml for Array. Try to read about
Object#to_yaml in Ruby Documentation.

2008/3/16, Tom Cloyd <tomcloyd@comcast.net>:
>
> Beginner woes, here, no doubt, but woes nevertheless.
>
> I'm exploring the wonders of the Object class, and I see Object#to_yaml.
> Seems pretty obvious what to expect here, which would seem to account
> for the total lack of documentation in each of the gazillion instances
> of this method which appear in core library. Still, let's test it...
>
> irb(main):010:0> [2,5,3,6,'abc'].to_yaml
> NoMethodError: undefined method `to_yaml' for [2, 5, 3, 6, "abc"]:Array
> from (irb):10
> from :0
>
> Can someone explain what just happened?
>
> Thanks!
>
> t.
>
> --
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Tom Cloyd, MS MA, LMHC
> Private practice Psychotherapist
> Bellingham, Washington, U.S.A: (360) 920-1226
> << tc@tomcloyd.com >> (email)
> << TomCloyd.com >> (website & psychotherapy weblog)
> << sleightmind.wordpress.com >> (mental health issues weblog)
> << directpathdesign.com >> (web site design & consultation)
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>

Stefano Crocco

3/16/2008 2:14:00 PM

0

On Sunday 16 March 2008, Tom Cloyd wrote:
> Beginner woes, here, no doubt, but woes nevertheless.
>
> I'm exploring the wonders of the Object class, and I see Object#to_yaml.
> Seems pretty obvious what to expect here, which would seem to account
> for the total lack of documentation in each of the gazillion instances
> of this method which appear in core library. Still, let's test it...
>
> irb(main):010:0> [2,5,3,6,'abc'].to_yaml
> NoMethodError: undefined method `to_yaml' for [2, 5, 3, 6, "abc"]:Array
> from (irb):10
> from :0
>
> Can someone explain what just happened?
>
> Thanks!
>
> t.

Yes. to_yaml is defined in the yaml.rb file, so you need to require it before
using to_yaml:

irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> [2,3,4,5, 'abc'].to_yaml
=> "--- \n- 2\n- 3\n- 4\n- 5\n- abc\n"

How are you supposed to know that to_yaml is not in ruby core but in the
standard library? I don't really know. When I started using ruby some years
ago, I downloaded a version of the rdoc documentation from
www.ruby-doc.org which only included documentation for the core classes (that
is, those you can use without need to require some files), while documentation
for the standard library was in a separate package. Unfortunately, the
documentation you can find nowadays includes both core and standard library.
This has the very unpleasant side effect of displaying together methods which
come from the core classes and methods which come from files in the standard
library (which should be required before using such methods). At any rate, at
the top of the page, just below the name of the class, there's a list of the
files which have been used to generate the documentation for the class. If a
method listed in the documentation doesn't seem to exist, try requiring those
files. In the specific case of Object#to_yaml, this doesn't work, though,
since the file where the method is defined, lib/yaml/rubytypes.rb, can't be
required directly, but only requiring 'yaml'. It can give you a hint.

Stefano

Phillip Gawlowski

3/16/2008 2:16:00 PM

0

Tom Cloyd wrote:
> Beginner woes, here, no doubt, but woes nevertheless.
>
> I'm exploring the wonders of the Object class, and I see Object#to_yaml.
> Seems pretty obvious what to expect here, which would seem to account
> for the total lack of documentation in each of the gazillion instances
> of this method which appear in core library. Still, let's test it...
>
> irb(main):010:0> [2,5,3,6,'abc'].to_yaml
> NoMethodError: undefined method `to_yaml' for [2, 5, 3, 6, "abc"]:Array
> from (irb):10
> from :0
>
> Can someone explain what just happened?
>
> Thanks!
>
> t.
>
irb(main):005:0> [2,3,4].to_yaml
NoMethodError: undefined method `to_yaml' for [2, 3, 4]:Array
from (irb):5
from :0
irb(main):006:0> require "yaml"
=> true
irb(main):008:0> [2,3,4].to_yaml
=> "--- \n- 2\n- 3\n- 4\n"

See also:
http://ruby-doc.org/core/classes...

--Phillip Gawlowski

Tom Cloyd

3/16/2008 2:18:00 PM

0

Documentation? WHAT documentation? Please read my email.

Also, replicate my example in irb - with any object you wish. (I tried
Array, Fixnum, Float - all raise the same error)

Finally, consider this statement from the general documentation for the
Class Object at http://www.ruby-doc...:
"Object <http://www.ruby-doc...classes/Object.html> is the parent
class <http://www.ruby-doc...classes/Object.html#M000348> of all
classes in Ruby. Its methods
<http://www.ruby-doc...classes/Object.html#M000359> are therefore
available to all objects unless explicitly overridden."

My problem remains as I stated it.

Thanks for your response, in any case. I will hold out for the
possibility that with a little more examination you'll see what I have
so far missed - the reason for this problem.

Thanks!

t.

Mateusz Tybura wrote:
> I'm beginner to but I understand it. You cannot use to_yaml for Array,
> because there's no method to_yaml for Array. Try to read about
> Object#to_yaml in Ruby Documentation.
>
> 2008/3/16, Tom Cloyd <tomcloyd@comcast.net>:
>
>> Beginner woes, here, no doubt, but woes nevertheless.
>>
>> I'm exploring the wonders of the Object class, and I see Object#to_yaml.
>> Seems pretty obvious what to expect here, which would seem to account
>> for the total lack of documentation in each of the gazillion instances
>> of this method which appear in core library. Still, let's test it...
>>
>> irb(main):010:0> [2,5,3,6,'abc'].to_yaml
>> NoMethodError: undefined method `to_yaml' for [2, 5, 3, 6, "abc"]:Array
>> from (irb):10
>> from :0
>>
>> Can someone explain what just happened?
>>
>> Thanks!
>>
>> t.
>>
>> --
>>


--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Phillip Gawlowski

3/16/2008 2:34:00 PM

0

Tom Cloyd wrote:
> Documentation? WHAT documentation? Please read my email.
>
> Also, replicate my example in irb - with any object you wish. (I tried
> Array, Fixnum, Float - all raise the same error)
>
> Finally, consider this statement from the general documentation for the
> Class Object at http://www.ruby-doc...:
> "Object <http://www.ruby-doc...classes/Object.html> is the parent
> class <http://www.ruby-doc...classes/Object.html#M000348> of all
> classes in Ruby. Its methods
> <http://www.ruby-doc...classes/Object.html#M000359> are therefore
> available to all objects unless explicitly overridden."
>
> My problem remains as I stated it.
>
> Thanks for your response, in any case. I will hold out for the
> possibility that with a little more examination you'll see what I have
> so far missed - the reason for this problem.

You don't use the tools Ruby/irb gives you:
>> Object.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", "taint", "tainted?", "to_a", "to_s",
"type", "untaint"]

As you can see, the method is not, by default, defined You already know
that the documentation leaves some things desirable, so start using
Google, or Ruby's capabilities of inspection, to narrow down your
mistakes/errors and do some troubleshooting. You'll need these skills
when writing applications, anyway.

And as an MS/MA you certainly learned research skills at college. If
they've atrophied, now's a good chance to resurrect them, no?

-- Phillip Gawlowski

Tim Hunter

3/16/2008 3:58:00 PM

0

Stefano Crocco wrote:
> How are you supposed to know that to_yaml is not in ruby core but in the
> standard library? I don't really know. When I started using ruby some years
> ago, I downloaded a version of the rdoc documentation from
> www.ruby-doc.org which only included documentation for the core classes (that
> is, those you can use without need to require some files), while documentation
> for the standard library was in a separate package. Unfortunately, the
> documentation you can find nowadays includes both core and standard library.

The ri command shows the documentation for the merged core and standard
libraries. It's sad but it's true. However, it isn't hard to find out
what's the the core and what isn't.

This link http://www.ruby-lang.org/en/docu... has a link to the
top-level www.ruby-doc.org as well as individual links to the core and
standard API sections of ruby-doc.

When I go to www.ruby-doc.org right now, the documentation for the Core
API is clearly separated from the documentation for the Standard API.
The yaml library is documented in the Standard API documentation.

When I view the online (first edition) version of _Programming_Ruby_
(http://www.ruby-doc.org/docs/Progra...) the core API is
documented in the "Built in Classes and Methods" chapter. The Standard
library is documented in the "Standard Library" chapter. The yaml
library isn't documented in that book because it didn't exist when the
first edition was written. The 2nd edition (pay for) includes yaml in
the Standard Library chapter. There is also a short tutorial about yaml
in the marshalling chapter.

--
RMagick: http://rmagick.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html

Dave Thomas

3/16/2008 4:00:00 PM

0


On Mar 16, 2008, at 8:56 AM, Tom Cloyd wrote:

> I'm exploring the wonders of the Object class, and I see
> Object#to_yaml. Seems pretty obvious what to expect here, which
> would seem to account for the total lack of documentation in each of
> the gazillion instances of this method which appear in core library.
> Still, let's test it...
>
> irb(main):010:0> [2,5,3,6,'abc'].to_yaml
> NoMethodError: undefined method `to_yaml' for [2, 5, 3, 6,
> "abc"]:Array
> from (irb):10
> from :0
>
> Can someone explain what just happened?


Tom:

Could you tell us just where you found the description of
Object#to_yaml.

As others have explained, to_yaml is not a built-in method. It is
added when you require the yaml library. So, if a piece of
documentation is out there that fails to make it clear that this
library must be loaded before to_yaml is called, then that
documentation needs to be fixed.

As others have also pointed out, once the library is loaded, you can
indeed invoke to_yaml on arrays, hashes, and so on.


Regards


Dave Thomas

Dave Thomas

3/16/2008 4:06:00 PM

0


On Mar 16, 2008, at 9:18 AM, Tom Cloyd wrote:

> I will hold out for the possibility that with a little more
> examination you'll see what I have so far missed - the reason for
> this problem.

Just to follow up on my previous email...

It's also possible you saw Object#to_yaml if you're using the 'ri'
utility and you have the yaml library installed on your local machine.
ri lists all the methods that can possibly appear in a class, and
(somewhat stupidly) fails to differential between those that are built
in and those that can be added. (I say somewhat stupidly because it
was my fault :).

Regards


Dave Thomas

Tom Cloyd

3/16/2008 4:08:00 PM

0

Phillip Gawlowski wrote:
> Tom Cloyd wrote:
>> Documentation? WHAT documentation? Please read my email.
>>
>> Also, replicate my example in irb - with any object you wish. (I
>> tried Array, Fixnum, Float - all raise the same error)
>>
>> Finally, consider this statement from the general documentation for
>> the Class Object at http://www.ruby-doc...:
>> "Object <http://www.ruby-doc...classes/Object.html> is the
>> parent class
>> <http://www.ruby-doc...classes/Object.html#M000348> of all
>> classes in Ruby. Its methods
>> <http://www.ruby-doc...classes/Object.html#M000359> are
>> therefore available to all objects unless explicitly overridden."
>>
>> My problem remains as I stated it.
>>
>> Thanks for your response, in any case. I will hold out for the
>> possibility that with a little more examination you'll see what I
>> have so far missed - the reason for this problem.
>
> You don't use the tools Ruby/irb gives you:
> >> Object.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", "taint", "tainted?", "to_a",
> "to_s", "type", "untaint"]
>
> As you can see, the method is not, by default, defined You already
> know that the documentation leaves some things desirable, so start
> using Google, or Ruby's capabilities of inspection, to narrow down
> your mistakes/errors and do some troubleshooting. You'll need these
> skills when writing applications, anyway.
>
> And as an MS/MA you certainly learned research skills at college. If
> they've atrophied, now's a good chance to resurrect them, no?
>
> -- Phillip Gawlowski
>
>
Phillip, good research begins with careful observation - in this case,
of the materials at hand. In my immediately previous email I pointed out
that the ruby core documentation, for which I gave a reference, so you
could check my accuracy (basic scholarly method), tells me that
Object#to_yaml should be available to all object unless "explicitly
overridden". I then present some cases where it isn't available. That
contradiction is what I could not explain. I do not expect such a
blatant error/omission/contradiction - or whatever - in the ruby core
documentation. If I can't trust what it says about the root object of
all objects, then what can I trust? That, to me, looks like a problem -
and not exactly one I'd expect Google or some blog entry to solve.

Your ">> Object.methods.sort " tip is useful, as is the suggestion that
I learn more of Ruby's self-inspection tools. I will pursue this. It
didn't occur to me that irb would deny use of this method. That was
clearly an erroneous assumption, howsoever reasonable it may have seemed
at the time.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Stefano Crocco

3/16/2008 4:10:00 PM

0

On Sunday 16 March 2008, Tim Hunter wrote:
> Stefano Crocco wrote:
> > How are you supposed to know that to_yaml is not in ruby core but in the
> > standard library? I don't really know. When I started using ruby some
> > years ago, I downloaded a version of the rdoc documentation from
> > www.ruby-doc.org which only included documentation for the core classes
> > (that is, those you can use without need to require some files), while
> > documentation for the standard library was in a separate package.
> > Unfortunately, the documentation you can find nowadays includes both core
> > and standard library.
>
> The ri command shows the documentation for the merged core and standard
> libraries. It's sad but it's true. However, it isn't hard to find out
> what's the the core and what isn't.
>
> This link http://www.ruby-lang.org/en/docu... has a link to the
> top-level www.ruby-doc.org as well as individual links to the core and
> standard API sections of ruby-doc.
>
> When I go to www.ruby-doc.org right now, the documentation for the Core
> API is clearly separated from the documentation for the Standard API.
> The yaml library is documented in the Standard API documentation.

As far as I can see, the links to the 'Core documentation' on both www.ruby-
doc.org and http://www.ruby-lang.org/en/docu... point to the same
documentation, which sadly contains both the core and the standard library. As
you say, the standard library is also documented in its own section, which is
much more organized, and easy to use.

Stefano