[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby doesn't convert from String to int

Yu Co

9/21/2006 3:57:00 AM

Hi all,

I have the following peace of code:
arr = [5, 5, 2002]
date = Date.new(arr[2].to_i, arr[1].to_i, arr[0].to_i)

I need to convert the String elements in the array into int. I want to
do that with to_i, but this method returns me a Fixnum, thus I couldn't
fill my date Object...
Any suggestions?

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

7 Answers

Austin Ziegler

9/21/2006 4:02:00 AM

0

On 9/20/06, Yu Co <djhackebeil@yahoo.de> wrote:
> Hi all,
>
> I have the following peace of code:
> arr = [5, 5, 2002]
> date = Date.new(arr[2].to_i, arr[1].to_i, arr[0].to_i)
>
> I need to convert the String elements in the array into int. I want to
> do that with to_i, but this method returns me a Fixnum, thus I couldn't
> fill my date Object...
> Any suggestions?

Yes. Try it again.

1. Your array contains Fixnum values already.
2. You're probably getting a different error.
3. Fixnum values are integer values.

If you have a problem, your real code and/or a backtrace would be useful.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Yu Co

9/21/2006 5:33:00 AM

0

Austin Ziegler wrote:
> On 9/20/06, Yu Co <djhackebeil@yahoo.de> wrote:
>> Hi all,
>>
>> I have the following peace of code:
>> arr = [5, 5, 2002]
>> date = Date.new(arr[2].to_i, arr[1].to_i, arr[0].to_i)
>>
>> I need to convert the String elements in the array into int. I want to
>> do that with to_i, but this method returns me a Fixnum, thus I couldn't
>> fill my date Object...
>> Any suggestions?
>
> Yes. Try it again.
>
> 1. Your array contains Fixnum values already.
> 2. You're probably getting a different error.
> 3. Fixnum values are integer values.
>
> If you have a problem, your real code and/or a backtrace would be
> useful.
>
> -austin

The error message looks like
{RUBY_HOME}/lib/ruby/1.8/date.rb:591:in `new': invalid date
(ArgumentError)
from {}db/old_DB.rb:98:in `parseToDate'
from {}db/old_DB.rb:61:in `callMethod'
from {}db/old_DB.rb:47:in `callMethod'
...

I am wondering why Ruby converts a String to Fixnum after calling to_i

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

snacktime

9/21/2006 5:58:00 AM

0

On 9/20/06, Yu Co <djhackebeil@yahoo.de> wrote:
> Austin Ziegler wrote:
> > On 9/20/06, Yu Co <djhackebeil@yahoo.de> wrote:
> >> Hi all,
> >>
> >> I have the following peace of code:
> >> arr = [5, 5, 2002]
> >> date = Date.new(arr[2].to_i, arr[1].to_i, arr[0].to_i)
> >>
> >> I need to convert the String elements in the array into int. I want to
> >> do that with to_i, but this method returns me a Fixnum, thus I couldn't
> >> fill my date Object...
> >> Any suggestions?
> >
> > Yes. Try it again.
> >
> > 1. Your array contains Fixnum values already.
> > 2. You're probably getting a different error.
> > 3. Fixnum values are integer values.
> >
> > If you have a problem, your real code and/or a backtrace would be
> > useful.
> >
> > -austin
>
> The error message looks like
> {RUBY_HOME}/lib/ruby/1.8/date.rb:591:in `new': invalid date
> (ArgumentError)
> from {}db/old_DB.rb:98:in `parseToDate'
> from {}db/old_DB.rb:61:in `callMethod'
> from {}db/old_DB.rb:47:in `callMethod'
> ...
>
> I am wondering why Ruby converts a String to Fixnum after calling to_i

Because that's what to_i does? An integer IS a Fixnum. Also, the
code you posted as not working works just fine, which is why the
subtle hint by Austin to post the *real* code that is actually
breaking. Chances are you array doesn't contain what you assume it
does.

Yu Co

9/21/2006 6:05:00 AM

0

snacktime wrote:
> On 9/20/06, Yu Co <djhackebeil@yahoo.de> wrote:
>> >> fill my date Object...
>> >
>> I am wondering why Ruby converts a String to Fixnum after calling to_i
> Because that's what to_i does? An integer IS a Fixnum. Also, the
> code you posted as not working works just fine, which is why the
> subtle hint by Austin to post the *real* code that is actually
> breaking. Chances are you array doesn't contain what you assume it
> does.

All right, then I'm wondering why the array doesn't accept Fixnum as an
argument...

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

Paul Lutus

9/21/2006 7:00:00 AM

0

Yu Co wrote:

[ from an earlier post ]

>> I have the following peace of code:
>> arr = [5, 5, 2002]
>> date = Date.new(arr[2].to_i, arr[1].to_i, arr[0].to_i)

>> I need to convert the String elements in the array into int.

They are already Fixnums.

> All right, then I'm wondering why the array doesn't accept Fixnum as an
> argument...

What? The array does accept fixum as an argument. The arguments you put into
the array are Fixnum objects.

-----------------------------------------

#!/usr/bin/ruby -w

arr = [5, 5, 2002]

arr.each do |element|
puts element.class
end

-----------------------------------------

Output:

Fixnum
Fixnum
Fixnum

--
Paul Lutus
http://www.ara...

Vance A Heron

9/21/2006 8:19:00 AM

0

Yu Co wrote:

>Hi all,
>
>I have the following peace of code:
>arr = [5, 5, 2002]
>date = Date.new(arr[2].to_i, arr[1].to_i, arr[0].to_i)
>
>I need to convert the String elements in the array into int. I want to
>do that with to_i, but this method returns me a Fixnum, thus I couldn't
>fill my date Object...
>Any suggestions?
>
>
This program works fine for me ...
heron@ka6toe ~ $ cat td.rb
#! /usr/bin/env ruby
#
require 'date'
arr = [5,5,2002]
date = Date.new(arr[2], arr[1], arr[0])
puts "Date is #{date}"
heron@ka6toe ~ $ ./td.rb
Date is 2002-05-05
heron@ka6toe ~ $

Another option is to use the * (splat) operator as follows:

heron@ka6toe ~ $ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> arr = [5,5,2002]
=> [5, 5, 2002]
irb(main):003:0> date = Date.new(*arr.reverse)
=> #<Date: 4904799/2,0,2299161>
irb(main):004:0> date.to_s
=> "2002-05-05"
irb(main):005:0> quit
heron@ka6toe ~ $


HTH,
Vance




James Gray

9/21/2006 1:46:00 PM

0

On Sep 21, 2006, at 1:05 AM, Yu Co wrote:

> All right, then I'm wondering why the array doesn't accept Fixnum
> as an
> argument...

You are having trouble with your assumptions. A Fixnum is an
Integer. Arrays accept Integers. Your sample code posted runs
without modification and thus isn't broken at all.

You real code has a bug, but you will need to show us that code if
you want us to help fix it.

James Edward Gray II