回答編集履歴
2
追記
test
CHANGED
@@ -47,3 +47,31 @@
|
|
47
47
|
p [one,two,three] #=> [1, 2, 3]
|
48
48
|
|
49
49
|
```
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
---
|
56
|
+
|
57
|
+
**追記**
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
インスタンス変数ならば
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
|
67
|
+
%w[one two three].each.with_index(1){|key, val|
|
68
|
+
|
69
|
+
instance_variable_set("@#{key}", val)
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
```
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
でやれます。
|
1
追記
test
CHANGED
@@ -3,6 +3,20 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
動的に変数を作りたいときは代替として[Hash](https://docs.ruby-lang.org/ja/latest/class/Hash.html)を用いるとおおよその場合、うまくいきます。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
|
11
|
+
values = ['one', 'two', 'three'].each.with_index(1).map{|key, val| [key, val] }.to_h
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
p values # => {"one"=>1, "two"=>2, "three"=>3}
|
16
|
+
|
17
|
+
p values["one"] # => 1
|
18
|
+
|
19
|
+
```
|
6
20
|
|
7
21
|
|
8
22
|
|