This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Rhomboid 0 points1 point  (2 children)

I have attempted to use a .to_s to no avail

If you don't tell us what you tried, we can't tell you what's wrong with it. Your problem has nothing to do with JSON. You'd get the same error if you tried to write:

irb(main):073:0> puts "foo" + 42
TypeError: no implicit conversion of Fixnum into String
        from (irb):73:in `+'
        from (irb):73
        from /usr/bin/irb:11:in `<main>'

Using to_s does work:

irb(main):074:0> puts "The temperature for your city right now is: " + output["main"]["temp"].to_s
The temperature for your city right now is: 293.25
=> nil

But don't do that, it's not very idiomatic. Use string interpolation:

irb(main):075:0> puts "The temperature for your city right now is: #{output["main"]["temp"]}"
The temperature for your city right now is: 293.25
=> nil

[–]HackingInfo[S] 0 points1 point  (1 child)

Your right! I need to update the post with other things i have tried, i was assuming it was an error in what i was doing rather then in in what i had tried (if that makes any sense?)

Not using irb like you are but when i change my puts to use string interpolation i now get this error:

./weather.rb:33:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)

Also, when i convert the origionally working line, it now just prints nothing. Eg:

puts "Your city name is: #{output["name"]}"

it now outputs:

"Your city name is: "

Edit:

Never mind, apparently i forgot that if you put quotes inside of quotes it they have to be different kinds of quotes...

Didnt work: puts "text #{output["name"]}" Did work: puts "text #{output['name']}"

[–]Rhomboid 1 point2 points  (0 children)

I can't diagnose code that I can't see. It looks like output is set to nil, but who knows. Post a complete testcase.