
Ruby Tips Part 2 - Peroni
http://globaldev.co.uk/2013/09/ruby-tips-part-2/
======
matthewmacleod
Some interesting and useful syntax here! However...

 _I’m also partial to this trick for grabbing just the first element of an
array._

    
    
      family, = socket.peeraddr
    

This is the sort of code that really turns me off when I'm digging through
Ruby. It adds no clarity and actually looks like a typo. What's wrong with
this?

    
    
      family = socket.peeraddr.first
    

It's much clearer what's going on.

~~~
matsadler
(context: I'm the author)

I do agree with you, but through some failing of my own I still quite enjoy
that trick. I included it to highlight the syntax, but I'll add a note saying
it's maybe not the best idea to actually use it.

~~~
garethadams
I often use it when I'm running through code in a console.

I put it in production code once, but removed it when I realised the "# This
comma is important" comment was longer than just using `.first`

------
daviddavis
You have to be careful with using Array() to transform a non-array into an
array. Suppose for example you wanted an array of hashes:

    
    
      >> Array([{:a => "b"}])
      => [{:a=>"b"}]
      >> Array({:a => "b"})
      => [[:a, "b"]]
    

It doesn't work as you might expect with a Hash. I'd probably recommend
sticking with something like either of these unless you know you aren't going
to be getting a Hash:

    
    
      a = [a] unless a.is_a?(Array) # option 1
      a = [*a]                      # option 2

~~~
molf
Or `Array.wrap` if you're using Rails.

------
losvedir
Great article. One question:

> There’s one final use for the splat ( * ); when overriding methods in a
> subclass and calling super. If it turns out you don’t need the arguments to
> the method for whatever additional behaviour you’re adding, you can accept
> all arguments with a bare splat, then a bare super passes all arguments to
> super.

I thought that was the default behavior of super. Isn't it the case that a
bare super by default passes all the method's original arguments? I know this
has bit me in the past where I've been forced to call `super()` with empty
parentheses explicitly to prevent this behavior.

~~~
matsadler
You're correct, that is the default behaviour of super.

My intention wasn't to say that super only works like that when you've defined
the method like `def foo( * )` but rather `def foo( * )` is an alternative to
having to name your arguments when you're not even going to use them as they
are automatically passed with bare super.

~~~
losvedir
Ah, yes, of course. I understand now. Skimmed a bit too fast there and
misunderstood what you were getting at. Thanks!

------
sheldor
I think it would be neat to have a single place for all up-to-date
tips/tutorials/techniques regarding Ruby & RoR (like Superhero.js)

Excellent resource BTW. Keep them coming.

~~~
kot-behemoth
There's [http://pineapple.io/tags/ruby-on-
rails](http://pineapple.io/tags/ruby-on-rails) which is getting updated quite
regularly!

------
ultimoo
Nice write-up about some nifty tricks! Ruby tricks blogs are dime a dozen but
I really enjoyed this post as well as Part 1 which was posted here few weeks
ago. Looking forward to the rest of the series!

------
dayyan
Some of these "tricks" contradict the Ruby language philosophy.

~~~
timblair
Care to give some examples where you think this is the case?

