Vance A Heron
9/21/2006 8:19:00 AM
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