Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Strings to int conversion

Reply
Thread Tools

Strings to int conversion

 
 
Peter Wu
Guest
Posts: n/a
 
      06-26-2009
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/.

 
Reply With Quote
 
 
 
 
Aaron Patterson
Guest
Posts: n/a
 
      06-26-2009
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?


"1234".split('')[1].to_i # => 2

--
Aaron Patterson
http://tenderlovemaking.com/

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      06-26-2009
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?


s[1,1]
s[1..1]

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
Peter Wu
Guest
Posts: n/a
 
      06-26-2009
Thanks, guys.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Bertram Scharpf
Guest
Posts: n/a
 
      06-26-2009
Hi,

Am Samstag, 27. Jun 2009, 05:55:20 +0900 schrieb Peter Wu:
> s = "1234" [0..1]
> s = s[1]
> p s #> 50
>
> I can see the reason for this happening, but I don't want it to happen.


The answer has already been given.

As far as I know Ruby 1.9 returns "2".

There's another [] operator that's not well known:

16[4] #=> 1 (the 4th bit is 1)

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

 
Reply With Quote
 
Robert Schaaf
Guest
Posts: n/a
 
      06-27-2009
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/.
>



 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      06-27-2009
Hi --

On Sat, 27 Jun 2009, 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?


Addendum to other answers:

In 1.9 it returns the character rather than the character code.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.envycasts.com

 
Reply With Quote
 
Daniel DeLorme
Guest
Posts: n/a
 
      06-28-2009
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


 
Reply With Quote
 
pierr
Guest
Posts: n/a
 
      06-30-2009



Peter Wu-5 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?
>
>

p s[1].chr.to_i #=> 2



--
View this message in context: http://www.nabble.com/Strings-to-int...p24268762.html
Sent from the ruby-talk mailing list archive at Nabble.com.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57