Reputation: 9
This was the behaviour in 1.9 ruby:
[].to_s.empty? #=> false
{}.to_s.empty? #=> false
[].to_s #=> "[]"
{}.to_s #=> "{}"
[43,43].to_s #=> "[43, 43]"
{"fire"=>"water"}.to_s #=> "{\"fire\"=>\"water\"}"
This is the behaviour in 2.0 ruby:
[].to_s.empty? #=> true
{}.to_s.empty? #=> true
[].to_s #=> ""
{}.to_s #=> ""
[43,43].to_s #=> "4343"
{"fire"=>"water"}.to_s #=> "firewater"
I am wondering about this change in behaviour for to_s
method in 2.0.
Edit: My apologies. I am closing this question. For some reason I thought I was running 2.x.x ruby but the path was messed up. It pointed to 1.8.7 version of ruby that was on the image. When I fixed the path to point at the right ruby (2.1.4) I see the right behaviour.
Upvotes: 0
Views: 129
Reputation: 176552
In 2.x, this is the output of [].to_s
2.1.5 :001 > [].to_s
=> "[]"
2.1.5 :002 > [43,43].to_s
=> "[43, 43]"
Given that it doesn't match your Ruby setup, there is definitely something that is changing it in your system.
Try to start a simple, clear irb
session. If it works like I have shown above, it means there is something wrong in your Ruby app.
If it doesn't work, it means there is something wrong in your Ruby setup.
Regardless of where the problem is, I wonder why you are relying on a code like
[].to_s.empty?
I can't find a single case where the code above would ever make sense. If you want to check the presence of some item in the array, simply use
[].empty?
If you want to treat the array as string, convert it using a possible reasonable approach, such as join
[].join("").empty?
Upvotes: 1