Skip to content

Latest commit

 

History

History
39 lines (27 loc) · 753 Bytes

README.md

File metadata and controls

39 lines (27 loc) · 753 Bytes

Try Convert

A ruby gem that adds some missing ::try_convert methods to classes where it could be useful.

Rationale

The following defensive pattern is quite common:

if obj.respond_to? :to_int
  i = obj.to_int
else
  die
end

I believe it to be somewhat of an antipattern, compared to:

i = Integer.try_convert(obj) or die

New Methods

Float.try_convert(obj)

Integer.try_convert(obj)

Proc.try_convert(obj)