Neden is_a?
, Hash
sınıfı için false
döndürür?Neden isa? Hash sınıfı için false döndürür?
Örnek:
value = {"x" => 3, "y" => 2}
puts value.class
puts value.is_a?(Hash)
Çıktı: Ruby 1.9.2
GÜNCELLEME kullanılarak
Hash
false
Im: Benim sınıfının tam kaynak:
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
Her şey yakut 1.9.2p290 (2011-07-09) (i gerçek değer var) benim için – cristian
eserler üzerinde ok ... – Chowlett
Ayrıca burada çalışıyor. Hash.new.is_a? (Hash) 'ın da false döndürüp döndürmediğini kontrol edebilir misiniz? Bu oldukça tuhaf olurdu. –