Ruby 新手。这段代码有什么问题?
city_details['longitude'] + "," + city_details['latitude']
我收到这个错误:
./player_location.rb:6:in `+': String can't be coerced into Float (TypeError)
最佳答案
看起来 city_details['longitude']
和 city_details['latitude']
是 Float
值。
您不能像这样在 Ruby 中将 Float
添加到 String
。您可以将所有内容转换为 String
,然后 +
它们,或者使用 String 插值。
city_details['longitude'].to_s + "," + city_details['latitude'].to_s
"#{city_details['longitude']},#{city_details['latitude']}"
大多数 Rubyist 倾向于使用字符串插值。
关于Ruby float 到字符串错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6397204/