Rubyで関数を使ってみる

今日は関数を使ってRubyコードを書いていきます。
何故関数を使うのか。それは、何度も同じプログラミングを書くのはとてもめんどくさい。でも、関数を使うとその手間が省ける。コードがすっきりする。つまり、幸せになれるんです。
さて、下記のコードを幸せな形にしていきます。

puts("Enter numeric value")
sx = gets()
ix = Integer(sx)
puts("Enter numeric value")
sy = gets()
iy = Integer(sy)
puts("#{ix} + #{iy} = #{ix + iy}") 
puts("#{ix} - #{iy} = #{ix - iy}") 
puts("#{ix} * #{iy} = #{ix * iy}") 
puts("#{ix} / #{iy} = #{ix / iy}") 
puts("#{ix} % #{iy} = #{ix % iy}") 
puts("#{ix} ** #{iy} = #{ix ** iy}") 


関数とはfuncthionという処理のテンプレートのこと。どのプログラム言語にもあるものです。



しかし

Rubyには関数がないっだと!?

なんて、衝撃でしょう。

Rubyには代わりとして、関数的なメソッドがあるそうです。それがこれ↓

def 関数名([仮引数 [, ...]])
  [式
  ]
end

仮引数とは、関数の呼び出しの際に渡ってきた引数を参照する引数のこと。


さて。まずはixとiyをすっきりさせましょう。
数字を読み込む関数read_integerを作ります。

 1:def read_integer()
 2:  puts("Enter numeric value")
 3:  s = gets()
 4:  i = Integer(s)
 5:end

 6:ix = read_integer()
 7:iy = read_integer()

 8:puts("#{ix} + #{iy} = #{ix + iy}") 
 9:puts("#{ix} - #{iy} = #{ix - iy}") 
10:puts("#{ix} * #{iy} = #{ix * iy}") 
11:puts("#{ix} / #{iy} = #{ix / iy}") 
12:puts("#{ix} % #{iy} = #{ix % iy}") 
13:puts("#{ix} ** #{iy} = #{ix ** iy}") 

ixとiyがread_interger関数を呼び出すだけになりました!スッキリ!!
ここで、私が読んでいる参考書では、line4の"i = Integer(s)"の部分が"Integer(s)"となっていました。関数は最後の処理を返すので、"i ="の部分は必要ないのです。

次はline10〜14です。これらの文も似ているので関数にできるはず・・・。

 1:def read_integer()
 2:  puts("Enter numeric value")
 3:  s = gets()
 4:  i = Integer(s)
 5:end
 6:def show_result(operator, x, y, result)
 7:  puts("#{x}" "#{operator}" "#{y}" = "#{result}")

 8:ix = read_integer()
 9:iy = read_integer()
10:show_result("+", ix, iy, ix + iy)
11:show_result("-", ix, iy, ix - iy)
12:show_result("*", ix, iy, ix * iy)
13:show_result("/", ix, iy, ix / iy)
14:show_result("**", ix, iy, ix ** iy)

実行すると・・・

calc_integer.rb:7: syntax error, unexpected '=', expecting ')'
  puts("#{x}" "#{operator}" "#{y}" = "#{result}")
                                    ^
calc_integer.rb:7: syntax error, unexpected ')', expecting keyword_end
calc_integer.rb:15: syntax error, unexpected $end, expecting keyword_end

こんなエラーが・・・。phpの癖で値を""で囲みたくなってしまうorz  puts関数の中はリテラルだから必要ないのにね・・・。これは慣れるまでに時間がかかりそうだ。
さて、「puts("#{x} #{operator} #{y} = #{result}")」を「puts("#{x} #{operator} #{y} = #{result}")」に直して実行しなおした・・・ら・・・

calc_integer.rb:15: syntax error, unexpected $end, expecting keyword_end

こんなエラーが出た。う〜なんでだ・・・

 1:def read_integer()
 2:  puts("Enter numeric value")
 3:  s = gets()
 4:  i = Integer(s)
 5:end
 6:def show_result(operator, x, y, result)
 7:  puts("#{x}" "#{operator}" "#{y}" = "#{result}")

 8:ix = read_integer()
 9:iy = read_integer()
10:show_result("+", ix, iy, ix + iy)
11:show_result("-", ix, iy, ix - iy)
12:show_result("*", ix, iy, ix * iy)
13:show_result("/", ix, iy, ix / iy)
14:show_result("**", ix, iy, ix ** iy)

このコードと2・3分くらい格闘。エラーから文法間違いで、$endだからどっか行の終わりとか構文とかおかしいのだろう。と予測をつけて悩んでました。

で、
気づいたら
line7と8の間に、defの後のendがないだけでした。ふぅ〜。

def read_integer()
  puts("Enter numeric value")
  s = gets()
  i = Integer(s)
end
def show_result(operator, x, y, result)
  puts("#{x} #{operator} #{y} = #{result}")
end

ix = read_integer()
iy = read_integer()
show_result("+", ix, iy, ix + iy)
show_result("-", ix, iy, ix - iy)
show_result("*", ix, iy, ix * iy)
show_result("/", ix, iy, ix / iy)
show_result("**", ix, iy, ix ** iy)

endを付けたして実行したら、できました!実行結果

Enter numeric value
3
Enter numeric value
5
3 + 5 = 8
3 - 5 = -2
3 * 5 = 15
3 / 5 = 0
3 ** 5 = 243