I love Ruby’s smalltalk features. I really do.
I often wish that Java had some of them.
And the pattern-based string processing is
cool.
But…
Why magic variables?
They’re great when you are intimately familiar
with the internal operation of the system. But
miserable when you’re reading code without
having acquired that understanding. And even if
their use is discouraged, that won’t prevent them
from appearing when I inadvertently code something
that uses them.
Dynamic allocation – Is that a good thing?
I love things like the ability to add functionality to
an existing class, like String. But I have also
spent hours reading right past a small typo that
caused me to assign to a new variable that was
supposed to be an existing variable, or adding a
number to a string when the operation made no
sense at all. (Ah, Forth…)
One of the things I love about Java is that it finds
so many errors at compile time. (On the other hand,
I love unit testing, so maybe Ruby really needs a
unit-test framework generator, to find such problems,
along with the many other bugs lurking in my code.)
3.times.
How many times does this execute, really?
I saw one page that said it cycled through
indices 0,1,2,3. Hopefully, that was an error!
(If not, this item definitely belongs on the list
of things every newcomer needs to know!)
3.times.
How many times does this execute, really?
3:
$ ruby -e ‘3.times {|x| p x}’
0
1
2
I saw one page that said it cycled through
indices 0,1,2,3. Hopefully, that was an error!
Can you find that page? I wonder if there was confusion because
in irb you see a ‘3’ at the end of the list (which is actually the
return value of the call to #times).
3.times.
How many times does this execute, really?
I saw one page that said it cycled through
indices 0,1,2,3. Hopefully, that was an error!
(If not, this item definitely belongs on the list
of things every newcomer needs to know!)
Can you find that page? I wonder if there was confusion because
in irb you see a ‘3’ at the end of the list (which is actually the
return value of the call to #times).
Lo and behold. That’s the source of the confusion!
3.times.
How many times does this execute, really?
I saw one page that said it cycled through
indices 0,1,2,3. Hopefully, that was an error!
(If not, this item definitely belongs on the list
of things every newcomer needs to know!)
I saw one page that said it cycled through
indices 0,1,2,3. Hopefully, that was an error!
Can you find that page? I wonder if there was confusion because
in irb you see a ‘3’ at the end of the list (which is actually the
return value of the call to #times).
Ah. I’ll bet that was it. I’ll see if I can find the page.
I’ll bet it was the return value. The loop was echoing the
index, producing the result 0,1,2,3 !
Um… beneficial crufts? I think because they are already idioms.
I don’t even know the other name for $? (check return status).
They’re only idioms if you’re used to Perl.
or shell scripting. But you’re right, a ruby user shouldn’t be expected to
have prior perl or shell scripting knowledge. Then, the only people
benefited by magic variables are people like me who are too lazy to
look up the proper name for $? (Ok… I’m looking it up now, I’m
also curious as to what it is called).
This is the clearest way to explain the concept, I believe.
Let me see if I can find that page. It was in one of the
major “getting started” pages on the web…
I can’t seem to get at the cookbook right now – server
appears to be down. That’s the only place left to look.
Couldn’t seem to find it anywhere else…
Um… beneficial crufts? I think because they are already idioms.
I don’t even know the other name for $? (check return status).
They’re only idioms if you’re used to Perl.
or shell scripting. But you’re right, a ruby user shouldn’t be expected to
have prior perl or shell scripting knowledge. Then, the only people
benefited by magic variables are people like me who are too lazy to
look up the proper name for $? (Ok… I’m looking it up now, I’m
also curious as to what it is called).