It has been a while since the release of Ruby 2.0, and a lot of comprehensive guides to the new 2.0 features were written. However, I feel that some of them are still left in obscurity. The justice shall be served.
All constants now support ||= and &&=
This one is pretty sad. In Ruby 1.8 and 1.9, constants behaved just like variables… yes, I just said that. Sigh. Also, not all of them:
1 2 3 4 5 6 7 8 |
|
Note that in the last case, the constant was not even defined before.
However, Ruby 2.0 allows us to use them freely:
1 2 |
|
Please, do not use this, ever, in your code.
Better disambiguation of symbols and hash labels
In Ruby 1.9, the following code has a syntactic error:
1 2 3 4 |
|
However, Ruby 2.0 parses it:
1 2 3 |
|
Please, do not take advantage of this in your code.
BEGIN{} now displays proper error message when used within a method body
Ruby 1.8:
1 2 3 4 |
|
Ruby 1.9:
1 2 3 4 |
|
Ruby 2.0:
1 2 3 |
|
Please, do not write code that depends on this, or uses BEGIN
at all. (… do you notice a pattern?)
I have no idea what to write in this heading
Ruby 1.9:
1 2 3 4 |
|
Ruby 2.0:
1 2 |
|
If you want to use this, you should consult your assigned therapist. What do you mean you don’t have one? Well, you will need assistance anyway after you finish reading this post.
Shadowing block variable declarations can now span multiple lines
Ruby 1.9:
1 2 3 |
|
Ruby 2.0:
1 2 3 |
|
This one is pretty nice actually. I needed it yesterday; what a nice way to justify installing 2.0.0-p0 on production to our ops team!
4220
Um… if you get this and do it like that, and then pick a square peg and push it through a hexagonal hole… or actually it’s all the other way around, but it doesn’t matter much?
Ruby 1.9:
1 2 3 |
|
Ruby 2.0:
1 2 3 |
|
But why?!
What is this and do you Evan? I mean, do you break even? Break something?.. Get it? I neither.
A question I’m not going to answer. However, if you’d ask how… lo and behold, thou shalt useth a mighty diff.
Also, who is Evan? You tell me!