Problem With eval and each_index

M

Michael W. Ryder

I am trying to set a group of variables to values stored in a tab
delimited string. I have no problem splitting the string into an array,
call it e, or in putting the variable names in a second array, call it
v. The problem arises when I try to merge the two together. If I enter:
irb(main):073:0> p v
["name", "street", "city", "state", "zip", "*", "telephone"]
=> nil
irb(main):074:0> p e
["John Doe", "123 Main St", "Anywhere", "US", "01234-5678", "ab123",
"1234567890"]
=> nil
irb(main):075:0> eval "#{v[0]} = e[0]"
=> "John Doe"
irb(main):076:0> p name
"John Doe"
=> nil

it works as I want. But when I try:
irb(main):077:0> v.each_index {|i| eval "#{v} = e[0]"}
=> ["name", "street", "city", "state", "zip", "*", "telephone"]

and then enter: puts zip
it returns with a NameError saying that the variable was undefined.

Am I missing something, or should I be trying a different method to
accomplish this?
 
M

Michael W. Ryder

Michael said:
I am trying to set a group of variables to values stored in a tab
delimited string. I have no problem splitting the string into an array,
call it e, or in putting the variable names in a second array, call it
v. The problem arises when I try to merge the two together. If I enter:
irb(main):073:0> p v
["name", "street", "city", "state", "zip", "*", "telephone"]
=> nil
irb(main):074:0> p e
["John Doe", "123 Main St", "Anywhere", "US", "01234-5678", "ab123",
"1234567890"]
=> nil
irb(main):075:0> eval "#{v[0]} = e[0]"
=> "John Doe"
irb(main):076:0> p name
"John Doe"
=> nil

it works as I want. But when I try:
irb(main):077:0> v.each_index {|i| eval "#{v} = e[0]"}
=> ["name", "street", "city", "state", "zip", "*", "telephone"]

and then enter: puts zip
it returns with a NameError saying that the variable was undefined.

Am I missing something, or should I be trying a different method to
accomplish this?


I just noticed that the above method does set name to "John Doe" which
makes it even more confusing for me.
 
M

matt neuburg

Michael W. Ryder said:
I am trying to set a group of variables to values stored in a tab
delimited string. I have no problem splitting the string into an array,
call it e, or in putting the variable names in a second array, call it
v. The problem arises when I try to merge the two together. If I enter:
irb(main):073:0> p v
["name", "street", "city", "state", "zip", "*", "telephone"]
=> nil
irb(main):074:0> p e
["John Doe", "123 Main St", "Anywhere", "US", "01234-5678", "ab123",
"1234567890"]
=> nil
irb(main):075:0> eval "#{v[0]} = e[0]"
=> "John Doe"
irb(main):076:0> p name
"John Doe"
=> nil

it works as I want. But when I try:
irb(main):077:0> v.each_index {|i| eval "#{v} = e[0]"}
=> ["name", "street", "city", "state", "zip", "*", "telephone"]

and then enter: puts zip
it returns with a NameError saying that the variable was undefined.


Inside a block, local variables are local to the block. So, you're
creating all those local variables and then the block ends and they are
all thrown away.

Why do you need variables called "name" (etc.) anyway? Why not use a
hash, so that h["name"] contains the name ("John Doe"), h["street"]
contains the street, and so on? So:

h = Hash.new
v.zip(e).each {|k,v| h[k]=v}

Notice that even here we define h before we get to the block. That way,
the block's h is our h; it is global to the block.

An even cooler approach is a struct:

Person = Struct.new(*(v.map {|i| i.to_sym}))
p = Person.new(*e)

Now you've got an object, p, where p.name is "John Doe" and so on.

m.
 
C

Christopher Dicely

Michael W. Ryder said:
I am trying to set a group of variables to values stored in a tab
delimited string. =C2=A0I have no problem splitting the string into an a= rray,
call it e, or in putting the variable names in a second array, call it
v. =C2=A0The problem arises when I try to merge the two together. =C2=A0= If I enter:
irb(main):073:0> p v
["name", "street", "city", "state", "zip", "*", "telephone"]
=3D> nil
irb(main):074:0> p e
["John Doe", "123 Main St", "Anywhere", "US", "01234-5678", "ab123",
"1234567890"]
=3D> nil
irb(main):075:0> eval "#{v[0]} =3D e[0]"
=3D> "John Doe"
irb(main):076:0> p name
"John Doe"
=3D> nil

it works as I want. =C2=A0But when I try:
irb(main):077:0> v.each_index {|i| eval "#{v} =3D e[0]"}
=3D> ["name", "street", "city", "state", "zip", "*", "telephone"]

and then enter: puts zip
it returns with a NameError saying that the variable was undefined.


Inside a block, local variables are local to the block. So, you're
creating all those local variables and then the block ends and they are
all thrown away.

Why do you need variables called "name" (etc.) anyway? Why not use a
hash, so that h["name"] contains the name ("John Doe"), h["street"]
contains the street, and so on? So:

h =3D Hash.new
v.zip(e).each {|k,v| h[k]=3Dv}


or, better:

h =3D Hash[*v.zip(e).flatten]
 
M

matt neuburg

Christopher Dicely said:
Michael W. Ryder said:
I am trying to set a group of variables to values stored in a tab
delimited string. I have no problem splitting the string into an array,
call it e, or in putting the variable names in a second array, call it
v. The problem arises when I try to merge the two together. If I enter:
irb(main):073:0> p v
["name", "street", "city", "state", "zip", "*", "telephone"]
=> nil
irb(main):074:0> p e
["John Doe", "123 Main St", "Anywhere", "US", "01234-5678", "ab123",
"1234567890"]
=> nil
irb(main):075:0> eval "#{v[0]} = e[0]"
=> "John Doe"
irb(main):076:0> p name
"John Doe"
=> nil

it works as I want. But when I try:
irb(main):077:0> v.each_index {|i| eval "#{v} = e[0]"}
=> ["name", "street", "city", "state", "zip", "*", "telephone"]

and then enter: puts zip
it returns with a NameError saying that the variable was undefined.


Inside a block, local variables are local to the block. So, you're
creating all those local variables and then the block ends and they are
all thrown away.

Why do you need variables called "name" (etc.) anyway? Why not use a
hash, so that h["name"] contains the name ("John Doe"), h["street"]
contains the street, and so on? So:

h = Hash.new
v.zip(e).each {|k,v| h[k]=v}


or, better:

h = Hash[*v.zip(e).flatten]


Right you are, sorry about that. m.
 
M

Michael W. Ryder

matt said:
Michael W. Ryder said:
I am trying to set a group of variables to values stored in a tab
delimited string. I have no problem splitting the string into an array,
call it e, or in putting the variable names in a second array, call it
v. The problem arises when I try to merge the two together. If I enter:
irb(main):073:0> p v
["name", "street", "city", "state", "zip", "*", "telephone"]
=> nil
irb(main):074:0> p e
["John Doe", "123 Main St", "Anywhere", "US", "01234-5678", "ab123",
"1234567890"]
=> nil
irb(main):075:0> eval "#{v[0]} = e[0]"
=> "John Doe"
irb(main):076:0> p name
"John Doe"
=> nil

it works as I want. But when I try:
irb(main):077:0> v.each_index {|i| eval "#{v} = e[0]"}
=> ["name", "street", "city", "state", "zip", "*", "telephone"]

and then enter: puts zip
it returns with a NameError saying that the variable was undefined.


Inside a block, local variables are local to the block. So, you're
creating all those local variables and then the block ends and they are
all thrown away.


I can see that if I create the variable before running the method the
variable is set even after the method ends. Which of course brings up
the question of why is the change visible outside of the block? This
behavior is almost like a mix of C and Basic. You have to define a
variable in C before you can use it and in Basic once a variable is set
it is always visible.
Why do you need variables called "name" (etc.) anyway? Why not use a
hash, so that h["name"] contains the name ("John Doe"), h["street"]
contains the street, and so on? So:

I have been programming in Business Basic for over 20 years and am used
to reading a file with a statement like:
Read(1,Key="1234")Name$,Street$,City$,State$,Zip$,*,Telephone$
and then using those variables. I was trying to use what I am familiar
with while learning Ruby. I realize in Ruby I would have to do read the
string of data in, split it on the separator, and then assign it to the
proper variables. Using a hash was an option but seemed to be more
typing to use as I would have to enter: puts h["name"] vs puts name.

h = Hash.new
v.zip(e).each {|k,v| h[k]=v}

Notice that even here we define h before we get to the block. That way,
the block's h is our h; it is global to the block.

An even cooler approach is a struct:

Person = Struct.new(*(v.map {|i| i.to_sym}))
p = Person.new(*e)

Now you've got an object, p, where p.name is "John Doe" and so on.

I might try this for some things as it seems more logical than the hash
for what I normally do. Thanks for the tip. I hadn't got into
structures yet.
 
C

Christopher Dicely

matt said:
Michael W. Ryder said:
I am trying to set a group of variables to values stored in a tab
delimited string. =C2=A0I have no problem splitting the string into an = array,
call it e, or in putting the variable names in a second array, call it
v. =C2=A0The problem arises when I try to merge the two together. =C2= =A0If I enter:
irb(main):073:0> p v
["name", "street", "city", "state", "zip", "*", "telephone"]
=3D> nil
irb(main):074:0> p e
["John Doe", "123 Main St", "Anywhere", "US", "01234-5678", "ab123",
"1234567890"]
=3D> nil
irb(main):075:0> eval "#{v[0]} =3D e[0]"
=3D> "John Doe"
irb(main):076:0> p name
"John Doe"
=3D> nil

it works as I want. =C2=A0But when I try:
irb(main):077:0> v.each_index {|i| eval "#{v} =3D e[0]"}
=3D> ["name", "street", "city", "state", "zip", "*", "telephone"]

and then enter: puts zip
it returns with a NameError saying that the variable was undefined.


Inside a block, local variables are local to the block. So, you're
creating all those local variables and then the block ends and they are
all thrown away.


I can see that if I create the variable before running the method the
variable is set even after the method ends. =C2=A0Which of course brings = up the
question of why is the change visible outside of the block?


Ruby blocks are closures, and they therefore capture the environment
where they are created. So if a local variable exists in that
environment when the block is created, the block has access to it, and
changes will affect the externally-created local variable. But new
local variables introduced in the block are local to the block. In
essence, the environment of the block is subordinate to the
environment in which it is created.
Why do you need variables called "name" (etc.) anyway? Why not use a
hash, so that h["name"] contains the name ("John Doe"), h["street"]
contains the street, and so on? So:

I have been programming in Business Basic for over 20 years and am used t= o
reading a file with a statement like:
=C2=A0Read(1,Key=3D"1234")Name$,Street$,City$,State$,Zip$,*,Telephone$
and then using those variables.

Assuming you have a "keyed_read" method defined properly, you can easily do=
:

name, street, city, state, zip, _, telephone =3D
data_file.keyed_read:)key=3D>"1234")

But the thing that is harder is dynamically assigning variable names
based on a array provided. Using hashes or structs are the more
idiomatic Ruby ways of doing something similar to that. Its possible
to do something very close to what you were attempting with instance
variables using Object#instance_variable_set.

irb(main):036:0> v.zip(e).each { |var, val|
irb(main):037:1* begin
irb(main):038:2* self.instance_variable_set(("@"+var).to_sym,val)
irb(main):039:2> rescue
irb(main):040:2> end
irb(main):041:1> }
=3D> [["name", "John Doe"], ["street", "123 Main St"], ["city",
"Anywhere"], ["state", "US"], ["zip", "01234-5678"], ["*", "ab123"],
["telephone", "1234567890"]]
irb(main):042:0> @name
=3D> "John Doe"

So if you want to do it that way, you can. (Note, I'm assuming that
the '*' is a special symbol for a value you don't want to store, if
not, you'll have to add some logic to map that to a valid instance
variable name.)
 
C

Christopher Dicely

That was not my understanding. =C2=A0How does one return a block from a f= unction?
=C2=A0(That's not a rhetorical question; I'm eager to learn.)

While block are closures, they aren't first-class. You can't return a
block, because its not a normal Ruby object. You can return an
instance of class Proc built from a particular block, though, and use
it outside the method in which it was created.

For example:

irb(main):001:0> def counter(seed=3D0)
irb(main):002:1> lambda { seed +=3D 1 }
irb(main):003:1> end
=3D> nil
irb(main):004:0> c =3D counter(0)
=3D> #<Proc:0x02bcfaf0@(irb):2>
irb(main):005:0> c.call
=3D> 1
irb(main):006:0> c.call
=3D> 2
 
M

Michael W. Ryder

Christopher said:
matt said:
I am trying to set a group of variables to values stored in a tab
delimited string. I have no problem splitting the string into an array,
call it e, or in putting the variable names in a second array, call it
v. The problem arises when I try to merge the two together. If I enter:
irb(main):073:0> p v
["name", "street", "city", "state", "zip", "*", "telephone"]
=> nil
irb(main):074:0> p e
["John Doe", "123 Main St", "Anywhere", "US", "01234-5678", "ab123",
"1234567890"]
=> nil
irb(main):075:0> eval "#{v[0]} = e[0]"
=> "John Doe"
irb(main):076:0> p name
"John Doe"
=> nil

it works as I want. But when I try:
irb(main):077:0> v.each_index {|i| eval "#{v} = e[0]"}
=> ["name", "street", "city", "state", "zip", "*", "telephone"]

and then enter: puts zip
it returns with a NameError saying that the variable was undefined.
Inside a block, local variables are local to the block. So, you're
creating all those local variables and then the block ends and they are
all thrown away.

I can see that if I create the variable before running the method the
variable is set even after the method ends. Which of course brings up the
question of why is the change visible outside of the block?


Ruby blocks are closures, and they therefore capture the environment
where they are created. So if a local variable exists in that
environment when the block is created, the block has access to it, and
changes will affect the externally-created local variable. But new
local variables introduced in the block are local to the block. In
essence, the environment of the block is subordinate to the
environment in which it is created.


This is what is confusing to me. If I use a variable in a function and
then call another function which uses the same name C does not change
the first instance when I return from the second function unless it was
part of the call. In Basic if I make a change or create a new variable
it is visible everywhere afterwards. Ruby seems to be doing something
like a mix of the two where if the variable is undefined entering the
block it stays undefined outside the block, but if it was defined before
entering the block it is changed to the new value on exit.
Why do you need variables called "name" (etc.) anyway? Why not use a
hash, so that h["name"] contains the name ("John Doe"), h["street"]
contains the street, and so on? So:
I have been programming in Business Basic for over 20 years and am used to
reading a file with a statement like:
Read(1,Key="1234")Name$,Street$,City$,State$,Zip$,*,Telephone$
and then using those variables.

Assuming you have a "keyed_read" method defined properly, you can easily do:

name, street, city, state, zip, _, telephone =
data_file.keyed_read:)key=>"1234")

But the thing that is harder is dynamically assigning variable names
based on a array provided. Using hashes or structs are the more
idiomatic Ruby ways of doing something similar to that. Its possible
to do something very close to what you were attempting with instance
variables using Object#instance_variable_set.
Part of what brought this up is that I work with a lot of Excel
spreadsheets of information from various clients. I export the data
into tab separated records. I then break up the string and assign it to
the various variables for further processing. Currently in Business
Basic I have to find the position of the first tab, set the first
variable to the data up to that position, chop off the first part of the
string up to the tab and repeat. In Ruby it looks like I can just split
the string and combine it with the variable names in a couple of steps.

irb(main):036:0> v.zip(e).each { |var, val|
irb(main):037:1* begin
irb(main):038:2* self.instance_variable_set(("@"+var).to_sym,val)
irb(main):039:2> rescue
irb(main):040:2> end
irb(main):041:1> }
=> [["name", "John Doe"], ["street", "123 Main St"], ["city",
"Anywhere"], ["state", "US"], ["zip", "01234-5678"], ["*", "ab123"],
["telephone", "1234567890"]]
irb(main):042:0> @name
=> "John Doe"

I will play with this and see how it compares to the other suggestions
from the group.
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top