Is this an executable code?

K

Kaye Ng

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
 
D

Dhruva Sagar

[Note: parts of this message were removed to make it a legal post.]

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.
 
K

Kaye Ng

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.
 
D

Dhruva Sagar

[Note: parts of this message were removed to make it a legal post.]

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 :

irb> year = '2009'
irb> month = '01'
 
K

Kaye Ng

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.
=)
 
A

Ammar Ali

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

It does make a difference, but the output does not show it. Try this in irb=
:
=3D> 444

Regards,
Ammar
 
K

Kaye Ng

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!
 
A

Ammar Ali

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top