Lighting Rod

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…

  1. 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.

  2. 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. 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!)

Hi –

  1. 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).

David

···

On Sat, 9 Nov 2002, Eric Armstrong wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Eric Armstrong eric.armstrong@sun.com writes:

I love Ruby’s smalltalk features. I really do.

Would you explain which ones are “ruby’s smalltalk features”. Sorry,
I’m smalltalk-illiterate.

  1. Why magic variables?

Um… beneficial crufts? I think because they are already idioms.
I don’t even know the other name for $? (check return status).

YS.

Eric Armstrong said:

  1. 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!)

This one is certainly easy to test for oneself:

[root@enigo1 tutorial]# irb
irb(main):001:0> 3.times {|d| puts d}
0
1
2
3
irb(main):002:0> 2.times {|d| puts d}
0
1
2
irb(main):003:0> 0.times {|d| puts d}
0

Note that the above is done with Ruby 1.6.7

And this is certainly inconsistent with what is stated in the Pickaxe book:

“Iterates block int times, passing in values from zero to int - 1”

Kirk Haines

They’re only idioms if you’re used to Perl.

Personally, I don’t like them and avoid
using them.

Note that they hardly appear at all in The
Ruby Way
.

Hal

···

----- Original Message -----
From: “Yohanes Santoso” ysantoso@jenny-gnome.dyndns.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, November 08, 2002 7:49 PM
Subject: Re: Lighting Rod

  1. Why magic variables?

Um… beneficial crufts? I think because they are already idioms.
I don’t even know the other name for $? (check return status).

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!

Doh!

Thanks for the clarification.

Kirk Haines

Kirk Haines wrote:

This one is certainly easy to test for oneself:

[root@enigo1 tutorial]# irb
irb(main):001:0> 3.times {|d| puts d}
0
1
2
3

The last line isn’t from puts, it is from irb itself. The result of the
times() call is the value it was called on, which irb echoes.

An illustrative example:

irb(main):003:0> 3.times {|d| puts d * 10}
0
10
20
3

/Anders

···

A n d e r s B e n g t s s o n | ndrsbngtssn@yahoo.se
Stockholm, Sweden |


Gratis e-mail resten av livet på www.yahoo.se/mail
Busenkelt!

“Kirk Haines” khaines@enigo.com wrote in message
news:48674.148.78.249.10.1036859159.squirrel@enigo.com

Eric Armstrong said:
And this is certainly inconsistent with what is stated in the Pickaxe
book:

“Iterates block int times, passing in values from zero to int - 1”

Isn’t that exactly what it is doing?

irb(main):001:0> 3.times {|d| puts d}
0 <— value starting at zero
1
2 <— to int(3)-1 which would 2
3 <— this is the return value (0,1,2 = 3)

Maybe I am mistaking what you are saying.

Bob

In article 48674.148.78.249.10.1036859159.squirrel@enigo.com,

Eric Armstrong said:

  1. 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!)

This one is certainly easy to test for oneself:

[root@enigo1 tutorial]# irb
irb(main):001:0> 3.times {|d| puts d}
0
1
2
3
irb(main):002:0> 2.times {|d| puts d}
0
1
2
irb(main):003:0> 0.times {|d| puts d}
0

Note that the above is done with Ruby 1.6.7

And this is certainly inconsistent with what is stated in the Pickaxe book:

“Iterates block int times, passing in values from zero to int - 1”

Well, no it’s not inconsistent. The last number you’re seeing is returned
from the block and irb prints it.

Try this:

irb(main):002:0> 3.times { |i| puts “iteration: #{i}” }
iteration: 0
iteration: 1
iteration: 2
3

Phil

···

Kirk Haines khaines@enigo.com wrote:

dblack@candle.superlink.net wrote:

$ 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).

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 !

“Hal E. Fulton” hal9000@hypermetrics.com writes:

···

----- Original Message -----
From: “Yohanes Santoso” ysantoso@jenny-gnome.dyndns.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, November 08, 2002 7:49 PM
Subject: Re: Lighting Rod

  1. Why magic variables?

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 $? :slight_smile: (Ok… I’m looking it up now, I’m
also curious as to what it is called).

YS.

Hi –

···

On Sun, 10 Nov 2002, Phil Tomson wrote:

Well, no it’s not inconsistent. The last number you’re seeing is returned
from the block and irb prints it.

s/block/method/ # :slight_smile:

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Phil Tomson wrote:

Try this:

irb(main):002:0> 3.times { |i| puts “iteration: #{i}” }
iteration: 0
iteration: 1
iteration: 2
3

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…

That and the alternatives to some are a little more verbose. I’m thinking of
the Regex match variables $1, $2, etc…

Now I’m curious, how many people use this:

if( str =~ /pattern/)

… do stuff with $1, $2, et al…

versus this:

if( m = /pattern/.match(str) )

do stuff with match obj. m

···

On Sat, Nov 09, 2002 at 11:47:28AM +0900, Yohanes Santoso wrote:

“Hal E. Fulton” hal9000@hypermetrics.com writes:

----- Original Message -----
From: “Yohanes Santoso” ysantoso@jenny-gnome.dyndns.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, November 08, 2002 7:49 PM
Subject: Re: Lighting Rod

  1. Why magic variables?

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 $? :slight_smile: (Ok… I’m looking it up now, I’m
also curious as to what it is called).

YS.


Alan Chen
Digikata LLC
http://digikata.com

Hi –

Now I’m curious, how many people use this:

if( str =~ /pattern/)

… do stuff with $1, $2, et al…

versus this:

if( m = /pattern/.match(str) )

do stuff with match obj. m

I tend to use some variant of the latter. Usually it ends up looking
like:

m = re.match(str)
if m
puts “Your first name is #{m[1]}.”
else
puts “You have no name…”
end

David

···

On Sat, 9 Nov 2002, Alan Chen wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

That and the alternatives to some are a little more verbose. I’m thinking of
the Regex match variables $1, $2, etc…

Now I’m curious, how many people use this:

if( str =~ /pattern/)

… do stuff with $1, $2, et al…

versus this:

if( m = /pattern/.match(str) )

do stuff with match obj. m

Alan Chen

I’m perfectly happy with
str =~ /pattern/

But I do use OO regexen whereever I can, to answer your broader question :slight_smile:

Gavin

···

From: “Alan Chen” alan@digikata.com