# Is this an executable code?

**URL:** <https://www.ruby-forum.com/t/is-this-an-executable-code/197572>\
**Category:** Ruby\
**Created:** [October 16, 2010, 8:23am UTC](https://www.ruby-forum.com/t/is-this-an-executable-code/197572 "2010-10-16T08:23:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [October 16, 2010, 8:23am UTC](https://www.ruby-forum.com/t/is-this-an-executable-code/197572/1 "2010-10-16T08:23:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marlon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@marlon](https://www.ruby-forum.com/u/marlon)\
**Post date:** [October 16, 2010, 8:29am UTC](https://www.ruby-forum.com/t/is-this-an-executable-code/197572/2 "2010-10-16T08:29:48Z")

</div>

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 S…

---

<div class="post-metadata">

**Author:** ![marlon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@marlon](https://www.ruby-forum.com/u/marlon)\
**Post date:** [October 16, 2010, 10:11am UTC](https://www.ruby-forum.com/t/is-this-an-executable-code/197572/3 "2010-10-16T10:11:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![marlon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@marlon](https://www.ruby-forum.com/u/marlon)\
**Post date:** [October 16, 2010, 10:40am UTC](https://www.ruby-forum.com/t/is-this-an-executable-code/197572/4 "2010-10-16T10:40:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marlon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@marlon](https://www.ruby-forum.com/u/marlon)\
**Post date:** [October 16, 2010, 10:34am UTC](https://www.ruby-forum.com/t/is-this-an-executable-code/197572/5 "2010-10-16T10:34:02Z")

</div>

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’  
.  
.  
.  
irb\> 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 S…

---

<div class="post-metadata">

**Author:** ![marlon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@marlon](https://www.ruby-forum.com/u/marlon)\
**Post date:** [October 16, 2010, 1:28pm UTC](https://www.ruby-forum.com/t/is-this-an-executable-code/197572/6 "2010-10-16T13:28:43Z")

</div>

Thank you guys.

---

<div class="post-metadata">

**Author:** ![marlon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@marlon](https://www.ruby-forum.com/u/marlon)\
**Post date:** [October 16, 2010, 10:55am UTC](https://www.ruby-forum.com/t/is-this-an-executable-code/197572/7 "2010-10-16T10:55:26Z")

</div>

On Sat, Oct 16, 2010 at 11:40 AM, Kaye Ng [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![marlon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@marlon](https://www.ruby-forum.com/u/marlon)\
**Post date:** [October 16, 2010, 1:40pm UTC](https://www.ruby-forum.com/t/is-this-an-executable-code/197572/8 "2010-10-16T13:40:27Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![marlon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@marlon](https://www.ruby-forum.com/u/marlon)\
**Post date:** [October 16, 2010, 1:54pm UTC](https://www.ruby-forum.com/t/is-this-an-executable-code/197572/9 "2010-10-16T13:54:34Z")

</div>

On Sat, Oct 16, 2010 at 2:40 PM, Kaye Ng [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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
