tags:

views:

81

answers:

3

This question is about formatting ruby's strings.

In Python, built-in data structures have a built-in to-string method, and so when a variable is printed, the string is conveniently formatted to be reflective of the data structure used. For example:

>>>$ python
Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
$>>> a = [1,2,3,4]
$>>> str(a)
'[1, 2, 3, 4]'
$>>> print a
[1, 2, 3, 4]
$>>> d = { "a":"a", "b":"b", 1:5 }
$>>> str(d)
"{'a': 'a', 1: 5, 'b': 'b'}"
$>>> print d
{'a': 'a', 1: 5, 'b': 'b'}
$>>> x = [1, 23, 4]
$>>> print x
[1, 23, 4]

notice that when i print a, the value is [1, 2, 3, 4]

However, in ruby, when i try to do the same things, i get this result:

>>>$ irb
irb(main):001:0> x = [1,23,4]
=> [1, 23, 4]
irb(main):002:0> x.to_s
=> "1234"
irb(main):003:0> puts x
1
23
4 
=> nil
irb(main):004:0> print x
1234=> nil
irb(main):005:0> h = { "a" => "a", 1 => 5, 'b'=>'b' } 
=> {"a"=>"a", "b"=>"b", 1=>5}
irb(main):006:0> print h 
aabb15=> nil
irb(main):007:0> h.to_s
=> "aabb15"
irb(main):008:0> puts h
aabb15
=> nil
irb(main):009:0>

As you can see, there is no formatting with the to_s method. Furthermore, there's a uniqueness problem if i call to_s on [1,2,3,4] and [1,23,4] and [1234] because the to_s clumps all elements together so they all end up being "1234". I know that i can try to emulate the python built-in to-string methods for every native data structure by overriding the to_s method ( "[" + a.join(",") + "]" #just for arrays), but i was wondering if there is a better alternative since hacking it would seem to break the convention-over-configuration concept.

So is there a ruby equivalent of python's built-in to-string method?

+6  A: 
[1,23,4].inspect #=> "[1, 23, 4]"
p [1,23,4] # Same as  puts [1,23,4].inspect
sepp2k
gotcha, that's closer to what i wanted. thanks. I read through the Array API but couldn't find a get-around still for this:----------------------------------------------irb(main):002:0> [ 1,2,3,4,'a',"b" ].inspect=> "[1, 2, 3, 4, \"a\", \"b\"]"----------------------------------------------------it'd be nice to not see the backslashes. is this possible?
dzt
print [1,2,3,4,'a',"b"].inspect => [1,2,3,4,'a',"b"]
Adam
thanks Adam. But just out of curiosity, how would you retain the value of the nicely formatted string? when you called print, the return value is a nil, not the formatted string.
dzt
@dzt: Your confusion seems to be that irb displays string in escaped form. I.e. if you have a string containing a tab, irb will print "\t" instead of an actual tab. The string still contains a real tab though. So if you do `mystring = [1,2,3,4,'a',"b"].inspect`, then mystring will contain a `b` surrounded by quotes and no backslashes even if irb displays the quotes as `\"`.
sepp2k
Ah, yes, i see now. Thanks for clearing that up.
dzt
A: 

Use inspect

irb(main):001:0> h = { "a" => "a", 1 => 5, 'b'=>'b' }
=> {"a"=>"a", "b"=>"b", 1=>5}
irb(main):003:0> puts h.inspect
{"a"=>"a", "b"=>"b", 1=>5}
=> nil
irb(main):004:0>
Larry K
thanks. but is there a get-around for this:::: [ 1,2,3,4,'a',"b" ].inspect => "[1, 2, 3, 4, \"a\", \"b\"]" ------- as in is it possible to not see the backslashes?
dzt
Yes. As I show in my example, (in IRB), use puts h.inspect, not h.inspect
Larry K
+3  A: 
Jörg W Mittag
Thanks, this helps clear up a lot. Thanks for introducing Hirb too, this work great with viewing Rails's active record models.
dzt