
Ruby Tips Part 4 - Peroni
http://globaldev.co.uk/2013/10/ruby-tips-part-4/
======
enko
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

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

~~~
byroot
If you already use ActiveSupport, then you can use Array#wrap
[http://api.rubyonrails.org/classes/Array.html#method-c-
wrap](http://api.rubyonrails.org/classes/Array.html#method-c-wrap)

~~~
xentronium
This is exactly what I meant! Thank you!

------
JonnieCache
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/](http://pryrepl.org/)

[https://github.com/pry/pry/wiki](https://github.com/pry/pry/wiki)

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

~~~
rubyn00bie
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)

