obj = [obj] unless obj.type_of?(Array)
Integer() was another head-slapper that I can't believe I didn't know. And the series is full of them.Thanks very much to the Author(s)!
TypeError: can't convert nil into Author obj = [obj] unless obj.type_of?(Array)
Integer() was another head-slapper that I can't believe I didn't know. And the series is full of them.Thanks very much to the Author(s)!
TypeError: can't convert nil into AuthorOther Enumerables might make for unpleasant surprise too.
Old mootools had $A method (called Array.from now) which did exactly what you expected.
He's one of those quiet geniuses that we feel exceptionally lucky to have as part of our team.
def is_int?(x)
Integer(x); true; rescue; false
end
Which will let you know if some input passed can be safely converted to an integer.- is_int? is rather misleading (I would not expect is_int?(1.2) to be true)
- Since Ruby ints are truthy, the idiomatic form would probably just return Integer(x) and false or nil otherwise:
def is_int?(x)
Integer(x) rescue false
end resources :photos, only: [:index, :show]
resources :photos, only: :show
For those interested, this is the relevant code: https://github.com/rails/rails/blob/master/actionpack/lib/ac... obj = [obj].flattenAnd I agree...I've been programming in Ruby for awhile and only lately had learned about the `Array()` conversion, which I learned through Avdi Grimm's "Confident Ruby" book (he blogged about Array several years ago: http://devblog.avdi.org/2009/10/21/array-ifying-values/).
Such a small thing, but as you note, can greatly impact the cruft in your error-checking interfaces.