block_given? problem

W

westlin9

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
 
R

Robert Dober

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
 
W

westlin9

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 -

Thanks
 
M

Mariusz Pękala

--QKdGvSO+nmPlgiQ/
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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?
=20
=20
class TheClass
def self.TheMethod()
if block_given?
puts "yes, block given"
else
puts "no block is given"
end
'return val'
end
end
=20
# ok
puts TheClass.TheMethod {|param| 123}
=20
# 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 =3D TheClass.TheMethod {|param| 123}
puts a
=20
# ok
a =3D TheClass.TheMethod do |param|
123
end
puts a

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


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

--QKdGvSO+nmPlgiQ/
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7-ecc0.1.6 (GNU/Linux)

iD8DBQFGoK6CsnU0scoWZKARAjRyAJ0X/iohGUZnNllIT/dVcyMH3R6IqgCgwt61
Nnc26RoAjeGPvwzUX3whblI=
=V0YH
-----END PGP SIGNATURE-----

--QKdGvSO+nmPlgiQ/--
 
R

Rick DeNatale

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

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,986
Latest member
ColbyG935

Latest Threads

Top