Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > what's the difference?

Reply
Thread Tools

what's the difference?

 
 
Joel VanderWerf
Guest
Posts: n/a
 
      10-23-2005
Daniel Sch=FCle wrote:
> Hello @*,
>=20
> irb(main):105:0> 10.times { d=3D{}; puts d.object_id }
> 537970544
> 537969634
> 537969504
> 537969404
> 537969354
> 537969334
> 537969304
> 537969274
> 537969244
> 537969184
> =3D> 10
> irb(main):106:0>
>=20
> 10 different Hash objects are constructed and their id's are printed ou=

t
> why doesn't the following do the same?
>=20
> irb(main):106:0> 10.times { puts {}.object_id }
>=20
>=20
>=20
>=20
>=20
>=20
>=20
>=20
>=20
>=20
> =3D> 10
> irb(main):107:0>


Ruby's parser associates the second line like this:

irb(main):001:0> 10.times { (puts {}).object_id }

Which does a "puts {}" ten times, and prints 10 blank lines. the
#object_id method is called on the return value of puts (which is nil)
and is never printed.

But:

irb(main):002:0> 10.times { puts({}.object_id) }
-606176002
-606176022
-606176042
-606176072
-606176092
-606176112
-606176132
-606176162
-606176182
-606176202
=3D> 10

Why does this funny association happen? Ruby thinks the {} without
parens is a block, not a hash:

irb(main):004:0> puts {}

=3D> nil
irb(main):005:0> puts do end

=3D> nil
irb(main):006:0> puts {exit}

=3D> nil

The block is silently ignored by ruby.

--=20
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


 
Reply With Quote
 
 
 
 
Daniel Schüle
Guest
Posts: n/a
 
      10-23-2005
Hello @*,

irb(main):105:0> 10.times { d={}; puts d.object_id }
537970544
537969634
537969504
537969404
537969354
537969334
537969304
537969274
537969244
537969184
=> 10
irb(main):106:0>

10 different Hash objects are constructed and their id's are printed out
why doesn't the following do the same?

irb(main):106:0> 10.times { puts {}.object_id }










=> 10
irb(main):107:0>

I copied & pasted the above code
mond:/pool/PROG/ruby # irb -v
irb 0.9(02/07/03)

I am in the learning phase so I experement a lot

thx, Daniel

 
Reply With Quote
 
 
 
 
Dave Burt
Guest
Posts: n/a
 
      10-23-2005
Daniel Schüle
> thank you for this explanation, I think I got it.
> if a function doesn't expect a block parameter
> but gets one, then this block is ignored
>
> def foo; end
> foo {exit} => nil
> foo { puts "hello" } => nil
>
> and puts is not supposed to take a block as parameter


That's right. Your method foo also ignores any given block; it's the default
behaviour.

p {} # a block
p({}) # a hash

Cheers,
Dave


 
Reply With Quote
 
Daniel Schüle
Guest
Posts: n/a
 
      10-23-2005
> Why does this funny association happen? Ruby thinks the {} without
> parens is a block, not a hash:
>
> irb(main):004:0> puts {}
>
> => nil
> irb(main):005:0> puts do end
>
> => nil
> irb(main):006:0> puts {exit}
>
> => nil
>
> The block is silently ignored by ruby.



thank you for this explanation, I think I got it.
if a function doesn't expect a block parameter
but gets one, then this block is ignored

def foo; end
foo {exit} => nil
foo { puts "hello" } => nil

and puts is not supposed to take a block as parameter

Regards, Daniel

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments