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



616
617
618
619
620
# File 'lib/rubyhacks.rb', line 616

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

Instance Method Details

#[](key) ⇒ Object



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

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}
  if old_key
    return super(old_key[0])
  else 
    return nil
  end
end

#[]=(key, var) ⇒ Object



631
632
633
634
635
636
637
638
639
640
# File 'lib/rubyhacks.rb', line 631

def []=(key, var)
#     super(key.to_f, var)
#     raise TypeError unless key.kind 
  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



623
624
625
# File 'lib/rubyhacks.rb', line 623

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

#pretty_inspectObject



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

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