Module: Useful::RubyExtensions::Fixnum

Included in:
Fixnum
Defined in:
lib/useful/ruby_extensions/fixnum.rb

Instance Method Summary collapse

Instance Method Details

#pad(length = 3, pad_num = 0) ⇒ Object

returns a string reprensentation of the number padded with pad_num to a specified length



6
7
8
# File 'lib/useful/ruby_extensions/fixnum.rb', line 6

def pad(length = 3, pad_num = 0)
  self.to_s.rjust(length,pad_num.to_s) rescue self.to_s
end

#to_nearest_value(values = []) ⇒ Object

return the value in values that is nearest to the number



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/useful/ruby_extensions/fixnum.rb', line 11

def to_nearest_value(values = [])
  return self if values.length == 0
  value = values.first.to_i rescue self
  diff = (self-value).abs
  values.each do |val|
    if (self-val.to_i).abs < diff
      diff = (self-val.to_i).abs
      value = val.to_i
    end
  end
  value
end