Iterate attribute assignment from hash

I have a hash stored in JSON. I'm setting myvuln to a new instance of a
class and then assigning the value from the hash to the attribute. This
works:

myvuln.fixtext = vuln_data[vid]['fixtext']
puts "Let's fix #{myvuln.vid}, it needs #{myvuln.fixtext}."

Now I'm trying to iterate through all the keys in the hash and assign them.
It does not work, even though all attributes have the setter and getter
methods.

vuln_data[vid].each do |key, value|
   myvuln.key = vuln_data[vid][key]
end

I'm assuming operator error, I just don't see what it is.

Thanks!

Leam

···

--
Mind on a Mission <http://leamhall.blogspot.com/>

'key' isn't being evaluated when you use it in 'myvuln.key'. It's accessing
the method named key.

What you likely want to do is use #instance_variable_set:

```
vuln_data.each {|k,v| myvuls.instance_variable_set("@#{k}", v) } ```
Footnote, you could probably use #send also since the accessors are setup.

···

On Mon, Jul 20, 2015 at 4:09 PM, leam hall <leamhall@gmail.com> wrote:

I have a hash stored in JSON. I'm setting myvuln to a new instance of a
class and then assigning the value from the hash to the attribute. This
works:

myvuln.fixtext = vuln_data[vid]['fixtext']
puts "Let's fix #{myvuln.vid}, it needs #{myvuln.fixtext}."

Now I'm trying to iterate through all the keys in the hash and assign
them. It does not work, even though all attributes have the setter and
getter methods.

vuln_data[vid].each do |key, value|
   myvuln.key = vuln_data[vid][key]
end

I'm assuming operator error, I just don't see what it is.

Thanks!

Leam

--
Mind on a Mission <http://leamhall.blogspot.com/&gt;

--
Incoherently,
Ricky Ng

Ricky, thanks! That worked.

Leam

···

On Mon, Jul 20, 2015 at 5:23 PM, Ricky Ng <dummey@gmail.com> wrote:

'key' isn't being evaluated when you use it in 'myvuln.key'. It's
accessing the method named key.

What you likely want to do is use #instance_variable_set:

```
vuln_data.each {|k,v| myvuls.instance_variable_set("@#{k}", v) } ```
Footnote, you could probably use #send also since the accessors are setup.

On Mon, Jul 20, 2015 at 4:09 PM, leam hall <leamhall@gmail.com> wrote:

I have a hash stored in JSON. I'm setting myvuln to a new instance of a
class and then assigning the value from the hash to the attribute. This
works:

myvuln.fixtext = vuln_data[vid]['fixtext']
puts "Let's fix #{myvuln.vid}, it needs #{myvuln.fixtext}."

Now I'm trying to iterate through all the keys in the hash and assign
them. It does not work, even though all attributes have the setter and
getter methods.

vuln_data[vid].each do |key, value|
   myvuln.key = vuln_data[vid][key]
end

I'm assuming operator error, I just don't see what it is.

Thanks!

Leam

--
Mind on a Mission <http://leamhall.blogspot.com/&gt;

--
Incoherently,
Ricky Ng

--
Mind on a Mission <http://leamhall.blogspot.com/&gt;

Hi guys !

Why it should not being evaluated ?

Thanks :slight_smile:

···

On Mon, Jul 20, 2015 at 11:27 PM, leam hall <leamhall@gmail.com> wrote:

Ricky, thanks! That worked.

Leam

On Mon, Jul 20, 2015 at 5:23 PM, Ricky Ng <dummey@gmail.com> wrote:

'key' isn't being evaluated when you use it in 'myvuln.key'. It's
accessing the method named key.

What you likely want to do is use #instance_variable_set:

```
vuln_data.each {|k,v| myvuls.instance_variable_set("@#{k}", v) } ```
Footnote, you could probably use #send also since the accessors are setup.

On Mon, Jul 20, 2015 at 4:09 PM, leam hall <leamhall@gmail.com> wrote:

I have a hash stored in JSON. I'm setting myvuln to a new instance of a
class and then assigning the value from the hash to the attribute. This
works:

myvuln.fixtext = vuln_data[vid]['fixtext']
puts "Let's fix #{myvuln.vid}, it needs #{myvuln.fixtext}."

Now I'm trying to iterate through all the keys in the hash and assign
them. It does not work, even though all attributes have the setter and
getter methods.

vuln_data[vid].each do |key, value|
   myvuln.key = vuln_data[vid][key]
end

I'm assuming operator error, I just don't see what it is.

Thanks!

Leam

--
Mind on a Mission <http://leamhall.blogspot.com/&gt;

--
Incoherently,
Ricky Ng

--
Mind on a Mission <http://leamhall.blogspot.com/&gt;

Hi guys !

Why it should not being evaluated ?

Thanks :slight_smile:

Perhaps an example will help:

irb2.2.2> length = 'class'
#2.2.2 => "class"
irb2.2.2> "foo".length
#2.2.2 => 3
irb2.2.2> "foo".send(length)
#2.2.2 => String

Note that in the first case .length doesn't get "evaluated". Ruby just sends that method to "foo" and the result is 3. However, when using the #send method, its argument is evaluated (as a local variable) and the value "class" is the method sent to "foo".

-Rob

Rob Biedenharn
rob.biedenharn@gmail.com

···

On 2015-Jul-22, at 05:54 , Mehdi Farsi <mehdifarsi.pro@gmail.com> wrote:

On Mon, Jul 20, 2015 at 11:27 PM, leam hall <leamhall@gmail.com <mailto:leamhall@gmail.com>> wrote:
Ricky, thanks! That worked.

Leam

On Mon, Jul 20, 2015 at 5:23 PM, Ricky Ng <dummey@gmail.com <mailto:dummey@gmail.com>> wrote:
'key' isn't being evaluated when you use it in 'myvuln.key'. It's accessing the method named key.

What you likely want to do is use #instance_variable_set:

vuln_data.each {|k,v| myvuls.instance_variable_set("@#{k}", v) }

Footnote, you could probably use #send also since the accessors are setup.

On Mon, Jul 20, 2015 at 4:09 PM, leam hall <leamhall@gmail.com <mailto:leamhall@gmail.com>> wrote:

I have a hash stored in JSON. I'm setting myvuln to a new instance of a class and then assigning the value from the hash to the attribute. This works:

myvuln.fixtext = vuln_data[vid]['fixtext']
puts "Let's fix #{myvuln.vid}, it needs #{myvuln.fixtext}."

Now I'm trying to iterate through all the keys in the hash and assign them. It does not work, even though all attributes have the setter and getter methods.

vuln_data[vid].each do |key, value|
   myvuln.key = vuln_data[vid][key]
end

I'm assuming operator error, I just don't see what it is.

Thanks!

Leam

--
Mind on a Mission <http://leamhall.blogspot.com/&gt;

--
Incoherently,
Ricky Ng

--
Mind on a Mission <http://leamhall.blogspot.com/&gt;

Thanks Rob ! I got it. :wink:

···

On Wed, Jul 22, 2015 at 6:06 PM, Rob Biedenharn <rob.biedenharn@gmail.com> wrote:

On 2015-Jul-22, at 05:54 , Mehdi Farsi <mehdifarsi.pro@gmail.com> wrote:

Hi guys !

Why it should not being evaluated ?

Thanks :slight_smile:

Perhaps an example will help:

irb2.2.2> length = 'class'
#2.2.2 => "class"
irb2.2.2> "foo".length
#2.2.2 => 3
irb2.2.2> "foo".send(length)
#2.2.2 => String

Note that in the first case .length doesn't get "evaluated". Ruby just
sends that method to "foo" and the result is 3. However, when using the
#send method, its argument *is* evaluated (as a local variable) and the
value "class" is the method sent to "foo".

-Rob

Rob Biedenharn
rob.biedenharn@gmail.com

On Mon, Jul 20, 2015 at 11:27 PM, leam hall <leamhall@gmail.com> wrote:

Ricky, thanks! That worked.

Leam

On Mon, Jul 20, 2015 at 5:23 PM, Ricky Ng <dummey@gmail.com> wrote:

'key' isn't being evaluated when you use it in 'myvuln.key'. It's
accessing the method named key.

What you likely want to do is use #instance_variable_set:

```
vuln_data.each {|k,v| myvuls.instance_variable_set("@#{k}", v) } ```
Footnote, you could probably use #send also since the accessors are setup.

On Mon, Jul 20, 2015 at 4:09 PM, leam hall <leamhall@gmail.com> wrote:

I have a hash stored in JSON. I'm setting myvuln to a new instance of a
class and then assigning the value from the hash to the attribute. This
works:

myvuln.fixtext = vuln_data[vid]['fixtext']
puts "Let's fix #{myvuln.vid}, it needs #{myvuln.fixtext}."

Now I'm trying to iterate through all the keys in the hash and assign
them. It does not work, even though all attributes have the setter and
getter methods.

vuln_data[vid].each do |key, value|
   myvuln.key = vuln_data[vid][key]
end

I'm assuming operator error, I just don't see what it is.

Thanks!

Leam

--
Mind on a Mission <http://leamhall.blogspot.com/&gt;

--
Incoherently,
Ricky Ng

--
Mind on a Mission <http://leamhall.blogspot.com/&gt;