我有一个哈希(真正的要大得多)
parsed = {"follower_count" => 500, "something_else" => "etc", "xyz" => "abc"}
和一个看起来像这样的班级
class Company
attr_accessor :followers
def initialize(thehash)
@followers = thehash['follower_count']
end
end
所以最后有这段代码会在我担心其他任何问题之前抛出错误
>> parsed.map {|t| Company.new(t)}
TypeError: can't convert String into Integer
from (irb):7:in `[]'
from (irb):7:in `initialize'
from (irb):12:in `new'
from (irb):12
from (irb):12:in `map'
from (irb):12:in `each'
from (irb):12:in `map'
from (irb):12