5 ms·
Ruby Tips Part 4
- enko 13y agoThis 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
- Peroni 13y agoThe author is my colleague Mat - https://news.ycombinator.com/user?id=matsadler 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.
- danso 13y agoBTW, enko is referring to Part 2: http://globaldev.co.uk/2013/09/ruby-tips-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/ 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.
- MattBearman 13y agoI too was unaware of Array(), what an awesome little function! Definitely gonna read this series from the beginning.
- xentronium 13y agoBe 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 13y agoIf 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 13y agoThis is exactly what I meant! Thank you!
- sjtgraham 13y agoMost 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.
- ollysb 13y agoI've always seen the idiom [*array_or_object] used to do that. Array() is definitely clearer though.
- deleted 13y ago[deleted]
- Aaronneyer 13y agoInteger() 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.
- pmh 13y agoA 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
- maurycy 13y agos/is_int/intable/ ;-)
- Aaronneyer 13y agoAh 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.
- glitchdout 13y agoDoing 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/action_dispatch/routing/mapper.rb#L979 https://github.com/rails/rails/blob/master/actionpack/lib/ac...
- reyan 13y agoAnother 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 http://majesticseacreature.com/rbp-book/pdfs/rbp_1-0.pdf).
- twerquie 13y agoThis works well for us: obj = [obj].flatten
- JonnieCache 13y agoA 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.
- dopamean 13y agoI just recently started using pry and it has already made a big difference. Couldn't recommend it more.
- projct 13y agoSee also https://github.com/nixme/jazz_hands https://github.com/nixme/jazz_hands for a collection of these in one. :)
- rubyn00bie 13y agoGlad 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)