rubyの文字列+コロン(Stack Overflowからパクリ)
1093 ワード
What is the difference between:
1) abc:
2) :xyz
3) abc::xyz
4) abc: :xyz
5) abc: xyz
6) :abc => xyz
Please post if I've missed any of them.
1)
2)
3)
Example code:
4)
5)
6)
7) ternary operator
1) abc:
2) :xyz
3) abc::xyz
4) abc: :xyz
5) abc: xyz
6) :abc => xyz
Please post if I've missed any of them.
1)
abc:
it can't exist independently 2)
:xyz
it is a symbol :xyz.class
=> Symbol
3)
abc::xyz
it represents namespace Example code:
module ABC
class Xyz
def initialize
@size = 400
end
end
end
x = ABC::Xyz.new
4)
abc: :xyz
hash = {abc: :xyz} #hash key and value all are symbol.
5)
abc: xyz
xyz = "just a test"
hash = {abc: xyz} #hash key is symbol, value is string.
6)
:abc => xyz
xyz = "just a test"
hash = {:abc => xyz} # same with (5), just another representation
7) ternary operator
:
abc = 1
xyz = 2
result = abc > xyz ? abc : xyz
=> result = 2