Gun*_*ter 1 ruby ruby-on-rails colon
有什么区别:
1)abc:
2):xyz
3)abc :: xyz
4)abc :: xyz
5)abc:xyz
6):abc => xyz
如果我错过了任何一个,请发帖.
1)abc: 它不能独立存在
2):xyz 它是一个象征
:xyz.class
=> Symbol
Run Code Online (Sandbox Code Playgroud)
3)abc::xyz 它代表命名空间
示例代码:
module ABC
class Xyz
def initialize
@size = 400
end
end
end
x = ABC::Xyz.new
Run Code Online (Sandbox Code Playgroud)
4) abc: :xyz
hash = {abc: :xyz} #hash key and value all are symbol.
Run Code Online (Sandbox Code Playgroud)
5) abc: xyz
xyz = "just a test"
hash = {abc: xyz} #hash key is symbol, value is string.
Run Code Online (Sandbox Code Playgroud)
6) :abc => xyz
xyz = "just a test"
hash = {:abc => xyz} # same with (5)? just another representation
Run Code Online (Sandbox Code Playgroud)
7)三元运算符 :
abc = 1
xyz = 2
result = abc > xyz ? abc : xyz
=> result = 2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
957 次 |
| 最近记录: |