[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Rite, Block locals and Autovivification

dblack

12/1/2003 11:19:00 PM

4 Answers

John Carter

12/2/2003 9:23:00 PM

0

Bill Kelly

12/2/2003 9:33:00 PM

0

Hi,

From: "John Carter" <john.carter@tait.co.nz>
>
> def histogram( enum)
> # <--- Implicit hash = Hash.new(0) here
> enum.each{|e| hash[e]+=1} # Hash autovivifies.

eh? But... somevar[e] ... Why would Ruby assume I wanted a Hash
object? That could be an Array, or a String, or.......

Do you mean the autovivification would happen only if the variable
was actually _named_ 'hash' ?

Or am I not understanding your example?

> # Since hash autovivified we expect it to be available
> # outside the block scope.
> hash.keys.sort{|a,b| hash[a]<=>hash[b]}.each do |k|
> puts "#{k}\t#{hash[k]}"
> end
> end


Regards,

Bill




Dan Doel

12/2/2003 9:42:00 PM

0

John Carter wrote:

>def histogram( enum)
> # <--- Implicit hash = Hash.new(0) here
> enum.each{|e| hash[e]+=1} # Hash autovivifies.
> # Since hash autovivified we expect it to be available
> # outside the block scope.
> hash.keys.sort{|a,b| hash[a]<=>hash[b]}.each do |k|
> puts "#{k}\t#{hash[k]}"
> end
>end
>
>
How do you know that hash needs to be a hash? Depending on what enum is,
it could be an array or something else even.

As I understand it, variables first assigned to in a block in 2.0 are
equivalent to
variables in 1.8 with an implicit var = nil before the block, so they're
visible
outside the block. Autovivifying to something other than nil seems more
dangerous
than it would be worth to me.

- Dan



John Carter

12/3/2003 12:53:00 AM

0