(追記 情報に誤りがあります)
上記の質問で、定義部分に大文字を使うと定数、小文字を使うと変数になる、と教えていただいたのですが、
以下のソースで配列の定義に小文字を使うのと大文字を使うのとでは、挙動の違いが発見できなかったのですが、
挙動が同じものと違ってくる定義、メソッド、一体何が違い、どこで判断すれば良いのでしょうか?
挙動が同じ(追記 挙動が違うので注意)
rb
1tmp = gets.chomp 2 3le = ["qwertasdfgzxcvb"] 4ri = ["yuiophjklnm"] 5 6def key_check(key) 7 if le.index(key) 8 true 9 elsif ri.index(key) 10 false 11 end 12end 13 14puts key_check(tmp) 15 16#=> 17# 1 18# left 19 20#=> 21# 0 22# right
rb
1tmp = gets.chomp 2 3LE = ["qwertasdfgzxcvb"] 4RI = ["yuiophjklnm"] 5 6def key_check(key) 7 if LE.index(key) 8 true 9 elsif RI.index(key) 10 false 11 end 12end 13 14puts key_check(tmp) 15 16#=> 17# 1 18# left 19 20#=> 21# 0 22# right 23
挙動が違う
rb
1tmp = gets.chomp 2 3LE = "12345" 4RI = "67890" 5 6def key_check(key) 7 if LE.index(key) 8 "left" 9 elsif RI.index(key) 10 "right" 11 end 12end 13 14puts key_check(tmp) 15# => 16# 1 17# left 18 19#=> 20# 9 21# right
rb
1tmp = gets.chomp 2 3le = "12345" 4ri = "67890" 5 6def key_check(key) 7 if le.index(key) 8 "left" 9 elsif ri.index(key) 10 "right" 11 end 12end 13 14puts key_check(tmp) 15# => 16#test.rb:7:in `key_check': undefined local variable or method `le' for main:Object (NameError)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/23 06:13 編集