[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

displaying an array

7stud 7stud

8/29/2007 9:09:00 PM

In irb, an array displays like this:

irb(main):001:0> a = %w(bird dog cat)
=> ["bird", "dog", "cat"]
irb(main):002:0>

Is there a command to get the same format when I display an array with a
program?

r1test.rb:
----------
a1 = %w(cat dog bird)
puts a1

$ ruby r1test.rb
cat
dog
bird


Also, this code:

----------
a1 = %w(cat dog bird)
puts a1

puts

a2 = %w{john joe jim}
puts a2

--output:--
cat
dog
bird
john
joe
jim
-----------------

seems to suggest that braces and parentheses are equivalent. Is that
the case?

Thanks
--
Posted via http://www.ruby-....

3 Answers

Sebastian Hungerecker

8/29/2007 9:22:00 PM

0

7stud 7stud wrote:
> In irb, an array displays like this:
>
> irb(main):001:0> a = %w(bird dog cat)
> => ["bird", "dog", "cat"]
> irb(main):002:0>
>
> Is there a command to get the same format when I display an array with a
> program?

p (which is equivalent to puts foo.inspect)


> Also, this code:
>
> a1 = %w(cat dog bird)
> puts a1
>
> a2 = %w{john joe jim}
> puts a2
>[...]
> seems to suggest that braces and parentheses are equivalent. Is that
> the case?

You can use any non-alphanumeric character with %w (or %r or whatever). It
won't make a difference. Examples:
%w<la lu li>
%w_li la lo_
%w#chunky bacon#
%w)foo bar) # This one's strange, I know.
etc.

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Phrogz

8/29/2007 10:18:00 PM

0

7stud 7stud wrote:
> In irb, an array displays like this:
>
> irb(main):001:0> a = %w(bird dog cat)
> => ["bird", "dog", "cat"]
> irb(main):002:0>
>
> Is there a command to get the same format when I display an array with a
> program?

C:\>type p.rb
a = %w(cat dog bird)
p a

C:\>ruby p.rb
["cat", "dog", "bird"]
--
Posted via http://www.ruby-....

7stud 7stud

8/30/2007 4:31:00 AM

0

Sebastian Hungerecker wrote:
>> Is there a command to get the same format when I display an array with a
>> program?
>
> p (which is equivalent to puts foo.inspect)
>
>
> You can use any non-alphanumeric character with %w (or %r or whatever).
> It won't make a difference.

Thanks Sebastian. Thanks Gavin.
--
Posted via http://www.ruby-....