Is this an executable code?

Time.local(year, month, day, hour, min, sec, msec)

I typed this in IRB and it was
"undefined local variable or method 'year'...."

Is 'local' a method for the Time class?

Thanks guys

···

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

Hi,

local is a valid method for the Time class, you can check for that if wish
to using this : Time.respond_to?(:local)

The problem here is that in your call to local, you have passed in 7
arguments, from the error it is clear that you haven't defined the variables
year, month, day...etc before making this call.
Check the values you are passing to the function call.

···

--
Thanks & Regards,
Dhruva Sagar.

On Sat, Oct 16, 2010 at 11:53, Kaye Ng <sbstn26@yahoo.com> wrote:

Time.local(year, month, day, hour, min, sec, msec)

I typed this in IRB and it was
"undefined local variable or method 'year'...."

Is 'local' a method for the Time class?

Thanks guys

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

One more thing.
I typed this in IRB:

epoch_time = Time.gm(2007, 5).to_i
t = Time.at(epoch_time)

The second code returned:
2007-05-01 08:00:00 +0800

Can anybody tell me why the "hour" value is 8?
I thought the default value for "hour" would be zeroes, as in this code:
Time.local(2010)
=> 2010-01-01 00:00:00 +0800

Thanks!

···

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

I think I get it now. I typed Time.local(2010,10,16,15,50,33)
and IRB returned with values of year, month, day, hour, min, and sec in
that order, with the values of the arguments passed to 'local'.

But the code sample from the book I'm reading has the msec argument.
Time.local(year, month, day, hour, min, sec, msec)
I added another argument for the msec position but IRB returned with the
same result as the previous, as if I didn't add the msec argument in
there. Why is that?

Sorry, beginner here and no background in programming whatsoever.

···

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

The +0800 at the end of the output is a good clue. Time.at interprets
the given seconds as local time, and you are 8 hours ahead of GMT.

Regards,
Ammar

···

On Sat, Oct 16, 2010 at 2:40 PM, Kaye Ng <sbstn26@yahoo.com> wrote:

One more thing.
I typed this in IRB:

epoch_time = Time.gm(2007, 5).to_i
t = Time.at(epoch_time)

The second code returned:
2007-05-01 08:00:00 +0800

Can anybody tell me why the "hour" value is 8?
I thought the default value for "hour" would be zeroes, as in this code:
Time.local(2010)
=> 2010-01-01 00:00:00 +0800

Hi,

well that is the syntax for using the method 'local'. The syntax mentioned
in the book is showing you how to use the method and what the arguments you
are supposed to pass to it in order to achieve what you have to. In place of
the arguments you have to put in real values when you are actually using the
method. You could assign those values to variables and then pass those
variables to the method. When you just type that in IRB, when it interprets
the call, it tries to resolve the values like 'year', 'month' etc. Since
they have not been defined, it outputs the error that the local variable
hasn't been defined.

in IRB if you did :

year = '2009'
month = '01'

.
.
.

Time.local(year, month....)

then it will work as you'd expect it to again.

PS : If you do not have programming background, I suggest you go through a
book targetting beginners like yourself and get a hold of the basics of ruby
programming.

···

--
Thanks & Regards,
Dhruva Sagar.

On Sat, Oct 16, 2010 at 13:41, Kaye Ng <sbstn26@yahoo.com> wrote:

I think I get it now. I typed Time.local(2010,10,16,15,50,33)
and IRB returned with values of year, month, day, hour, min, and sec in
that order, with the values of the arguments passed to 'local'.

But the code sample from the book I'm reading has the msec argument.
Time.local(year, month, day, hour, min, sec, msec)
I added another argument for the msec position but IRB returned with the
same result as the previous, as if I didn't add the msec argument in
there. Why is that?

Sorry, beginner here and no background in programming whatsoever.

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

The book I'm reading IS for beginners, and it's really good, I'm just a
slow learner. Anyway, you didn't answer my question about the msec
argument. It makes no difference in my IRB if I typed the msec argument
or not.
=)

···

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

Anyway, you didn't answer my question about the msec
argument. It makes no difference in my IRB if I typed the msec argument
or not.

It does make a difference, but the output does not show it. Try this in irb:

t = Time.local 2010, 1, 1, 12, 30, 0, 444

=> 2010-01-01 12:30:00 +0200

t.usec

=> 444

Regards,
Ammar

···

On Sat, Oct 16, 2010 at 11:40 AM, Kaye Ng <sbstn26@yahoo.com> wrote:

Thank you guys.

···

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