如何修改名称已赋予的变量的值

时间:2020-12-06 23:39:18

I wish to have a method that takes in a string, and then updates a variable with the name of that string. This is an example of my attempt:

我希望有一个接受字符串的方法,然后使用该字符串的名称更新变量。这是我尝试的一个例子:

@other_class = OtherClass.new()

def change_variable(variable_string)
  self.@other_class.send.variable_string += 1
end

I am getting the error:

我收到错误:

syntax error, unexpected tIVAR

语法错误,意外的tIVAR

with the pointer just before 'send' in the method above. Does anyone have any suggestions to make this work?

使用上面方法中“发送”之前的指针。有没有人有任何建议让这项工作?

2 个解决方案

#1


1  

The immediate syntatic error is that you're using self and @ wrongly.

直接的合成错误是你正在使用self和@错误。

Either one is fine but not in conjunction with each other.

任何一个都可以,但不能相互结合。

So in your case self.other_class.send... would be fine but then you cant declare it as @. As would @ be but then you cant do self.

所以在你的情况下,self.other_class.send ...会很好但是你不能把它声明为@。就像@那样但是你不能做自我。

These are meant to do different things and these are that

这些是为了做不同的事情,而这些是

@ is an instance variable and so is self but the difference is that using @ calls the attribute other_class directly as to where self calls the method other_class.

@是一个实例变量,因此是self,但不同之处在于使用@直接调用属性other_class,以及self调用方法other_class的位置。

So @ is both a getter and setter in one so you can do

所以@既是一个吸气剂又是一个吸气剂,所以你可以做到

@other_class = my_milk_man as to where

@other_class = my_milk_man至于哪里

self.other_class -> self.other_class (as getter),

self.other_class - > self.other_class(作为getter),

self.other_class = my_milk_man -> self.other_class= (as setter).

self.other_class = my_milk_man - > self.other_class =(作为setter)。

#2


2  

You probably want instance_variable_set http://ruby-doc.org/core-2.0/Object.html#method-i-instance_variable_set

你可能想要instance_variable_set http://ruby-doc.org/core-2.0/Object.html#method-i-instance_variable_set

The syntax using your variables is I think:

我认为使用变量的语法是:

other_var = ('@' + variable_string).to_sym

@other_class.instance_variable_set( other_var, @other_class.instance_variable_get( other_var ) + 1 )

#1


1  

The immediate syntatic error is that you're using self and @ wrongly.

直接的合成错误是你正在使用self和@错误。

Either one is fine but not in conjunction with each other.

任何一个都可以,但不能相互结合。

So in your case self.other_class.send... would be fine but then you cant declare it as @. As would @ be but then you cant do self.

所以在你的情况下,self.other_class.send ...会很好但是你不能把它声明为@。就像@那样但是你不能做自我。

These are meant to do different things and these are that

这些是为了做不同的事情,而这些是

@ is an instance variable and so is self but the difference is that using @ calls the attribute other_class directly as to where self calls the method other_class.

@是一个实例变量,因此是self,但不同之处在于使用@直接调用属性other_class,以及self调用方法other_class的位置。

So @ is both a getter and setter in one so you can do

所以@既是一个吸气剂又是一个吸气剂,所以你可以做到

@other_class = my_milk_man as to where

@other_class = my_milk_man至于哪里

self.other_class -> self.other_class (as getter),

self.other_class - > self.other_class(作为getter),

self.other_class = my_milk_man -> self.other_class= (as setter).

self.other_class = my_milk_man - > self.other_class =(作为setter)。

#2


2  

You probably want instance_variable_set http://ruby-doc.org/core-2.0/Object.html#method-i-instance_variable_set

你可能想要instance_variable_set http://ruby-doc.org/core-2.0/Object.html#method-i-instance_variable_set

The syntax using your variables is I think:

我认为使用变量的语法是:

other_var = ('@' + variable_string).to_sym

@other_class.instance_variable_set( other_var, @other_class.instance_variable_get( other_var ) + 1 )