Robert Dober
6/12/2007 3:46:00 PM
> Almost. puts adds "\n" unless it's already there. So these are
> equivalent:
>
> print "hello\n"
> puts "hello\n"
Right but do you remember when I stupidly changed your code from
puts a
to
puts a.join("\n")
So this is another difference between print and puts, puts prints the
content of an array seperated by newlines. print does no such thing of
course.
> particular, please have mercy and don't leave the parens out in method
> signatures. Things like this:
>
> def a b, c, d = 1
>
> read very strangely, at least to my eyes.
I love it, I *really* read it better like this. But I guess the
community rather puts parens :( and if you want to comply listen to
David.
Let us just have a look at the Ruby core as David suggests below
All methods
ruby -e 'c=0;ARGF.each{|f|c+=1 if /^\s*def\s/===f};puts c' $(ruby -e
'puts Dir["**/*.rb"]')
18258
====
All methods with parens
ruby -e 'c=0;ARGF.each{|f|c+=1 if /^\s*def.*\(/===f};puts c' $(ruby
-e 'puts Dir["**/*.rb"]')
12271
====
All methods without parens and without parameters
ruby -e 'c=0;ARGF.each{|f|c+=1 if /^\s*def\s\w+\s*$/===f};puts c'
$(ruby -e 'puts Dir["**/*.rb"]')
5303 +
------
ruby -e 'c=0;ARGF.each{|f|c+=1 if /^\s*def\s\w+\s*#/===f};puts c'
$(ruby -e 'puts Dir["**/*.rb"]')
94 = 5397
--- ====
Leaves
ruby -e 'puts 18258 - 12271 - 5303 - 97'
587 methods corresponding to "our" style which makes
ruby -e 'puts 58700/18258.0'
3.21502902837112%
Not enough to get into parliament I'd say.
>
> > As well, reserve the { } form of blocks to one-liners.
>
> That's going to depend partly on whether you run across the
> (relatively rare) case where the precedence difference between {} and
> do/end actually matters. There are also some interesting ideas on
> record (see archives) involving blocks with side effects vs. blocks
> that just calculate. But I can't remember which is supposed to be
> which :-)
>
I try to avoid do end inside { }
Beware of this too
some_method a, b {|x| puts x}
will not work use either parens or do...end.
Cheers
Robert
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw