LoginSignup
0
0

More than 3 years have passed since last update.

Ruby 配列やハッシュオブジェクトについてのざっくりメモ

Last updated at Posted at 2020-09-21

Rubyの配列やハッシュオブジェクトについてのメモです。
配列の基礎的なことは、知っているのでruby特有の書き方をメモしていきたいと思います。

配列に要素を追加するとき

num = ["one"]
num << "two"
p num  # ["one", "two"]

ハッシュを使っての、キーと値の定義

numKey = { one: 1, two: 2 }
p numKey  # {:one=>1, :two=>2}
p numKey[:two]  # 2

%記法で配列の定義

# numbers = ["one", "two", "three"] 同じ意味
numbers = %W( one two three )
p numbers  # ["one", "two", "three"]

配列の中の要素を一つずつ取り出す

%w[one two three].each do |num|
    p num
end
# "one"
# "two"
# "three"

元の配列に値を追加して、新しい配列を返す。

nums = %w[one two three four].map{ |num| "数字: #{num}" }
p nums  # ["数字: one", "数字: two", "数字: three", "数字: four"]
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