NoMethodError: undefined method `zone' for Time:Class - ruby

How do i use Time.zone in ruby if I am not using rails
I want to do Time.now but that's available in rails but not ruby
I thought that
require 'time'
would fix this and make it available in ruby but it didn't and I get
NoMethodError: undefined method `zone' for Time:Class

I don't know, what do you mean. But I think it should work as below :
(arup~>~)$ pry --simple-prompt
>> Time.now
=> 2014-04-09 23:19:04 +0530
>> Time.now.strftime('%Z')
=> "IST"
>> Time.now.strftime('%z')
=> "+0530"
>> Time.now.zone
=> "IST"
Documentation : #strftime and #zone .

You've tried to use zone as if it were a class method (Time.zone) [1]. If you want to use a class method:
1.9.3-p448 :007 > Time.now.zone
=> "EDT"
But Time.now is just a nice way of instantiating your own instance of Time[2]. So you're really just doing this (calling an instance method):
1.9.3-p448 :009 > time = Time.new
=> 2014-04-09 15:14:01 -0400
1.9.3-p448 :010 > time.zone
=> "EDT"
[1] http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/
[2] http://ruby-doc.org/core-1.9.3/Time.html#method-c-now

Related

undefined method `symbolize_keys' after requiring active support.

I am trying to symbolize the keys of a hash in a non rails project. I can see the symbolize_keys method is part of Active Support so I imported the library but it still doesn't work.
Here is an example of it failing
2.4.2 :001 > require 'active_support'
=> true
2.4.2 :002 > {'test' => 'test'}.symbolize_keys
NoMethodError: undefined method `symbolize_keys' for {"test"=>"test"}:Hash
Expected output
{test: "test"}
You should require 'active_support/all' if you want active support core extensions also required:
2.3.4 :002 > require 'active_support/all'
=> true
2.3.4 :003 > {'test' => 'test'}.symbolize_keys
=> {:test=>"test"}

undefined method `zone` for Time:Class after requiring active_support/time_with_zone

I'm on Ruby 2.2.1 and have active_support 4.2 installed so I want to use
Time.zone.parse('2007-02-10 15:30:45')
as described here: http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html
But even after requiring active_support and active_support/time_with_zone, I doesn't seem like Time.zone still doesn't work. Observe:
2.2.1 :002 > require "active_support"
=> true
2.2.1 :003 > Time.zone
NoMethodError: undefined method `zone' for Time:Class
2.2.1 :004 > require "active_support/time_with_zone"
=> true
2.2.1 :005 > Time.zone
NoMethodError: undefined method `zone' for Time:Class
What's going on?
The zone class method for the Time class is actually in active_support/core_ext/time/zones. You can require that class if you really want to use Time.zone but a better approach might to require active_support/all
Suggested Solution
require "active_support/all"
If you want to check out the source code fort for active_support look at the github repo
active_support/time_with_zone provides the ActiveSupport::TimeWithZone class, but it doesn't extend the core Time class.
You can require active_support/time instead:
require 'active_support'
require 'active_support/time'
Time.zone = 'Eastern Time (US & Canada)' #=> "Eastern Time (US & Canada)"
Time.zone.parse('2007-02-10 15:30:45') #=> Sat, 10 Feb 2007 15:30:45 EST -05:00
Try
require 'active_support/all'
instead of
require "active_support"

Why is Ruby's Date class automatically loaded but DateTime is not?

Using IRB, why are the Date & Time classes automatically loaded, but DateTime is not? I have to require 'date', this does not make sense to me because I thought that both Date and DateTime were using the standard library 'date'?
ruby-1.9.2-p290 :001 > Date
=> Date
ruby-1.9.2-p290 :002 > Time
=> Time
ruby-1.9.2-p290 :003 > DateTime
NameError: uninitialized constant Object::DateTime
from (irb):3
from /Users/kamilski81/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
ruby-1.9.2-p290 :004 > require 'date'
=> true
ruby-1.9.2-p290 :005 > require 'date'
=> false
ruby-1.9.2-p290 :006 > DateTime
=> DateTime
In IRB, include this line: require 'date' then you will be able to use DateTime.
irb(main):000:0> DateTime.class
NameError: uninitialized constant DateTime
from (irb):0
from /path/to/ruby/irb:12:in '(main)'
irb(main):001:0> require 'date'
=> true
irb(main):002:0> DateTime.class
=> Class
Worked for me when first initializing with require 'date'.
Being a little more curious, I tried:
$ ruby -e 'puts DateTime.class'
-e:1:in `<main>': uninitialized constant Object::DateTime (NameError)
[~, kamilski81#mac]
$ ruby -e 'puts Date.class'
-e:1:in `<main>': uninitialized constant Object::Date (NameError)
$ ruby -e 'puts Time.class'
Class
So it makes me think that it's an irb issue that automatically loads 'date'.

Ruby Date Methods Issue

ruby-1.9.2-p290 :001 > require 'date' => true
ruby-1.9.2-p290 :002 > date = '01/23/2011'=> "01/23/2011"
ruby-1.9.2-p290 :003 > Date.strptime(date, "%m/%d/%Y") =>
#<Date: 2011-01-23 (4911169/2,0,2299161)>
ruby-1.9.2-p290 :004 > Date.mon
NoMethodError: undefined method `mon' for Date:Class
from (irb):4
from /Users/noahclark/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
ruby-1.9.2-p290 :005 >
Why does it do this? I've looked in the documentation and .mon or .month is a valid method.
Thanks in advance.
mon and month are valid methods of Date instances. You're calling them on Date - that is, a class.
I suggest you take a look at the difference between class and instance methods.
strptime belongs to the former, mon to the latter.

How do I use this mixin to alter the DateTime object?

I need to alter the DateTime object. The resource I've found states that this is what I need to do, but I'm not sure exactly what to do with this code. Where do I put it? Thanks for reading.
module DateTimePatch
def getlocal
"works!"
end
end
DateTime.send(:include, DateTimePatch)
EDIT:
So this is what I have
/lib/date_time.rb
class DateTime
def getlocal
"it works"
end
end
Console
ruby-1.9.2-p0 > Time.now.getlocal
=> 2010-11-09 23:40:36 +1000
ruby-1.9.2-p0 > DateTime.now
=> Tue, 09 Nov 2010 23:40:57 +1000
ruby-1.9.2-p0 > DateTime.now.getlocal
NoMethodError: undefined method `getlocal' for Tue, 09 Nov 2010 23:41:02 +1000:DateTime
EDIT2:
Ok so now I'm doing:
ruby-1.9.2-p0 > require './lib/date_time.rb'
=> true
ruby-1.9.2-p0 > DateTime.now.getlocal
NoMethodError: undefined method `now' for DateTime:Class
from (irb):2
from /Users/benhartney/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
ruby-1.9.2-p0 > DateTime.now
NoMethodError: undefined method `now' for DateTime:Class
from (irb):3
from /Users/benhartney/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
I tried adding a temporary now method to the mixin to see if the error still occurred and it did. DateTime.now worked fine before I loaded the mixin, not sure why this is happening?
So, you want to add a method to the DateTime class ? Try:
class DateTime
def get_local
"works!"
end
end
Probably you forgot to require your file.
irb(main):002:0> DateTime.now.getlocal
NoMethodError: undefined method `getlocal' for #
from (irb):2
from d:/Ruby/bin/irb.bat:20:in `'
irb(main):003:0> require './lib/date_time.rb'
=> true
irb(main):004:0> DateTime.now.getlocal
=> "it works"
UPD: and require 'date' in order to use ruby's DateTime class.

Resources