[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

to_yaml method for array

Raj Singh

5/21/2008 6:38:00 PM

This is irb.


> irb
irb(main):001:0> a = %w[ 1 2 3 ]
=> ["1", "2", "3"]
irb(main):002:0> a.to_yaml
NoMethodError: undefined method `to_yaml' for ["1", "2", "3"]:Array
from (irb):2


This is Rails script/console

>> a = %w[ 1 2 3]
=> ["1", "2", "3"]
>> a.to_yaml
=> "--- \n- \"1\"\n- \"2\"\n- \"3\"\n"


However no where in ActiveSupport I can find where Rails is implementing
the method to_yaml.

I go to http://www.ruby-doc... and look for Array methods. I see
to_yaml. So why in the irb I am getting undefined method to_yaml?
--
Posted via http://www.ruby-....

14 Answers

Jason Roelofs

5/21/2008 6:48:00 PM

0

require 'yaml'

On 5/21/08, Raj Singh <neeraj.jsr@gmail.com> wrote:
> This is irb.
>
>
> > irb
> irb(main):001:0> a = %w[ 1 2 3 ]
> => ["1", "2", "3"]
> irb(main):002:0> a.to_yaml
> NoMethodError: undefined method `to_yaml' for ["1", "2", "3"]:Array
> from (irb):2
>
>
> This is Rails script/console
>
> >> a = %w[ 1 2 3]
> => ["1", "2", "3"]
> >> a.to_yaml
> => "--- \n- \"1\"\n- \"2\"\n- \"3\"\n"
>
>
> However no where in ActiveSupport I can find where Rails is implementing
> the method to_yaml.
>
> I go to http://www.ruby-doc... and look for Array methods. I see
> to_yaml. So why in the irb I am getting undefined method to_yaml?
>
> --
> Posted via http://www.ruby-....
>
>

Phillip Gawlowski

5/21/2008 6:48:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Raj Singh wrote:

| However no where in ActiveSupport I can find where Rails is implementing
| the method to_yaml.
|
| I go to http://www.ruby-doc... and look for Array methods. I see
| to_yaml. So why in the irb I am getting undefined method to_yaml?

require 'yaml'

This adds the desired functionality. Alas, the RDoc generated
(currently) includes functions in Core lib that are *actually* part of
STDLIB.

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.bl...

A born loser:
~ Somebody who calls the number that's scrawled in lipstick on the phone
~ booth wall-- and his wife answers.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkg0bmwACgkQbtAgaoJTgL8EFwCeLjb0NO34MOgJFKTX/GGElor8
/vMAoJq1lX5r1S6gxjvNQsw1szZzSTKJ
=QPb9
-----END PGP SIGNATURE-----

Sean O'Halpin

5/21/2008 6:49:00 PM

0

On Wed, May 21, 2008 at 7:37 PM, Raj Singh <neeraj.jsr@gmail.com> wrote:
>
> I go to http://www.ruby-doc... and look for Array methods. I see
> to_yaml. So why in the irb I am getting undefined method to_yaml?

You need to

require 'yaml'

to load the yaml library.

Regards,
Sean

Stefano Crocco

5/21/2008 7:00:00 PM

0

On Wednesday 21 May 2008, Raj Singh wrote:
> This is irb.
>
> > irb
>
> irb(main):001:0> a = %w[ 1 2 3 ]
> => ["1", "2", "3"]
> irb(main):002:0> a.to_yaml
> NoMethodError: undefined method `to_yaml' for ["1", "2", "3"]:Array
> from (irb):2
>
>
> This is Rails script/console
>
> >> a = %w[ 1 2 3]
>
> => ["1", "2", "3"]
>
> >> a.to_yaml
>
> => "--- \n- \"1\"\n- \"2\"\n- \"3\"\n"
>
>
> However no where in ActiveSupport I can find where Rails is implementing
> the method to_yaml.
>
> I go to http://www.ruby-doc... and look for Array methods. I see
> to_yaml. So why in the irb I am getting undefined method to_yaml?

You need to require 'yaml' before using the Array#to_yaml method (or any other
predefined to_yaml methods, for the matter). I guess Rails console already
does that itself, so there your code works. Regarding the documentation,
unfortunately the one you mention mixes 'core' classes (that is, classes and
modules which are always availlable) and 'standard library' classes (classes
and modules which are distributed with ruby but need to be required to be
used). The to_yaml method is defined in a file in the standard library, but
that can't be guessed from the documentation. If you want to generate the
documentation for only the core classes, you can follow the procedure
explained in this thread:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
The documentation for only the standard library can be downloaded here:
http://www.ruby-doc.o...

I hope this helps

Stefano


James Britt

5/21/2008 7:42:00 PM

0


>
> I go to http://www.ruby-doc... and look for Array methods. I see
> to_yaml. So why in the irb I am getting undefined method to_yaml?

rdoc, run over the ruby source, is dopey in that it parses the yaml
files, sees that they modify core Ruby objects, then generates rdoc that
presents those core objects as if they had yaml methods built-in.

In actual practice, you have to explicitly mix-in yaml to get that behavior.


--
James Britt

http://www.... - Hacking in the Desert
http://www.jame... - Playing with Better Toys

Stefano Crocco

5/21/2008 7:49:00 PM

0

On Wednesday 21 May 2008, James Britt wrote:
> > I go to http://www.ruby-doc... and look for Array methods. I see
> > to_yaml. So why in the irb I am getting undefined method to_yaml?
>
> rdoc, run over the ruby source, is dopey in that it parses the yaml
> files, sees that they modify core Ruby objects, then generates rdoc that
> presents those core objects as if they had yaml methods built-in.
>
> In actual practice, you have to explicitly mix-in yaml to get that
> behavior.

Actually, you don't need to mix-in yaml. When you require yaml.rb, it will add
the generic to_yaml method to the Object class itself (or to the Kernel
module, I'm not sure) and the more specialized methods to those classes for
which they exist (such as Array, String and Hash). There's no mixing-in
involved, as far as I can tell.

Stefano


Marc Heiler

5/21/2008 9:27:00 PM

0

You followup-guys are overkilling on such a simple question with such a
simple solution... i am sure in some time there be dragons! ;-)
--
Posted via http://www.ruby-....

Phillip Gawlowski

5/21/2008 9:32:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Marc Heiler wrote:
| You followup-guys are overkilling on such a simple question with such a
| simple solution... i am sure in some time there be dragons! ;-)

Though, if somebody uses a search engine, the chance of finding a
solution to the same problem by somebody else increases.

Also, explanations as to *why* something is not the way as it is
expected helps in troubleshooting problems of a similar nature. ;)

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.bl...

ZEAL:
Quality seen in new graduates -- if you're quick.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkg0lPgACgkQbtAgaoJTgL9LYQCbBTlelRDg6GDaz2rAJW/GHlBt
mt4AoJCmci3AVykF6Pt6jM4xl/WwSaYF
=rez5
-----END PGP SIGNATURE-----

James Britt

5/22/2008 3:14:00 AM

0

Stefano Crocco wrote:
> Actually, you don't need to mix-in yaml. When you require yaml.rb, it will add
> the generic to_yaml method to the Object class itself (or to the Kernel
> module, I'm not sure) and the more specialized methods to those classes for
> which they exist (such as Array, String and Hash). There's no mixing-in
> involved, as far as I can tell.

Yes, you are quite right.



--
James Britt

http://www.... - Hacking in the Desert
http://www.jame... - Playing with Better Toys

smartin

5/12/2009 11:45:00 PM

0

Hi Lynn,

I cannot think of any other way...

PathPart1 = "\\server\abc\def\"
PathPart2 = "\[filename.xls]worksheetname!A1"
UserInput = ' whatever you use to obtain from UI

MyPath = PathPart1 & UserInput & PathPart2

There is nothing inelegant about this; that is the way it is done. I
still feel like I'm missing something. Why the aversion to this way?


Lynn wrote:
> Sorry for the late reply, away from email for a few days. That was the whole
> point of my question - is there a way to do it without having to concatenate
> several strings?
> Still hoping for another way to do it...
>
> "smartin" wrote:
>
>> Lynn wrote:
>>> Posted this in Link section but no response yet so posting here. I need to
>>> link many cells in 2 workbooks to transfer data from one to the other; the
>>> path to the
>>> source workbook will always be the same except for one folder, which
>>> is the project number and will vary every time. Is there an easier or more
>>> elegant way to insert this variable folder into the link path other than
>>> CONCATENATE? I have several options on how to get user to enter the project
>>> number, but am missing that final step of getting user's input into the path
>>> link. Example: '\\server\abc\def\PROJECT NUMBER GOES
>>> HERE\[filename.xls]worksheetname'!a1. Thanks!
>>>
>>
>> Seems pretty straightforward... obtain the user input with a form
>> control or input box and build up the path string via concatenation. Or
>> am I missing something?
>>