[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

type conversion

Shandy Nantz

7/17/2007 8:12:00 PM

So there is an to_s for strings and a to_i for integers. However, if I
have an array of integers and I want to grab one, I get back and ASCII
character. For example, when I grab a 1 from the array it returns a 49 -
but I wanted the one! How do I convert it back to its integer
want-to-be? Thanks,

~S

P.S. I imagine this is probably an easy solution but, I am a ruby
newbie.

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

6 Answers

James Britt

7/17/2007 8:32:00 PM

0

Shandy Nantz wrote:
> So there is an to_s for strings and a to_i for integers. However, if I
> have an array of integers and I want to grab one, I get back and ASCII
> character.

Are you sure you have an array of integers?

Show some code.

> For example, when I grab a 1 from the array it returns a 49 -
> but I wanted the one! How do I convert it back to its integer
> want-to-be? Thanks,
>


This works for me:

# Create int array, 11 to 19
# the * expands the range into an array
a = *11..19

# print it to see what we have
p a

# See what we have at the first location
p a[0] # I get 11



--
James Britt

"Take eloquence and wring its neck."
- Paul Verlaine

Ian Whitlock

7/17/2007 10:06:00 PM

0

Shandy Nantz wrote:
> So there is an to_s for strings and a to_i for integers. However, if I
> have an array of integers and I want to grab one, I get back and ASCII
> character. For example, when I grab a 1 from the array it returns a 49 -
> but I wanted the one! How do I convert it back to its integer
> want-to-be? Thanks,
>
> ~S
>
> P.S. I imagine this is probably an easy solution but, I am a ruby
> newbie.

Shandy, Perhaps this helps. Note the difference between 004 and 006

irb(main):003:0> i = "1234"
=> "1234"
irb(main):004:0> i[0]
=> 49
irb(main):005:0> i[0].class
=> Fixnum
irb(main):006:0> i[0,1]
=> "1"
irb(main):007:0> i[0,1].class
=> String
irb(main):008:0> i[0,1].to_i
=> 1
irb(main):009:0> i[0,1].to_i.class
=> Fixnum


Good luck from another newbie.
Ian

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

John Joyce

7/17/2007 10:42:00 PM

0


On Jul 17, 2007, at 5:05 PM, Ian Whitlock wrote:

> Shandy Nantz wrote:
>> So there is an to_s for strings and a to_i for integers. However,
>> if I
>> have an array of integers and I want to grab one, I get back and
>> ASCII
>> character. For example, when I grab a 1 from the array it returns
>> a 49 -
>> but I wanted the one! How do I convert it back to its integer
>> want-to-be? Thanks,
>>
>> ~S
>>
>> P.S. I imagine this is probably an easy solution but, I am a ruby
>> newbie.
>
> Shandy, Perhaps this helps. Note the difference between 004 and 006
>
> irb(main):003:0> i = "1234"
> => "1234"
> irb(main):004:0> i[0]
> => 49
> irb(main):005:0> i[0].class
> => Fixnum
> irb(main):006:0> i[0,1]
> => "1"
> irb(main):007:0> i[0,1].class
> => String
> irb(main):008:0> i[0,1].to_i
> => 1
> irb(main):009:0> i[0,1].to_i.class
> => Fixnum
>
> --
> Posted via http://www.ruby-....
>
arr = "1234"

Will only give you a string.
You need :
arr = Array.new
arr << 1
arr << 2
...
OR
arr = [1,2,3,4,]

At the command line, use ri:
ri Array
ri '[]='
ri 'Array#[]='
ri 'String#[]='


Shandy Nantz

7/18/2007 2:52:00 PM

0

Sorry about not giving code. I did finally get it to work, but here is
what I had to do:

card_num = "123456789"
c = 0

char = card_num[i..i].to_i

before I had:

char = card_num[i].to_i

All I wanted to do was to pick out a single character (a digit) from
that string and perform some calculations on it. But by just saying [i]
I got a 57 value when I wanted the 9 value. When I said [i..i] I got the
9 value. Thanks for all your help,

~S

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

Peter Hickman

7/18/2007 3:01:00 PM

0

Shandy Nantz wrote:
> Sorry about not giving code. I did finally get it to work, but here is
> what I had to do:
>
> card_num = "123456789"
> c = 0
>
> char = card_num[i..i].to_i
>
> before I had:
>
> char = card_num[i].to_i
>
> All I wanted to do was to pick out a single character (a digit) from
> that string and perform some calculations on it. But by just saying [i]
> I got a 57 value when I wanted the 9 value. When I said [i..i] I got the
> 9 value. Thanks for all your help,
>
> ~S
>
>
Card_num[i] is returning the character value at position i.

card_num[i].chr will return the character at position i

card_num[i].chr.to_i will return the integer value of the character

card_num[i].type => Fixnum

card_num[i..i].type => String and is why card_num[i..i].to_i worked



Morton Goldberg

7/18/2007 3:07:00 PM

0

On Jul 18, 2007, at 10:51 AM, Shandy Nantz wrote:

> Sorry about not giving code. I did finally get it to work, but here is
> what I had to do:
>
> card_num = "123456789"
> c = 0
>
> char = card_num[i..i].to_i
>
> before I had:
>
> char = card_num[i].to_i
>
> All I wanted to do was to pick out a single character (a digit) from
> that string and perform some calculations on it. But by just saying
> [i]
> I got a 57 value when I wanted the 9 value. When I said [i..i] I
> got the
> 9 value. Thanks for all your help,

It's a bit late, I guess, but did you consider something like:

"123456789"[-1] - ?0 # => 9

Regards, Morton