読者です 読者をやめる 読者になる 読者になる

Ruby文法入門まとめ

hello world

printf "hello world!" # 末尾改行なし => hello world!
print "hello world!"  # 末尾改行なし => hello world!
puts "hello world!"   # 末尾改行あり => hello world!¥n
p "hello world!"      # 形式がわかる => "hello world!"¥n

変数

lang = "Ruby"  # 変数
lang = "Java"  # 書き換えOK

LANG = "Ruby"  # 定数(先頭大文字)
LANG = "Java"  # 書き換え不可。エラーとなる

数値

http://ruby-doc.org/core-2.1.3/Numeric.html(v2.1.3)
http://ruby-doc.org/core-2.1.3/Float.html(v2.1.3)

num = 10    # integer => 10
num = 10.0  # float => 10.0

num = 10.to_f    # float変換 => 10.0
num = 10.9.to_i  # integer変換(切り捨て) => 10

num = 100_000    # "_"は無視 => 100000

num = 10.56.round  # 四捨五入 => 11
num = 10.32.ceil   # 切り上げ => 11
num = 10.78.floor  # 切り捨て => 10
num = (-10.5).abs  # 絶対値 => 10.5

num = 10 + 5  # 加算 => 15
num = 10 - 5  # 減算 => 5
num = 10 * 5  # 乗算 => 50
num = 10 / 5  # 除算 => 2
num = 10 % 5  # 余り => 0

文字列

http://www.ruby-doc.org/core-2.1.3/String.html(v2.1.3)

a = "Ruby"  # string => Ruby
a = 'Ruby'  # string => Ruby

b = "I like #{a}"  # 変数置換 => I like Ruby
b = 'I like #{a}'  # 変数置換しない => I like #{a}

c = "10" + "5"  # 結合 => 105
c = "10" << "5" # 末尾に結合 => 105

d = "I like Ruby"
d.length    # 文字列の長さ => 11
d.reverse   # 文字列を逆転 => ybuR ekil I
d.upcase    # 大文字に変換 => I LIKE RUBY
d.upcase!   # 大文字に変換して変数の値も変更 => I LIKE RUBY
d.downcase  # 小文字に変換 => i like ruby
d.downcase! # 小文字に変換 => i like ruby

e = "I like Ruby"
e.empty? # 空かどうか => false

f = "I like Ruby"
f[2]     # 3番目の文字を取得 => l
f[2, 4]  # 3番目から4文字の文字を取得 => like
f[-1]    # 最後尾の文字を取得 => y

配列

http://www.ruby-doc.org/core-2.1.3/Array.html(v2.1.3)

array = [2, 5, 4, 1, 3]

array[0]              # 1番目 => 2
array[-1]             # 最後尾 => 3
array[1..3]           # 2〜4番目 => [5, 4, 1]
array[1...3]          # 2〜3番目 => [5, 4]
array.length          # 配列の長さ => 5
array.reverse         # 配列を逆順に => [3, 1, 4, 5, 2]
array.sort            # 配列を小さい順にソート => [1, 2, 3, 4, 5]
array.sort.reverse    # 配列を大きい順にソート => [5, 4, 3, 2, 1]
p array << 6          # 末尾に追加 => [2, 5, 4, 1, 3, 6]

ハッシュ

http://www.ruby-doc.org/core-2.1.3/Hash.html(v2.1.3)

hash = {"name" => "kimura", "lang" => "ruby"}  # {key1 => value1, key2 => value2, ...}
hash["name"]  # キー名でアクセス => kimura

hash = {:name => "kimura", :lang => "ruby"}  # {symbol1 => value1, symbol2 => value2, ...}
hash = {name: "kimura", lang: "ruby"}        # {symbol1: value1, symbol2: value2, ...} ※v1.9から
hash[:name] # シンボル名でアクセス => kimura

p hash.size             # 要素数 => 2
p hash.keys             # キーの一覧 => [:name, :lang]
p hash.values           # 値の一覧 => ["kimura", "ruby"]
p hash.has_key?(:name)  # キーが存在するか => true

if,case

color = "blue"

# if
if color == "red"
  puts "赤です"
elsif color == "green"
  puts "緑です"
elsif color == "blue"
  puts "青です"
end

puts "赤です" if color == "red"
puts "緑です" if color == "green"
puts "青です" if color == "blue"

# unless
unless color == "red"
  puts "赤ではありません"
end

puts "赤ではありません" unless color == "red"

# case
case color
when "red"
  puts "赤です"
when "green"
  puts "緑です"
when "blue"
  puts "青です"
when "pink", "yellow"
  puts "ピンクか緑です"
else
  puts "知らない色です"
end

# 条件演算子
puts color == "blue" ? "青です" : "青ではありません"

ループ

# ----- while -----
i = 0
while i < 5
  puts i
  i += 1
end

# ----- until -----
j = 0
until j > 5
  puts j
  j += 1
end

# ----- for -----
for lang in ["Ruby", "PHP", "Perl", "Python", "Java"] do
  puts "I like #{lang}!"
end

for k in (1..10)
  puts k
end

# ----- times -----
3.times do
  puts "I like Ruby!"
end

3.times { puts "I like Ruby!" }

3.times do |l| # o始まり
  puts "#{l}"
end

3.times {|l| puts "#{l}" }

# ----- each -----
["Ruby", "PHP", "Perl", "Python", "Java"].each do |lang|
  puts "I like #{lang}!"
end

{ lang1: "Ruby", lang2: "PHP", lang3: "Perl", lang4: "Python", lang5: "Java" }.each do |index, lang|
  puts "#{index}: #{lang}!"
end

# ----- loop -----
m = 0
loop do
  puts m += 1
  break if m > 5
end