Object loops and what they return

Jim Freeze <jim@freeze.org> writes:

* Jim Weirich <jim@weirichhouse.org> [2005-05-12 23:32:52 +0900]:

I use to pitch my tent in the one line => braces camp. But every time my
one liner tent grew to more than one line, they made me repitch my tent
using begin/end tent stakes. That's when I moved to the return value =>
brace camp.

I came from the C camp, where everything looked like {}.
Since my editor supported matching these curly things,
I (usually) pitch camp at { || ... }.

Welcome, fellow. :slight_smile: Have some marshallows.

My rule of thumb is like this: I use {} constantly, except for
rakefile rules and other similar domain specific/configuration
languages.

Usually, this results in a nice mix of `}' and `end' closing their
beginnings, and makes finding the matching beginning easier. YMMV.

BTW, I do regularly hike up to the ternary operator camp as well, so I'll
see you there.

I too like this camp since it is a short hike, but since I
don't like strenuous activitiy, I never nest them.

Welcome, fellow... :slight_smile:

Then, I'm often in the camp of inline-case users... how about you?

···

Jim Freeze

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Hi --

···

On Fri, 13 May 2005, Jim Weirich wrote:

David A. Black said:

I know, it's not 100% reliable... but it suggests that, at least
roughly, 3% of 'do' blocks end on the same line, while 62% of {}
blocks end on the same line. So this "odd" behavior is actually
pretty canonical :slight_smile:

Hmmm ... More statistics, same code base, same disclaimers.

dev05$ find . -name '*.rb' | xargs egrep '(each|loop) *do' | wc -l
   1112
dev05$ find . -name '*.rb' | xargs egrep '(each|loop) *{' | wc -l
    878
dev05$ find . -name '*.rb' | xargs egrep
'(collect|map|find|find_all|select|((inject|grep) *\(.*\))) *do' | wc -l
     68
dev05$ find . -name '*.rb' | xargs egrep
'(collect|map|find|find_all|select|((inject|grep) *\(.*\))) *{' | wc -l
    657

So, nearly 55% of methods that ignore the return value of the block use
do/end.

Over 90% of the methods that use the block's value use {}.

So you can use either style and claim to be main-stream.

It looks like you can only claim to be mainstream if you use *both*
styles :slight_smile: Or am I reasoning wrongly?

David

--
David A. Black
dblack@wobblini.net

I could accept that too -- I would just want one syntax for blocks, a
different one for braces, and no confusing middle ground.

I'd be happy with this for arrays too:

["pet" => "dog", "name" => "fred", "drink" => "ale"]

or

["pet": "dog", "name": "fred", "drink": "ale"]

I just think that {a => b} and {a <= b} are too similar.

Ben

···

On Thursday 12 May 2005 13:38, David A. Black wrote:

You'll have to fight it out with the ones who wanted braces for block
literals and hashes to be something else :slight_smile:

* Christian Neukirchen <chneukirchen@gmail.com> [2005-05-13 03:03:54 +0900]:

Then, I'm often in the camp of inline-case users... how about you?

The camp I belong to here is what yields the best readability.
For example:

  # for returning short, simple expressions
  fred =
    case x
      when 'y' then true
      when 'n' then false
      else raise
    end

  # for doing multiple actions - usually no return value
  case x
  when 'y'
    do_long_compute_1
    do_long_compute_2
  when 'b'
    do_long_compute_3
    do_long_compute_4
  else
    raise
  end

···

--
Jim Freeze
Ruby: I can explain it to ya but I can't understand it fer ya.

David A. Black wrote:

> I know it's way too late for the Ruby 1.X series, but I'd like to

see a

> change for Ruby 2 that reserved braces for hashes and blocks had to

have

> do/end.

You'll have to fight it out with the ones who wanted braces for block
literals and hashes to be something else :slight_smile:

/me fumbles around trying to find his personal sword to join the fray.

I would agree that the duality of {} vs do/end is a bit odd and sort of
unlike Ruby. (I could stretch and say "You can have arbitrary
delimiters for all sorts of literals, so...blocks are sort of like
that" but it would be a stretch.)

I'd far, far prefer {} for blocks than only do/end. One of the greatest
strengths of Ruby IMO is simple iterators using closures. You can do
the exact same thing in Javascript, but it looks like this:

myArray.each( function( obj ){
   alert( obj );
} );

Obviously that's TERRIBLE. And the reason it's terrible is the large
amount of crap you have to type to express a simple concept.

The degree to which blocks become harder/longer to type is the degree
to which Ruby loses some of its shine, IMO.

···

On Fri, 13 May 2005, Ben Giddings wrote:

Tangentially, inject uses the first value of the enumerable as the seed
if you don't supply one (the joys of dynamic typing!), so you can simply
say (1..100).inject {|r,v| r*v}

martin

···

Brian Schröder <ruby.brian@gmail.com> wrote:

print "100! = "
puts (1..100).inject(1) { |r,v|
  r*v
}

David A. Black said:

It looks like you can only claim to be mainstream if you use *both*
styles :slight_smile: Or am I reasoning wrongly?

Since value returning blocks tend to be shorter than side-effecting
blocks[1], I'm not surprised that there is a correlation between the two
styles.

I guess do/end vs {} is Ruby's version of the "One True Brace Style"
argument.

···

--
-- Jim Weirich jim@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

[1] I have no proof of this ... so shoot me.

Ben Giddings wrote:

···

On Thursday 12 May 2005 13:38, David A. Black wrote:

You'll have to fight it out with the ones who wanted braces for block
literals and hashes to be something else :slight_smile:

I could accept that too -- I would just want one syntax for blocks, a different one for braces, and no confusing middle ground.

I'd be happy with this for arrays too:

["pet" => "dog", "name" => "fred", "drink" => "ale"]

or

["pet": "dog", "name": "fred", "drink": "ale"]

I just think that {a => b} and {a <= b} are too similar.

But then there would be the similarity between

["a" => "b"] # Hash

and

["a" <= "b"] # Array

(or did I misunderstand?)

Martin DeMello wrote:

print "100! = "
puts (1..100).inject(1) { |r,v|
  r*v
}

Tangentially, inject uses the first value of the enumerable as the
seed if you don't supply one (the joys of dynamic typing!), so you
can simply say (1..100).inject {|r,v| r*v}

This might or might not work, depending on the requirements of the
surrounding code:

.inject(1){|a,b| a*b}

=> 1

.inject{|a,b| a*b}

=> nil

.inject(0){|a,b| a+b}

=> 0

.inject{|a,b| a+b}

=> nil

:-))

If you want to detect the case where there are no elements go without the
argument. If you don't care and just want the sum / product provide an
argument (the neutral element of the operation).

Kind regards

    robert

···

Brian Schröder <ruby.brian@gmail.com> wrote:

How is that one an array? Oh, is it an array containing 'true'?

I've never been super fond of '=>' as a syntax for creating hashes, but I
can accept it. The point I'm trying to make is that currently it's easy
to visually confuse a block and a hash. I'd prefer if array, block and
hash were all visually distinct, but I think it's much less dangerous to
have arrays and hashes look similar because they are similar creatures.

Ben

···

On Thursday 12 May 2005 15:22, Joel VanderWerf wrote:

But then there would be the similarity between

["a" => "b"] # Hash

and

["a" <= "b"] # Array

Ben Giddings wrote:

But then there would be the similarity between

["a" => "b"] # Hash

and

["a" <= "b"] # Array

How is that one an array? Oh, is it an array containing 'true'?

irb(main):001:0> ["a" <= "b"]
=> [true]

I've never been super fond of '=>' as a syntax for creating hashes, but I can accept it. The point I'm trying to make is that currently it's easy to visually confuse a block and a hash. I'd prefer if array, block and hash were all visually distinct, but I think it's much less dangerous to have arrays and hashes look similar because they are similar creatures.

OTOH, hashes and arrays tend to be used in the same kinds of places, and blocks tend to be in different places, so context helps us distinguish hashes from blocks in the current syntax.

···

On Thursday 12 May 2005 15:22, Joel VanderWerf wrote:

Hi --

···

On Fri, 13 May 2005, Joel VanderWerf wrote:

Ben Giddings wrote:

I've never been super fond of '=>' as a syntax for creating hashes, but I can accept it. The point I'm trying to make is that currently it's easy to visually confuse a block and a hash. I'd prefer if array, block and hash were all visually distinct, but I think it's much less dangerous to have arrays and hashes look similar because they are similar creatures.

OTOH, hashes and arrays tend to be used in the same kinds of places, and blocks tend to be in different places, so context helps us distinguish hashes from blocks in the current syntax.

Yes, and another thing that's getting lost in the mix here is the fact
that {} and do/end are not the same as each other. They have
different precedence. It's not just a whimsical TMTOWTDI thing.

David

--
David A. Black
dblack@wobblini.net

David A. Black wrote:

Hi --

Ben Giddings wrote:

I've never been super fond of '=>' as a syntax for creating hashes,
but I can accept it. The point I'm trying to make is that
currently it's easy to visually confuse a block and a hash. I'd
prefer if array, block and hash were all visually distinct, but I
think it's much less dangerous to have arrays and hashes look
similar because they are similar creatures.

OTOH, hashes and arrays tend to be used in the same kinds of places,
and blocks tend to be in different places, so context helps us
distinguish hashes from blocks in the current syntax.

Yes, and another thing that's getting lost in the mix here is the fact
that {} and do/end are not the same as each other. They have
different precedence. It's not just a whimsical TMTOWTDI thing.

I'm glad someone finally mentioned this. I was about to write the same...
:slight_smile:

Kind regards

    robert

···

On Fri, 13 May 2005, Joel VanderWerf wrote: