LoginSignup
0
0

More than 5 years have passed since last update.

Methodの代わりにStringを使ってinstance variableをsetする

Last updated at Posted at 2013-02-08

いまいち何と説明するのか分からないけど
str = 'title'のtitleを使って@blog.title = 'bacon'みたいな事がしたい。

instance_variable_setを使う

str = 'title'
@blog.instance_variable_set("@#{str}".to_sym, 'bacon')

で出来るんだけど、他にやり方ないかなぁ。

getも出来る

str = 'title'
@blog.instance_variavle_get("@#{str}".to_sym)

追記

コメントで頂きました

@blog.send("#{str}=", 'bacon')

こっちのが良いですね!

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0