Skip to content
Tags

setting class or instance variables via console

March 14, 2012

I needed to set instance variables of a class, which did not have a setter method on it, via console.

The method I needed was instance_variable_set used this way:

> MyClass.instance_variable_set(:@my_instance_variable, variable_holding_required_value)

To get the value of the instance variable use

> MyClass.instance_variable_get(:@my_instance_variable)
"value"

Note: class_variable_set and class_variable_get are also be defined.

Note: I could use those methods on a class (although they should be usable also on instances of classes)

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: