Skip to content

Instantly share code, notes, and snippets.

@mizucoffee
Last active December 4, 2019 03:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizucoffee/1208739ae94c4b6b9b1698af34ed466e to your computer and use it in GitHub Desktop.
Save mizucoffee/1208739ae94c4b6b9b1698af34ed466e to your computer and use it in GitHub Desktop.
Rubyの挙動メモ
list = [0, 'a', false]
p list[0] # 括弧で取得
# ==========================
# シンボルリテラルだとシンボルリテラルで、アロー表記だとstringで取得できる
hash = {
a: 'data-a',
"b": 'data-b',
"c" => 'data-c',
:d => 'data-d'
}
# シンボルでアクセス
p hash[:a]
# 文字列だとnilになる
p hash['a']
# シンボルでアクセス
p hash[:b]
# 文字列だとnilになる
p hash['b']
# 文字列でアクセス
p hash['c']
# シンボルだとnilになる
p hash[:c]
# シンボルでアクセス
p hash[:d]
# 文字列だとnilになる
p hash['d']
# これは関数呼び出しになるので不可
# p hash.a
# ==========================
def func
p 'func'
end
# 括弧は省略可
func
# 付けても問題なし
func()
# ==========================
def func2(param1, param2)
p 'func2'
end
# カンマで区切れば括弧は不要
func2 1, 2
# よくある表記も可
func2(1, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment