[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:04238] Re: @ variables not updated within method?

From: Hugh Sasse Staff Elec Eng <hgs@...>
Date: 2000-07-27 17:55:51 UTC
List: ruby-talk #4238
On Thu, 27 Jul 2000, [iso-8859-1] Aleksi Niemelwrote:

> Hugh says:
> > I have a program, too large to post here, and an object in it with a
> > variable:
> 
> Well, you could try to make a short example which does not work. Usually
> this makes it easier to other to help you out and as a side effect many
> problems magically vanish :) - at least that happens me all the time.

	True, but before I strat chopping the thing to bits I thought
	I'd ask...
> 
> > Are there any catches I should be looking out for when doing 
> > this sort of thing?
> > @chunks = Hash.new([])
> > It is a hash of arrays.  
> 
> This is not a hash of arrays. @chunks refers to a normal array which has a
> default value an empty array.
> 
Hmmm, I thought it had a default value *for each key* of an empty
array....

>   ruby -e 'h=Hash.new([]); h[1]="bar"; p h["foo"]; p h;'
>   []
>   {1=>"bar"}
> 
> 	- Aleksi
> 
> 
with this:
		# [...lots trimmed...]
                print "going through @state.in_chunks\n"
                @state.in_chunks.each do
                    |chnk|
                    print "chnk is #{chnk.inspect}\n"
                    print "@chunks.type is #{@chunks.type}\n"
                    print "@chunks is #{@chunks.inspect}\n"
                    print "@chunks[chnk].type is #{@chunks[chnk].type}\n"
                    print "@chunks[chnk] is #{@chunks[chnk].inspect}\n"
                    @chunks[chnk].push(@line_index)
                    print "@chunks.type is #{@chunks.type}\n"
                    print "@chunks is #{@chunks.inspect}\n"
                    print "@chunks[chnk].type is #{@chunks[chnk].type}\n"
                    print "@chunks[chnk] is #{@chunks[chnk].inspect}\n"
                end
		# [...lots trimmed...]

I get output like:

going through @state.in_chunks
chnk is "introduction"
@chunks.type is Hash
@chunks is {}
@chunks[chnk].type is Array
@chunks[chnk] is [0, 1, 2, 3, 4, 5, 6]
@chunks.type is Hash
@chunks is {}
@chunks[chnk].type is Array
@chunks[chnk] is [0, 1, 2, 3, 4, 5, 6, 7]

Which I find pretty odd.
	Hugh
	hgs@dmu.ac.uk


In This Thread

Prev Next