为什么是_a? Hash 类返回 false?

发布于 2024-12-22 10:36:19 字数 1279 浏览 2 评论 0原文

为什么 is_a?Hash 类返回 false

示例:

value = {"x" => 3, "y" => 2}

puts value.class
puts value.is_a?(Hash)

输出:

Hash
false

我使用 Ruby 1.9.2

更新:我的课程的完整源代码:

class LatLng
  include Mongoid::Fields::Serializable

  attr_reader :lat, :lng

  def serialize(value)
    return if value.nil?

    puts value.class
    puts value.is_a?(Hash)

    if value.is_a?(self.class)
      puts "is geopoint" + value.to_json
      {'lng' => value.lng.to_f, 'lat' => value.lat.to_f}
    elsif value.is_a?(Hash)
      hash = value.with_indifferent_access
      puts "is hash" + value.to_json
      {'lng' => hash['lng'].to_f, 'lat' => hash['lat'].to_f}
    end
  end

  def deserialize(value)
    return if value.nil?

    value.is_a?(self.class) ? value : LatLng.new(value['lat'], value['lng'])
  end

  def initialize(lat, lng)
    @lat, @lng = lat.to_f, lng.to_f
  end

  def [](arg)
    case arg
      when "lat"
        @lat
      when "lng"
        @lng
    end
  end

  def to_a
    [lng, lat]
  end

  def ==(other)
    other.is_a?(self.class) && other.lat == lat && other.lng == lng
  end
end

Why is_a? returns false for a Hash class?

Example:

value = {"x" => 3, "y" => 2}

puts value.class
puts value.is_a?(Hash)

Output:

Hash
false

Im using Ruby 1.9.2

UPDATED: full source of my class:

class LatLng
  include Mongoid::Fields::Serializable

  attr_reader :lat, :lng

  def serialize(value)
    return if value.nil?

    puts value.class
    puts value.is_a?(Hash)

    if value.is_a?(self.class)
      puts "is geopoint" + value.to_json
      {'lng' => value.lng.to_f, 'lat' => value.lat.to_f}
    elsif value.is_a?(Hash)
      hash = value.with_indifferent_access
      puts "is hash" + value.to_json
      {'lng' => hash['lng'].to_f, 'lat' => hash['lat'].to_f}
    end
  end

  def deserialize(value)
    return if value.nil?

    value.is_a?(self.class) ? value : LatLng.new(value['lat'], value['lng'])
  end

  def initialize(lat, lng)
    @lat, @lng = lat.to_f, lng.to_f
  end

  def [](arg)
    case arg
      when "lat"
        @lat
      when "lng"
        @lng
    end
  end

  def to_a
    [lng, lat]
  end

  def ==(other)
    other.is_a?(self.class) && other.lat == lat && other.lng == lng
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎夕旧梦 2024-12-29 10:36:19
#irb
ruby-1.9.3-p0 :001 > value = {"x" => 3, "y" => 2}
 => {"x"=>3, "y"=>2} 
ruby-1.9.3-p0 :002 > value.is_a?(Hash)
 => true

尝试禁用您已加载的任何gems/扩展,并尝试使用干净的ruby

更新:

尝试value.is_a?(::Hash)

PS: 尝试阅读有关 Ruby 中的 Duck Typing 的内容。也许您应该调用 value.respond_to?(:key) 而不是 value.is_a?(Hash)

#irb
ruby-1.9.3-p0 :001 > value = {"x" => 3, "y" => 2}
 => {"x"=>3, "y"=>2} 
ruby-1.9.3-p0 :002 > value.is_a?(Hash)
 => true

try to disable any gems/extensions you have loaded, and try with clean ruby

UPDATE:

try value.is_a?(::Hash)

PS: try to read about Duck Typing in Ruby. Maybe you should call value.respond_to?(:key) instead of value.is_a?(Hash)

注定孤独终老 2024-12-29 10:36:19

当我在 Hash 类之前添加“::”时,它开始工作。

puts value.class
puts value.is_a?(::Hash)

输出:

Hash
true

When I added "::" before Hash class it starts working.

puts value.class
puts value.is_a?(::Hash)

Output:

Hash
true
梦忆晨望 2024-12-29 10:36:19

事实并非如此。

dave@hpubuntu1:~ $ rvm list

rvm rubies

   ruby-1.8.7-p334 [ i386 ]
   jruby-1.6.2 [ linux-i386-java ]
   ruby-1.9.2-p0 [ i386 ]
   ruby-1.9.2-p290 [ i386 ]
   ruby-1.9.3-p0 [ i386 ]
=> ruby-1.9.2-p180 [ i386 ]

dave@hpubuntu1:~ $ pry
pry(main)> value = {"x" => 3, "y" => 2}
=> {"x"=>3, "y"=>2}
pry(main)> value.is_a? Hash
=> true

Mongoid Hash 不是纯 Ruby Hash,也不会扩展它。您应该检查实际类型,可能使用 输入

仅仅因为某些内容打印出 Hash 并不意味着 (a) 它是您认为的继承链的一部分,以及 (b) 它是 Hash (见证 ActiveRecord Array,在某种程度上是谎言)。

It doesn't.

dave@hpubuntu1:~ $ rvm list

rvm rubies

   ruby-1.8.7-p334 [ i386 ]
   jruby-1.6.2 [ linux-i386-java ]
   ruby-1.9.2-p0 [ i386 ]
   ruby-1.9.2-p290 [ i386 ]
   ruby-1.9.3-p0 [ i386 ]
=> ruby-1.9.2-p180 [ i386 ]

dave@hpubuntu1:~ $ pry
pry(main)> value = {"x" => 3, "y" => 2}
=> {"x"=>3, "y"=>2}
pry(main)> value.is_a? Hash
=> true

A Mongoid Hash isn't a pure-Ruby Hash, nor does it extend it. You should check the actual type, probably by using type.

Just because something prints out Hash doesn't mean (a) that it's part of the inheritance chain you think it is, and (b) that it's a Hash (witness ActiveRecord Array, which lies, to a degree).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文