Hi --

On Fri, 28 Sep 2007, Bertram Scharpf wrote:

> Hi,
>
> Am Freitag, 28. Sep 2007, 17:20:40 +0900 schrieb Tom Metge:
>> On 9/28/07 2:13 AM, "Daniel Talsky" <danieltalsky / gmail.com> wrote:
>>
>>> 10.times do
>>>   collection_of_objects << MyObject.create
>>> end
>>>
>>> But this doesn't:
>>>
>>> 10.times do { collection_of_objects << MyObject.create }
>>
>> Take the "do" out of the statement when passing a block in {}:
>>
>> 10.times {collection_of_objects << MyObject.create}
>
> Yet another opportunity to mention that {} has higer
> precedence than do..end.
>
>    def f
>      puts "((("
>      yield if block_given?
>      puts ")))"
>    end
>    def g x
>      puts "[[[#{x}|"
>      yield if block_given?
>      puts "]]]"
>    end
>
>  > g f { puts "x" }
>  (((
>  x
>  )))
>  [[[|
>  ]]]
>  > g f do puts "x" end
>  (((
>  )))
>  [[[|
>  x
>  ]]]
>
>
> Not a very intuitive example I admit.

Here's one I've stumbled on in real life:

irb(main):001:0> a = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):002:0> puts a.map {|e| e * 10 }
10
20
30
40
=> nil
irb(main):003:0> puts a.map do |e| e * 10 end
1
2
3
4
=> nil


David

-- 
Upcoming training from Ruby Power and Light, LLC:
   * Intro to Ruby on Rails, Edison, NJ, October 23-26
   * Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!