Class: FloatHash

Inherits:
Hash show all
Defined in:
lib/rubyhacks.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#+, #expand_bools, #modify, #random, #random_key, #random_value

Class Method Details

.from_hash(hash) ⇒ Object



625
626
627
628
629
# File 'lib/rubyhacks.rb', line 625

def self.from_hash(hash)
      	   fh = new
   hash.each{|k,v| fh[k]=v}
          fh
end

Instance Method Details

#[](key) ⇒ Object



651
652
653
654
655
656
657
658
659
660
661
662
# File 'lib/rubyhacks.rb', line 651

def [](key)
# # # 		super(key.to_f)
# 		raise TypeError unless key.class == Float 
	#old_key = self.find{|k, v| (k-key.to_f).abs < Float::EPSILON}
		raise TypeError unless key.kind_of? Numeric
	old_key = self.keys.inject{|o, n| ((o-key).abs < (n-key).abs) ? o : n }
	if old_key
		return super(old_key)
	else 
		return nil
	end
end

#[]=(key, var) ⇒ Object

Raises:

  • (TypeError)


640
641
642
643
644
645
646
647
648
649
# File 'lib/rubyhacks.rb', line 640

def []=(key, var)
# 		super(key.to_f, var)
		raise TypeError unless key.kind_of? Numeric
	old_key = self.find{|k, v| (k-key.to_f).abs < Float::EPSILON}
	if old_key
		super(old_key[0].to_f, var)
	else 
		super(key.to_f, var)
	end
end

#inspectObject



632
633
634
# File 'lib/rubyhacks.rb', line 632

def inspect
	   "FloatHash.from_hash(#{old_inspect})"
end

#pretty_inspectObject



636
637
638
# File 'lib/rubyhacks.rb', line 636

def pretty_inspect
	"FloatHash.from_hash(#{old_pretty_inspect})"
end