On Sat, Mar 30, 2013 at 6:55 PM, Dave Aronson <rubytalk2dave / davearonson.com
> wrote:

> I recently made available the slides for a presentation I did
> recently, on Ruby Gotchas, at:
>
>
> https://docs.google.com/presentation/d/1cqdp89_kolr4q1YAQaB-6i5GXip8MHyve8MvQ_1r6_s
>
> Other than the title, intro, and wrapup, there are 20 slides of
> gotchas, from beginner (string interpolation requires double quotes)
> to advanced (throw/catch vs. raise/rescue).  I'm thinking of turning
> it into a series of very brief screencasts.  Check it out, let me know
> what you think... and if you have any more gotchas you'd like me to
> cover.
>
> Thanks,
> Dave
>
>
The double vs single quotes doesn't seem like a gotcha to me.

The constants one is good.

The freezing one is good, but should be separated from the constants, as
it's really just about variables. You can update the variable, but not
modify the object, so you have to know which you're doing. Might also
mention that it is for just that one object, so freezing an array doesn't
freeze the elements in the array.

the and vs && / or vs || are good

The method args are good (I see people tripped up on that moderately
frequently.

Balanced whitespace is good (better example would be `one -1`)

Naked variable is IMO, the most relevant, as it still bites me when I'm not
paying attention. Though I'm never intending to set the ivar, I always
meant to invoke the setter. I'm pretty sure that's the real case where this
bites you.

Class variables are also a good one, I just flat out don't use them.

The parent initialize one doesn't seem legitimate to me, that's how
inheritance works, why would you expect anything else?.

The example for local var in the block seems too contrived. It's only ever
hit me when I tried to do something like `.each{ |i| sum ||= 0; sum += i }`
in the loop, in which case it always gets set to zero.

The idea credit seems out of place, the hash block setting the value has
been a common pattern for a long time. Frankly, I dislike it because it (a)
sets keys on access, regardless of whether or not they will be used (e.g.
you could be carrying around thousands of empty arrays), and (b) changes
behaviour of hash such that you must use #.has_key? to see if the key is
set, because otherwise, the key is always set. You can't reliably use
things like #size. It just seems too clever to me. Really, though, I think
the Array version of this is a better example, because it's reasonable to
want to construct an array of objects with something like `Array.new(10,
[])` and not have a good enough understanding of the object model to see
why this can't work.

I'd maybe add lambda vs proc:
lambda { }.call(1)
Proc.new {}.call(1)
lambda { return 1 }.call
Proc.new { return 1 }.call

Might also add method_missing, which is evil (I don't think eval is evil,
but using method_missing on code that anyone else uses is sadistic)

Might also add floating point number imprecision.
$ ruby -e 'p 7.01 - 7'
0.009999999999999787

Might also mention when a block gets instance evaled, but you're calling
methods on your local context (e.g. a dsl you call from rspec can't call a
let).

Might also add nonprinting characters, I've had numerous times where I
copied code (e.g. Textmate output) and the whitespace came in as
nonprinting characters (probably nonbreaking space), and messed up the
file, but I couldn't see it. One cool thing about github is they highlight
these.


I'd also put the most relevant ones earlier, when people will be paying the
most attention.