Chad P. wrote:
I’d be happy with it either way, as long as it’s consistent.
Without space, it’s consistent.
It’s not always what we want it to mean, though.
Take this example (with which I frequently inconvenience myself):
#-----------------------------------------------------
(0…5).map do |n|
10*n
end
#-----------------------------------------------------
- I want to see the result, so I prepend "p ":
#-----------------------------------------------------
p (0…5).map do |n|
10*n
end
#=> C:/TEMP/rb284.TMP:1: warning: (…) interpreted as grouped
expression
[- Okay, that’s what it is.]
#=> [0, 1, 2, 3, 4, 5]
[- But that’s not what I wanted. The block binds to method #p
- the argument to #p is ((0…5).map) - an Array (in Ruby 1.8.2)
#-----------------------------------------------------
#-----------------------------------------------------
p(0…5).map do |n|
10*n
end
#=> 0…5
[Oh, no !]
#=> C:/TEMP/rb284.TMP:1: undefined method `map’ for nil:NilClass
(NoMethodError)
[Aargh!]
#-----------------------------------------------------
- Parenthesise correctly but “uglily” ;))
#-----------------------------------------------------
p((0…5).map do |n|
10*n
end)
#=> [0, 10, 20, 30, 40, 50] # no problem at all
#-----------------------------------------------------
- But we don’t need to do any of those.
- From the initial example, just prepend " = "
and there’s no “binding” issue …
#-----------------------------------------------------
r = (0…5).map do |n|
10*n
end
p r #=> [0, 10, 20, 30, 40, 50]
#-----------------------------------------------------
IMHO, it’s our problem, not Ruby’s.
The problem could appear in many places but, for me,
it’s almost always when prepending p, puts or print.
Things could be a lot worse than this
daz