Module: Useful::RubyExtensions::Integer
- Included in:
- Integer
- Defined in:
- lib/useful/ruby_extensions/integer.rb
Defined Under Namespace
Modules: FromActivesupport
Class Method Summary collapse
Instance Method Summary collapse
-
#pad(length = 3, pad_num = 0) ⇒ Object
returns a string reprensentation of the number padded with pad_num to a specified length.
-
#to_nearest_value(values = []) ⇒ Object
return the value in values that is nearest to the number.
-
#to_time ⇒ Object
(also: #to_time_at)
return a Time object for the given Integer.
Class Method Details
.included(receiver) ⇒ Object
59 60 61 |
# File 'lib/useful/ruby_extensions/integer.rb', line 59 def self.included(receiver) receiver.send :include, FromActivesupport end |
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
7 8 9 |
# File 'lib/useful/ruby_extensions/integer.rb', line 7 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
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/useful/ruby_extensions/integer.rb', line 12 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 |
#to_time ⇒ Object Also known as: to_time_at
return a Time object for the given Integer
26 27 28 |
# File 'lib/useful/ruby_extensions/integer.rb', line 26 def to_time Time.at(self) end |