Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ruby Tips Part 4 (globaldev.co.uk)
61 points by Peroni on Oct 14, 2013 | hide | past | favorite | 20 comments


This series is invaluable. I have been programming in Ruby professionally for over 7 years and did not know about Array()! Since then I have been joyfully combing through my repos and eliminating literally dozens of

  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


Be careful with Array() since it uses #to_a method whenever it exists. As such, this trick is unsuitable for array of hashes (in any version of ruby) and strings (in ruby 1.8).

Other Enumerables might make for unpleasant surprise too.

Old mootools had $A method (called Array.from now) which did exactly what you expected.


If you already use ActiveSupport, then you can use Array#wrap http://api.rubyonrails.org/classes/Array.html#method-c-wrap


This is exactly what I meant! Thank you!


BTW, enko is referring to Part 2: http://globaldev.co.uk/2013/09/ruby-tips-part-2/

And 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.


The author is my colleague Mat - https://news.ycombinator.com/user?id=matsadler

He's one of those quiet geniuses that we feel exceptionally lucky to have as part of our team.


Most if not all of this is in the language documentation. It's the best source, along with the actual implementation for learning this stuff. A good way to learn is to try to read docs for one module a day.


Integer() or Float() let you do cool things like this:

    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.


A couple of minor nits:

- 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


Ah you're right. Hadn't actually tested this. The way that I've actually used it is for `is_num?` and using `Float()`. I didn't consider the fact that `Integer` would cast a float.


s/is_int/intable/ ;-)


Doing open source, you learn little nuggets like this. I learned about `Array()` when checking out how Rails routing allowed you to pass a symbol or an array, like:

    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...


I too was unaware of Array(), what an awesome little function! Definitely gonna read this series from the beginning.


Another invaluable resource is 'Ruby Best Practices' by Gregory Brown. You can download the pdf for free (http://majesticseacreature.com/rbp-book/pdfs/rbp_1-0.pdf).


I've always seen the idiom [*array_or_object] used to do that. Array() is definitely clearer though.


This works well for us:

   obj = [obj].flatten


A lot of that stuff comes with the dynamite combination of pry and its various plugins: pry-debugger (gives you next, step etc), pry-stack_explorer (go up and down the call stack) and so on.

I implore you to check them out if you are a ruby developer. I can't remember how I got work done without them.

http://pryrepl.org/

https://github.com/pry/pry/wiki

PS. to make your life even easier, hook it up to awesome_print. Heaven.


Glad to see someone mentioned these!

Making your own irb console is dead easy:

myfile.rb

require 'pry' class Stuff def initialize binding.pry end end

Stuff.new # diy console

Also, awesome_print is a lifesaver as well...

One tip I do have is: be careful when "prying" you don't want to accidentally ship a "binding.pry" into production (*I did it to our staging server once-- both hilarious and stupid)


I just recently started using pry and it has already made a big difference. Couldn't recommend it more.


See also https://github.com/nixme/jazz_hands for a collection of these in one. :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: