Forum: Ruby on Rails Creating array, decimal

Posted by Werner (Guest)
on 2013-02-27 13:24
(Received via mailing list)
Hi
I have an attribute decimal => hour

If I do

hours.each do |h|
      h.my_hour
end
I get => 20.0 0.0 0.0 10.0

And this..
myhour = []
hours.each do |h|
myhour << hour
end
myhour.join(",")

I get =>
<Hour:0x007f81feec3330>,#<Hour:0x007f81feaa0c30>,

How do I get something like this ?
20.0, 0.0, 0.0, 10.0

Thanks for support
Posted by Dheeraj Kumar (Guest)
on 2013-02-27 13:32
(Received via mailing list)
override Hour's to_s method to return my_hour

--
Dheeraj Kumar
Posted by Werner (Guest)
on 2013-02-27 13:45
(Received via mailing list)
Thanks..but
mhhh..it is not clear what you mean...

hour.join(",").to_s ?????
no effect..


Am Mittwoch, 27. Februar 2013 13:31:21 UTC+1 schrieb Dheeraj Kumar:
Posted by Carlos Mathiasen (Guest)
on 2013-02-27 14:14
(Received via mailing list)
Matt's


On Wed, Feb 27, 2013 at 9:43 AM, Werner 
<webagentur.laude@googlemail.com>wrote:

>>
>> hours.each do |h|
>>       h.my_hour
>> end
>> I get => 20.0 0.0 0.0 10.0
>>
>> And this..
>> myhour = []
>> hours.each do |h|
>> myhour << hour
>>
>> Here is the problem:
  myhour << h.my_hours
Posted by Werner (Guest)
on 2013-02-27 14:26
(Received via mailing list)
Well my be I am a litte stubborn today..
What I need is an array

[20.0, 0.0, 0.0, 10.0]




Am Mittwoch, 27. Februar 2013 14:13:09 UTC+1 schrieb Carlos Mathiasen:
Posted by Carlos Mathiasen (Guest)
on 2013-02-27 14:29
(Received via mailing list)
this work for me:

hours.collect{|h| h.my_hour}


Matt's
Posted by Dheeraj Kumar (Guest)
on 2013-02-27 14:33
(Received via mailing list)
or

hours.map(&:my_hour)

--
Dheeraj Kumar
Posted by Werner (Guest)
on 2013-02-27 15:23
(Received via mailing list)
After chaning the datatype to integer..
yes..works

 but as decimal (6,2)
stays : <Hour:0x007f81feec3330>,#<Hour:0x007f81feaa0c30>,

Thanks so far


Am Mittwoch, 27. Februar 2013 14:31:46 UTC+1 schrieb Dheeraj Kumar:
Posted by Colin Law (Guest)
on 2013-02-27 16:07
(Received via mailing list)
On 27 February 2013 12:23, Werner <webagentur.laude@googlemail.com> 
wrote:
> And this..
> myhour = []
> hours.each do |h|
> myhour << hour

That should be
myhour << h.myhour
though myhour is a very poor name for an array.  It should be plural.

> end
> myhour.join(",")
>
> I get =>
> <Hour:0x007f81feec3330>,#<Hour:0x007f81feaa0c30>,

That it because you are adding Hour objects into the array, not decimal 
objects.

Colin
Posted by Werner Laude (Guest)
on 2013-02-27 16:13
(Received via mailing list)
Am 27.02.2013 um 16:05 schrieb Colin Law <clanlaw@googlemail.com>:

>>
>> myhour.join(",")
>>
>> I get =>
>> <Hour:0x007f81feec3330>,#<Hour:0x007f81feaa0c30>,
>
> That it because you are adding Hour objects into the array, not decimal objects.

o.k..
true..

Thanks
Posted by Colin Law (Guest)
on 2013-02-27 16:18
(Received via mailing list)
On 27 February 2013 15:11, Werner Laude 
<webagentur.laude@googlemail.com> wrote:
>>>      h.my_hour
>> though myhour is a very poor name for an array.  It should be plural.
> true..
>
> Thanks

Carlos did point out the problem earlier.  It is worth reading the 
answers
carefully when you ask a question.

Colin
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.