Block_given? problem

I can't figure out why the do...end block isn't detected by the
"block_given?" function. The problem occurs when a method call with a
block is used in a puts statement. See the code example below.
Can some one explain this to me please? Is this a bug?

class TheClass
  def self.TheMethod()
    if block_given?
      puts "yes, block given"
    else
      puts "no block is given"
    end
    'return val'
  end
end

# ok
puts TheClass.TheMethod {|param| 123}

# block not detected
puts TheClass.TheMethod do |param|
  123
end

# ok
a = TheClass.TheMethod {|param| 123}
puts a

# ok
a = TheClass.TheMethod do |param|
  123
end
puts a

puts( TheClass.TheMethod do nil end )

do...end and {} do not have the same precedence, thus, without the ()
you are doing this
puts( TheClass.TheMethod ) do nil end

HTH
Robert

···

On 7/20/07, westlin9@gmail.com <westlin9@gmail.com> wrote:

I can't figure out why the do...end block isn't detected by the
"block_given?" function. The problem occurs when a method call with a
block is used in a puts statement. See the code example below.
Can some one explain this to me please? Is this a bug?

class TheClass
  def self.TheMethod()
    if block_given?
      puts "yes, block given"
    else
      puts "no block is given"
    end
    'return val'
  end
end

# ok
puts TheClass.TheMethod {|param| 123}

# block not detected
puts TheClass.TheMethod do |param|
  123
end

# ok
a = TheClass.TheMethod {|param| 123}
puts a

# ok
a = TheClass.TheMethod do |param|
  123
end
puts a

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

I can't figure out why the do...end block isn't detected by the
"block_given?" function. The problem occurs when a method call with a
block is used in a puts statement. See the code example below.
Can some one explain this to me please? Is this a bug?

class TheClass
  def self.TheMethod()
    if block_given?
      puts "yes, block given"
    else
      puts "no block is given"
    end
    'return val'
  end
end

# ok
puts TheClass.TheMethod {|param| 123}

# block not detected
puts TheClass.TheMethod do |param|
  123
end

This is because do..end block gets attached to the 'puts' instead of
TheMethod.

do..end and {||..} differ in precedence. {||..} binds to the rightmost
method, while do..end to the leftmost one.

# ok
a = TheClass.TheMethod {|param| 123}
puts a

# ok
a = TheClass.TheMethod do |param|
  123
end
puts a

There you have no ambiguity as there is only one method call.

···

On 2007-07-20 20:19:58 +0900 (Fri, Jul), westlin9@gmail.com wrote:

--
No virus found in this outgoing message.
Checked by 'grep -i virus $MESSAGE'
Trust me.

Thanks

···

On 20 Juli, 14:04, "Robert Dober" <robert.do...@gmail.com> wrote:

On 7/20/07, westl...@gmail.com <westl...@gmail.com> wrote:

> I can't figure out why the do...end block isn't detected by the
> "block_given?" function. The problem occurs when a method call with a
> block is used in a puts statement. See the code example below.
> Can some one explain this to me please? Is this a bug?

> class TheClass
> def self.TheMethod()
> if block_given?
> puts "yes, block given"
> else
> puts "no block is given"
> end
> 'return val'
> end
> end

> # ok
> puts TheClass.TheMethod {|param| 123}

> # block not detected
> puts TheClass.TheMethod do |param|
> 123
> end

> # ok
> a = TheClass.TheMethod {|param| 123}
> puts a

> # ok
> a = TheClass.TheMethod do |param|
> 123
> end
> puts a

puts( TheClass.TheMethod do nil end )

do...end and {} do not have the same precedence, thus, without the ()
you are doing this
puts( TheClass.TheMethod ) do nil end

HTH
Robert

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck- Dölj citerad text -

- Visa citerad text -

do..end vs {}
and vs. &&
or vs. ||
not vs !

I think that it can be said that generally in ruby "symbols bind
tighter than words"

···

On 7/20/07, Robert Dober <robert.dober@gmail.com> wrote:

do...end and {} do not have the same precedence, thus, without the ()
you are doing this
puts( TheClass.TheMethod ) do nil end

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/