Class: Integer

Inherits:
Object show all
Defined in:
lib/conversion/class_decoration.rb

Class Method Summary collapse

Class Method Details

.to_converter_procObject

Returns a Proc that converts to Integer

Integer.to_converter_proc.call(1) # => 1
Integer.to_converter_proc.call(1.9) # => 2
Integer.to_converter_proc.call('1.9') # => 2
Integer.to_converter_proc.call('abc') # ArgumentError: invalid value for Float(): "abc"

See Conversion.converter, Object#convert_to



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/conversion/class_decoration.rb', line 54

def to_converter_proc
  proc do |value|
    if value.is_a?(Float)
      value.round
    else
      begin
        Integer(value)
      rescue Exception
        Float(value).round
      end
    end
  end
end