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.
'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.
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.
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.
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:
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.
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.