Reputation: 1170
Ok so I have a really fascinating meta programming issue here.
I'm trying to check what the class of an object is in ruby so I have a method that looks like this:
attri = "created_at"
def convert_value_dataype(object)
klass = object.send(attri.to_sym).class.to_s
case klass
when "String"
return get_attri(object)
when "NilClass"
return nil
when "Fixnum"
return get_attri(object).to_i
when "ActiveSupport::TimeWithZone" || "DateTime"
return DateTime.parse(get_attri(object))
when "TrueClass"
return true
when "FalseClass"
return false
when "Float"
return get_attri(object).to_f
else
raise "Unkown dataype: #{klass}"
end
end
Now the interesting thing is that this works when the attribute I'm looking at is a Fixnum or something simple, eg. attri = "id" but I get this error when I run the code: TypeError: no implicit conversion of ActiveSupport::TimeWithZone into String.
However when I run this in the console I can type user.send("created_at".to_sym).class.to_s and I get the correct output. Is there something special that gets imported when I'm using console (pry) that isn't being imported when I'm running the app?
I can also put 'binding.pry' right above the line "klass = ..." and call that exact line and it works in the pry console, but as soon as I exit the pry console it errors.
Upvotes: 1
Views: 2851
Reputation: 1170
Nevermind, I figured out my issue. The error is occuring in the line return DateTime.parse(get_attri(object))
because I'm trying to parse an ActiveSupport::TimeWithZone object into a DateTime.
Upvotes: 1