0
0

More than 1 year has passed since last update.

WEB開発をやり始めてみる(Ruby:ハッシュ)

Last updated at Posted at 2022-10-16

やったこと

### ハッシュの作り方
scores = {luke: 100, jack: 90, robert: 70}
puts scores 
{:luke=>100, :jack=>90, :robert=>70}

### eachを使った変数の代入と出力
scores.each { |k, v| puts v}
100
90
70

### eachを使った変数の代入と出力
scores.each { |k, v| puts "#{k}, #{v}" }
luke, 100
jack, 90
robert, 70

## if文を使った点数の指定以上だけを出力
scores.each { |k, v|
     if v >= 80
       puts "#{k}, #{v}"
     end
   }

luke, 100
jack, 90

 
scores.keys
 => [:luke, :jack, :robert] 
### each文で使う場合
scores.each_key do { key }
end


scores.values
 => [100, 90, 70] 
### each文で使う場合
scores.each_value do { value }
end

scores.has_key?(:luke)
 => true 
 scores.has_key?(:takahashi)
 => false 

scores.size
 => 3 
0
0
0

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