値 | 説明
|
---|
a == b | aとbが等しい |
a != b | a と b が等しくない |
a > b | a が b よりも小さい |
a < b | a が b よりも大きい |
a >= b | a が b 以下である |
a <= b | a が b 以上である |
論理演算
値 | | 説明
!a | 否定 |
a && b | aかつb |
a || b | a または b |
三項演算子
```ruby
hoge = true ? 'Yes' : 'No'
=> "Yes"
hoge = false ? 'Yes' : 'No'
=> "No"
<h3>代入演算子</h3>
<table>
<tr><th>値</th><th>説明</td></tr>
<tr><td>a = b</td><td>aにbを代入する</td></tr>
<tr><td>a += b</td><td>a = a + b</td></tr>
<tr><td>a *= b</td><td>a = a * b</td></tr>
<tr><td>a /= b</td><td>a = a / b</td></tr>
<tr><td>a %= b</td><td>a = a % b</td></tr>
<tr><td>a |= b</td><td>a = a | b</td></tr>
</table>
<h2>配列</h2>
<h3>宣言</h3>
```ruby
hoge = [ 1, 2, 3 ]
=> [1, 2, 3]
Array.new(10)
=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
hoge = Array.new( 5, 'hoge')
=> ["hoge", "hoge", "hoge", "hoge", "hoge"]
hoge = %w(apple banana carrot)
=> ["apple", "banana", "carrot"]
配列の取得・追加
追加
hoge << 1
=> [1]
hoge << 2
=> [1, 2]
hoge << 3
=> [1, 2, 3]
取得
hoge[0]
=> 1
hoge.first
=> 1
hoge[1]
=> 2
hoge[2]
=> 3
配列演算子
連結
[1, 2, 3] + [4, 5, 6]
=> [1, 2, 3, 4, 5, 6]
減算
```ruby
[1, 2, 3, 4, 5, 6] - [1, 3, 5]
=> [2, 4, 6]
<h5>乗算</h5>
```ruby
[1, 2, 3] * 3 #各要素を3回分繰り返す
=> [1, 2, 3, 1, 2, 3, 1, 2, 3]
[1, 2, 3] * ','
=> "1,2,3"
ハッシュ
宣言
hoge = { fuga: 'a', bar: 'b' }
=> {:fuga=>"a", :bar=>"b"}
Hash.new
=> {}
hoge = Hash.new(0)
=> {}
hoge['a'] = 1
=> 1
hoge['a']
=> 1
hoge['b']
=> 0