s = "1234" [0..1]
p s #> "12"
p s.to_i #> 12
s = s[1]
p s #> 50
p s.to_i #> 50
I can see the reason for this happening, but I don't want it to happen.
So how do I make it return 2 instead of 50?
···
--
Posted via http://www.ruby-forum.com/.
"1234".split('')[1].to_i # => 2
···
On Sat, Jun 27, 2009 at 05:55:20AM +0900, Peter Wu wrote:
s = "1234" [0..1]
p s #> "12"
p s.to_i #> 12
s = s[1]
p s #> 50
p s.to_i #> 50
I can see the reason for this happening, but I don't want it to happen.
So how do I make it return 2 instead of 50?
--
Aaron Patterson
http://tenderlovemaking.com/
One of Ruby's problems is that it invites clever solutions, which are computationally costly and create undue amounts of garbage.
How about
?> "1234"[1].chr
=> "2"
>>
Cheers,
Bob Schaaf
···
On Jun 26, 2009, at 4:55 PM, Peter Wu wrote:
s = "1234" [0..1]
p s #> "12"
p s.to_i #> 12
s = s[1]
p s #> 50
p s.to_i #> 50
I can see the reason for this happening, but I don't want it to happen.
So how do I make it return 2 instead of 50?
--
Posted via http://www.ruby-forum.com/\.
Bertram Scharpf wrote:
There's another operator that's not well known:
16[4] #=> 1 (the 4th bit is 1)
Wow, I've been using ruby for many years but there's
always something new to learn isn't it? Thanks.
Daniel