Hello all
First some waffle as this is my first post…
I picked up on Ruby just a few days ago. Somebody in an unrelated
thread on an unrelated forum mentioned “A Ruby programmer is a happy
programmer”. So I decided to find out more.
My usual work is currently with Visual Basic and PL/SQL, but I like to
have a general purpose scripting language for all those extra jobs
that VB would be a bit too much of a sledgehammer for!
I’ve tried Perl (hateful messy syntax, gave up)
Looked at Python… not for me
Don’t like VBScript because of it’s “slightly different to VB” nature
(and besides, it’s Windows-only and I use both Windows and Solaris at
work)
etc.
Ruby, as I’m sure you lot have already have discovered, really hits
the spot. I read somewhere of it being called “a delightful
language”, and it’s very true. Nice syntax, nice and clean OO
features, everything I could want and easy to get started with. So
please, Matz and everybody else involved, stick with it, make it even
more feature-rich (but don’t damage the core syntax!), and let me
throw VB away forever!!! (well, I can live in hope!).
Anyway, to my first question…
I’ve read the User Guide and other docs on the net, but I’m a little
unsure as to whether I can ALWAYS substitute the DO/END with curly
brackets for a block? For example, I’ve only ever seen referenced to
hash definitions using curlies. What’s the rule here? Is it purely
down to preference?
Welcome.
You can always substitute the DO/END with curly brackets for a block.
However, in some cases, you may need some extra parenthesis.
This is due to {} having a different precedence than do/end and
correlated impact on associativity.
And Yes, Ruby Rocks.
Yours,
Jean-Hugues
···
At 05:54 02/05/2004 +0900, you wrote:
I’ve read the User Guide and other docs on the net, but I’m a little
unsure as to whether I can ALWAYS substitute the DO/END with curly
brackets for a block? For example, I’ve only ever seen referenced to
hash definitions using curlies. What’s the rule here? Is it purely
down to preference?
Web: @jhr is virteal, virtually real
Phone: +33 (0) 4 92 27 74 17
While there is some amount of preference involved, there is a general
rule that most Ruby programmers (and matz, I believe) agree on:
brackets for one-line blocks, do…end for longer blocks. ie:
5.times {|n| puts n }
5.times do |n|
print "bingo! " if n == 3
puts n
end
There are skirmishes sometimes between people who like to use all
brackets all the time, and those who use the above method. But this
is the most common way, and you’ll find it mostly like that in the
standard library.
HTH,
–Mark
···
On May 1, 2004, at 1:54 PM, Glenn wrote:
I’ve read the User Guide and other docs on the net, but I’m a little
unsure as to whether I can ALWAYS substitute the DO/END with curly
brackets for a block? For example, I’ve only ever seen referenced to
hash definitions using curlies. What’s the rule here? Is it purely
down to preference?
yes, in ambiguous situations, it can require parent either way. For
example, if you write
array.inject num.to_f {|a,b| a * b }
Ruby assumes you want the block to be passed to the num.to_f method
call. This is probably not what you want, though. To solve that one,
put parens around the num.to_f call:
array.inject( num.to_f ) {|a,b| a * b }
That will work properly. likewise, there can be similar problems with
do…end:
obj.foo array.map do |item|
p item
item * 2
end
This snippet will hand the block to obj.foo, which is probably not what
you want. to solve it, you could do this:
obj.foo( array.map do |item|
p item
item * 2
end )
… but that’s ugly. you would probably want to just break down and do
it in two steps:
a = array.map do |item|
p item
item * 2
end
obj.foo a
or convert it to a one liner:
obj.foo array.map{|item| p item; item * 2 }
cheers,
–Mark
···
On May 1, 2004, at 2:06 PM, Jean-Hugues ROBERT wrote:
At 05:54 02/05/2004 +0900, you wrote:
I’ve read the User Guide and other docs on the net, but I’m a little
unsure as to whether I can ALWAYS substitute the DO/END with curly
brackets for a block? For example, I’ve only ever seen referenced to
hash definitions using curlies. What’s the rule here? Is it purely
down to preference?
Welcome.
You can always substitute the DO/END with curly brackets for a block.
However, in some cases, you may need some extra parenthesis.
This is due to {} having a different precedence than do/end and
correlated impact on associativity.
Mark Hubbart wrote:
While there is some amount of preference involved, there is a general
rule that most Ruby programmers (and matz, I believe) agree on: brackets
for one-line blocks, do…end for longer blocks. ie:
I’ve heard that general advice, and even followed it for a while. I
finally gave up on it because many one liners grow into multiline
blocks, and it was just annoying to switch from {} to do/end when that
happened. Plus the style choice wasn’t conveying any additional
information, so I dropped it and went back to a “whatever looks good” style.
Just recently I’ve been trying out the following guidelines:
o If the block returns a value, use {}
o if the block doesn’t return a value, use do/end
So far I like the way it is turning out. The style choice helps to
convey some of the intent of the programmer.
I offer it here for others to try. If they like it, fine. If it
doesn’t work for you, no problem either.
···
“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)
I’ve read the User Guide and other docs on the net, but I’m a little
unsure as to whether I can ALWAYS substitute the DO/END with curly
brackets for a block? For example, I’ve only ever seen referenced to
hash definitions using curlies. What’s the rule here? Is it purely
down to preference?
As somebody has mentioned already it is not only preference, but also
precedence rules ({} sticks better than do…end, meaning higher
precedence).
While there is some amount of preference involved, there is a general
rule that most Ruby programmers (and matz, I believe) agree on:
brackets for one-line blocks, do…end for longer blocks. ie:
5.times {|n| puts n }
There’s also another usage some people, including myself, agree on:
do…end for blocks that actually DO stuff, like in Array#each,
File.open, Integer#times (Mark, sorry for contradiction ;-)), etc.; {}
for blocks that define conditions, like in map, select/reject, sort,
etc.
Sincerely,
Gennady Bystritsky
···
On May 1, 2004, at 2:33 PM, Mark Hubbart wrote:
On May 1, 2004, at 1:54 PM, Glenn wrote:
Jim Weirich wrote:
Mark Hubbart wrote:
While there is some amount of preference involved, there is a general
rule that most Ruby programmers (and matz, I believe) agree on:
brackets for one-line blocks, do…end for longer blocks. ie:
I’ve heard that general advice, and even followed it for a while. I
finally gave up on it because many one liners grow into multiline
blocks, and it was just annoying to switch from {} to do/end when that
happened. Plus the style choice wasn’t conveying any additional
information, so I dropped it and went back to a “whatever looks good”
style.
Just recently I’ve been trying out the following guidelines:
o If the block returns a value, use {}
o if the block doesn’t return a value, use do/end
{ … } means less typing, plus brace matching in Vim.
Yeah, I know that the ruby plugin for Vim will auto-type do/end for me,
but I also find the braces visually cleaner.
James
Gennady Bystritsky gfb@tonesoft.com wrote in message news:B38C9AD7-9BD1-11D8-B7CB-000A95EED88E@tonesoft.com…
There’s also another usage some people, including myself, agree on:
do…end for blocks that actually DO stuff, like in Array#each,
File.open, Integer#times (Mark, sorry for contradiction ;-)), etc.; {}
for blocks that define conditions, like in map, select/reject, sort,
etc.
And this would include curlies for hash definitions then?
I like the idea of standardising on do/end for multi-line blocks and
curlies for single lines. That makes sense. I like the readability
of using do/end - seems to make for nicer code.
Thanks all, you’ve helped a lot. Ruby certainly does rock!
I also like to use {} when I know the block will still exist
after the method finishes (for example in signal callbacks or
lambda expressions).
This way you can see clearly what belongs together.
Anyway calling it a rule I find be a bit to strong, I believe
it is anyone’s one choice which one he/she prefers to use.
Kristof
···
On Sun, 02 May 2004 09:43:20 +0900, Gennady Bystritsky wrote:
There’s also another usage some people, including myself, agree on:
do…end for blocks that actually DO stuff, like in Array#each,
File.open, Integer#times (Mark, sorry for contradiction ;-)), etc.; {}
for blocks that define conditions, like in map, select/reject, sort,
etc.
I agree with Jim Weirich, and would just like to point out:
- Vim will %-match do/end as well as {}
- do/end is easier to type than {} in terms of wear and tear on
my poor fingers
Gavin
···
On Sunday, May 2, 2004, 9:53:40 AM, James wrote:
Just recently I’ve been trying out the following guidelines:
o If the block returns a value, use {}
o if the block doesn’t return a value, use do/end
{ … } means less typing, plus brace matching in Vim.
Yeah, I know that the ruby plugin for Vim will auto-type do/end for me,
but I also find the braces visually cleaner.
Gavin Sinclair wrote:
Yeah, I know that the ruby plugin for Vim will auto-type do/end for me,
but I also find the braces visually cleaner.
I agree with Jim Weirich, and would just like to point out:
- Vim will %-match do/end as well as {}
That’s good news. Is this in the current Rub Vim scripts? I have some
version of ruby.vim/ruby-macros , but it does not % match do/end for me.
- do/end is easier to type than {} in terms of wear and tear on
my poor fingers
Ok, well how 'bout this: braces use fewer characters, so you save disk
space.
James
···
On Sunday, May 2, 2004, 9:53:40 AM, James wrote:
- do/end is easier to type than {} in terms of wear and tear on
my poor fingers
Ok, well how 'bout this: braces use fewer characters, so you save disk
space.
Whether do/end is easier to type than {} is rather subjective… from
personal experience, I tend towards {} because I can type {} in an
easy stroke (hold down shift, roll fingers over { and }), and I can
then fill in. But I digress - I’m just talking of personal experience.
To tackle the answer from a different angle - my guess is that
programmers who come from a C/Java/Perl environment would lean towards
{}, and programmers from a shell scripting environment may tend
towards do/end, simply because of habit.
The latest CVS from http://vim-ruby.rubyforge.org should provide this
behaviour. The truly good news is that Vim 6.3 is apparently not far
away, and it should include this.
Cheers,
Gavin
···
On Sunday, May 2, 2004, 3:53:30 PM, James wrote:
Gavin Sinclair wrote:
On Sunday, May 2, 2004, 9:53:40 AM, James wrote:
Yeah, I know that the ruby plugin for Vim will auto-type do/end for me,
but I also find the braces visually cleaner.
I agree with Jim Weirich, and would just like to point out:
- Vim will %-match do/end as well as {}
That’s good news. Is this in the current Rub Vim scripts? I have some
version of ruby.vim/ruby-macros , but it does not % match do/end for me.
James Britt wrote:
Ok, well how 'bout this: braces use fewer characters, so you save disk
space.
That’s easy; just use a smaller font. =)
I come from Pascal, so I tend to do/end. So you are probably right
about the habit.
The sad thing is that this style issue really matters a lot for people.
As a result rejecting either option is religious and accepting both is
agnostic. Once again religion leads to war. Really sad.
Ruby: a peaceful language (and fun too).
Yours,
Jean-Hugues
···
At 15:12 02/05/2004 +0900, you wrote:
- do/end is easier to type than {} in terms of wear and tear on
my poor fingers
Ok, well how 'bout this: braces use fewer characters, so you save disk
space.
Whether do/end is easier to type than {} is rather subjective… from
personal experience, I tend towards {} because I can type {} in an
easy stroke (hold down shift, roll fingers over { and }), and I can
then fill in. But I digress - I’m just talking of personal experience.
To tackle the answer from a different angle - my guess is that
programmers who come from a C/Java/Perl environment would lean towards
{}, and programmers from a shell scripting environment may tend
towards do/end, simply because of habit.
Web: http://hdl.handle.net/1030.37/1.1
Phone: +33 (0) 4 92 27 74 17
Hi –
Jean-Hugues ROBERT jean_hugues_robert@yahoo.com writes:
- do/end is easier to type than {} in terms of wear and tear on
my poor fingers
Ok, well how 'bout this: braces use fewer characters, so you save disk
space.
Whether do/end is easier to type than {} is rather subjective… from
personal experience, I tend towards {} because I can type {} in an
easy stroke (hold down shift, roll fingers over { and }), and I can
then fill in. But I digress - I’m just talking of personal experience.
To tackle the answer from a different angle - my guess is that
programmers who come from a C/Java/Perl environment would lean towards
{}, and programmers from a shell scripting environment may tend
towards do/end, simply because of habit.
I come from Pascal, so I tend to do/end. So you are probably right
about the habit.
I come from Ruby and I find myself tending to use {} for shorter
blocks and/or blocks that are chained:
a.select {blah}.map {blah} # etc.
partly because “do blah end.map do blah end” sort of runs together
visually.
My instinct was always to space the dots apart in such cases:
a.select {blah} .map {blah}
though I’ve recently noticed that this isn’t done in the Ruby
distribution, which I generally use as my style reference.
The sad thing is that this style issue really matters a lot for people.
As a result rejecting either option is religious and accepting both is
agnostic. Once again religion leads to war. Really sad.
Heavens. I think we were just chatting about it. Here on
ruby-talk/clr we still do that sometimes
David
···
At 15:12 02/05/2004 +0900, you wrote:
–
David A. Black
dblack@wobblini.net
Jean-Hugues ROBERT wrote:
···
At 15:12 02/05/2004 +0900, you wrote:
The sad thing is that this style issue really matters a lot for people.
As a result rejecting either option is religious and accepting both is
agnostic. Once again religion leads to war. Really sad.
I don’t see religion as having anything to do with this; I follow the
principal of IDGARA:
I Don’t Give A Rat’s Ass
James
Hi David, Robert & all…
David Alan Black wrote:
Jean-Hugues ROBERT jean_hugues_robert@yahoo.com writes:
- do/end is easier to type than {} in terms of wear and tear on
my poor fingers
8< snip …
Whether do/end is easier to type than {} is rather subjective…
…and certainly depends on the keyboard layout. On my (German) keyboard
for example ‘{’ is reached with [AltGr]-7 and I get ‘}’ with [AltGr]-0
(which, IMO, isn’t too comfortable).
Nevertheless I prefer using the {} pair. Perhaps because I used C++ and
the like before I switched to Ruby. But then, I used Pascal too (but
loooong ago).
8< snip …
The sad thing is that this style issue really matters a lot for people.
As a result rejecting either option is religious and accepting both is
agnostic. Once again religion leads to war. Really sad.
??? I don’t think we’re at war.
Heavens. I think we were just chatting about it. Here on
ruby-talk/clr we still do that sometimes
Yes. And that’s great.
It’s a matter of personal style, preferences, hisotry, momentum (in a
way), instinct and may be keyborad layout and (typing) lazyness.
I wouldn’t accept neither of these as a reason for war, heatet debates
or anything like that.
I like programming, I like Ruby - I combine these and be happy.
(And I somewhat like the idea of taoism, which you might have noticed…)
Happy rubying
Stephan
···
At 15:12 02/05/2004 +0900, you wrote:
I admit, I hadn’t realized the situation was a complex as it is. I had
seen where people used only brackets, so I mentioned that, not knowing
how many different styles I was leaving out It’s probably because
I’ve only been here a few months, and haven’t seen all the discussion.
Still, it’s very interesting to me to hear all the different rules
people use for their code blocks.
A very informative thread for me. I hope it didn’t sound like a flame
war
–Mark
···
On May 2, 2004, at 3:34 AM, David Alan Black wrote:
The sad thing is that this style issue really matters a lot for
people.
As a result rejecting either option is religious and accepting both is
agnostic. Once again religion leads to war. Really sad.
Heavens. I think we were just chatting about it. Here on
ruby-talk/clr we still do that sometimes
James Britt wrote:
Jean-Hugues ROBERT wrote:
The sad thing is that this style issue really matters a lot for people.
As a result rejecting either option is religious and accepting both is
agnostic. Once again religion leads to war. Really sad.
I don’t see religion as having anything to do with this; I follow the
principal of IDGARA:
I Don’t Give A Rat’s Ass
James
VIAGRA
Valuing
Independent
Analysis
Generally
Raises
Awareness
Even if you dont give a crap about the conclusion
···
At 15:12 02/05/2004 +0900, you wrote: