Trying to figure out array referencing

D

dkmd_nielsen

I'm new to Ruby (love it!). Below is a method that works just fine.
Passed to it as (i) is a two element array [parm,arg]. The method does
a table lookup on @itBlock (an array of two element arrays), and
replace the tables arg value with the arg value passed (if parm is
found in table). I understand the variable referencing that is going
on.

def loadparm(i)
@itBlock.each_index {|k|
j = @itBlock[k] # for each row
next if j[0] != i[0] # if not correct parm
j[1] = i[1].strip! # replace argument
k = j
break # get out of loop
}
end


What I don't understand is why the following does not work. I would
think the variable referencing would still be in tact. The values parm
and arg match the @itBlock row values entering and exiting the loop.
However, the @itBlock row is not updated.

def loadparm(i)
@itBlock.each {|parm,arg|
next if parm != i[0] # if not correct parm
arg = i[1].strip! # replace argument
break # get out of loop
}
end


Would some explain explain what is happening to the pointers and
variable referencing in the second example? I second question would
be...can I accept i as two arguments, as in "def loadparm(x,y)?

Thanks.
 
R

Robert Klemme

I'm new to Ruby (love it!). Below is a method that works just fine.
Passed to it as (i) is a two element array [parm,arg]. The method does
a table lookup on @itBlock (an array of two element arrays), and
replace the tables arg value with the arg value passed (if parm is
found in table). I understand the variable referencing that is going
on.

def loadparm(i)
@itBlock.each_index {|k|
j = @itBlock[k] # for each row
next if j[0] != i[0] # if not correct parm
j[1] = i[1].strip! # replace argument
k = j
break # get out of loop
}
end


What I don't understand is why the following does not work. I would
think the variable referencing would still be in tact. The values parm
and arg match the @itBlock row values entering and exiting the loop.
However, the @itBlock row is not updated.

def loadparm(i)
@itBlock.each {|parm,arg|
next if parm != i[0] # if not correct parm
arg = i[1].strip! # replace argument
break # get out of loop
}
end


Would some explain explain what is happening to the pointers and
variable referencing in the second example? I second question would
be...can I accept i as two arguments, as in "def loadparm(x,y)?

You assign arg which is a block parameter. This has no effect on the
underlying collection (@itBlock). The var is simply overwritten on next
iteration. If @itBlock is a Hash you can use simple lookup, if it's an
Array you can use map!. Note also that usually variable names use
under_scores instead of camelCase. Assuming a Hash your method
basically does this

def loadparam(key, val)
@it_block[key] = val.strip!
end

Kind regards

robert
 
M

Mike

I tried the suggested solution and ran into two minor problems:
1. strip! returns 'nil' if no stripping is done, so the code should
read something like
'@it_block[key] = val.strip()'
or
val.strip!()
@it_block[key] = val

2. the original code conditioned the replacement on the existance of
'key' in the hash,
so the code should probably read:
@it_block[key] = val.strip if @it_block.has_key?(key)

In reference to the last original question, 'def loadparam(key, val)'
is less fragile and more easily understood than 'def loadparam(i)'
where 'i' is required to be an array.
 
D

dkmd_nielsen

Thanks for the suggestions. I will look at redoing the loops using
collect and strip. What I didn't explain is that loadparm() is an
inside loop to an outside loop. but the suggestion of of collect has
made me re-think which loop should be inside and which should be
outside.

I originally implemented a has. Boy! Did that make life easy. But I
need to unload the table in the same sequence it was loaded, and that
can't be ensured with a hash.each.

It seems odd that a function like strip!, which updates a string in
place would return nil if nothing was changed. You would think it
should leave the original value alone.
 
R

Robert Klemme

Thanks for the suggestions. I will look at redoing the loops using
collect and strip. What I didn't explain is that loadparm() is an
inside loop to an outside loop. but the suggestion of of collect has
made me re-think which loop should be inside and which should be
outside.

I originally implemented a has. Boy! Did that make life easy. But I
need to unload the table in the same sequence it was loaded, and that
can't be ensured with a hash.each.

"unload the table"? Maybe you state what original problem you were
trying to solve. Maybe there's an easier solution.
It seems odd that a function like strip!, which updates a string in
place would return nil if nothing was changed. You would think it
should leave the original value alone.

It does - but you don't get it returned so you have a chance to detect
what happened.

Kind regards

robert
 
R

Robert Klemme

Thanks for the suggestions. I will look at redoing the loops using
collect and strip. What I didn't explain is that loadparm() is an
inside loop to an outside loop. but the suggestion of of collect has
made me re-think which loop should be inside and which should be
outside.

I originally implemented a has. Boy! Did that make life easy. But I
need to unload the table in the same sequence it was loaded, and that
can't be ensured with a hash.each.

"unload the table"? Maybe you state what original problem you were
trying to solve. Maybe there's an easier solution.
It seems odd that a function like strip!, which updates a string in
place would return nil if nothing was changed. You would think it
should leave the original value alone.

It does - but you don't get it returned so you have a chance to detect
what happened.

Kind regards

robert
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top