Ruby on rails RubyonRails中的冒号放置。
以下两者之间的区别是什么: 1) abc: 2) :xyz 3) abc::xyz 4) abc::xyz 5) abc:xyz 6) :abc=>xyz 如果我错过了任何一个,请发帖子。1)Ruby on rails RubyonRails中的冒号放置。,ruby-on-rails,ruby,colon,Ruby On Rails,Ruby,Colon,以下两者之间的区别是什么: 1) abc: 2) :xyz 3) abc::xyz 4) abc::xyz 5) abc:xyz 6) :abc=>xyz 如果我错过了任何一个,请发帖子。1)abc:它不能独立存在 2) :xyz它是一个符号 :xyz.class => Symbol 3) abc::xyz它表示名称空间 示例代码: module ABC class Xyz def initialize @size = 400 en
abc:
它不能独立存在
2) :xyz
它是一个符号
:xyz.class
=> Symbol
3) abc::xyz
它表示名称空间
示例代码:
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.
xyz = "just a test"
hash = {abc: xyz} #hash key is symbol, value is string.
xyz = "just a test"
hash = {:abc => xyz} # same with (5), just another representation
def my_method(abc: :xyz)
puts abc
end
5) abc:xyz
hash = {abc: :xyz} #hash key and value all are symbol.
xyz = "just a test"
hash = {abc: xyz} #hash key is symbol, value is string.
xyz = "just a test"
hash = {:abc => xyz} # same with (5), just another representation
def my_method(abc: :xyz)
puts abc
end
6) :abc=>xyz
hash = {abc: :xyz} #hash key and value all are symbol.
xyz = "just a test"
hash = {abc: xyz} #hash key is symbol, value is string.
xyz = "just a test"
hash = {:abc => xyz} # same with (5), just another representation
def my_method(abc: :xyz)
puts abc
end
7) 三值运算符:
abc = 1
xyz = 2
result = abc > xyz ? abc : xyz
=> result = 2
1) abc:
它不能独立存在
2) :xyz
它是一个符号
:xyz.class
=> Symbol
3) abc::xyz
它表示名称空间
示例代码:
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.
xyz = "just a test"
hash = {abc: xyz} #hash key is symbol, value is string.
xyz = "just a test"
hash = {:abc => xyz} # same with (5), just another representation
def my_method(abc: :xyz)
puts abc
end
5) abc:xyz
hash = {abc: :xyz} #hash key and value all are symbol.
xyz = "just a test"
hash = {abc: xyz} #hash key is symbol, value is string.
xyz = "just a test"
hash = {:abc => xyz} # same with (5), just another representation
def my_method(abc: :xyz)
puts abc
end
6) :abc=>xyz
hash = {abc: :xyz} #hash key and value all are symbol.
xyz = "just a test"
hash = {abc: xyz} #hash key is symbol, value is string.
xyz = "just a test"
hash = {:abc => xyz} # same with (5), just another representation
def my_method(abc: :xyz)
puts abc
end
7) 三值运算符:
abc = 1
xyz = 2
result = abc > xyz ? abc : xyz
=> result = 2
1) 不能单独使用abc:
。参见4)了解原因
2) :xyz
是一个符号。它与“xyz”非常相似,只是:xyz
是不可变的,而“xyz”是可变的,并且内存中总是只有一个:xyz
(可能这不再是真的,因为Ruby 2.2引入了符号GC?)
3) abc::xyz
很少出现,但是abc::xyz
非常常见。这就是您引用类/模块的内部类/模块/常量Xyz
的方式<代码>:可以但不应该用于调用类/模块方法
如果您真的想了解abc::xyz
的可能用法,那么
abc = Module.new do
def self.xyz; end
end
abc::xyz # Call the module method xyz of the anonymous module.
4) 在Ruby 2.0之前,abc::xyz只能作为传递给方法调用的参数出现。作为参数,这是散列或散列的一部分。以下4个表达式相同:
p abc: :xyz, foo: :bar #=> prints {:abc => :xyz, :foo => :bar}
p(abc: :xyz, foo: :bar) #=> prints {:abc => :xyz, :foo => :bar}
p({abc: :xyz, foo: :bar}) #=> prints {:abc => :xyz, :foo => :bar}
p({:abc => :xyz, :foo => :bar}) #=> prints {:abc => :xyz, :foo => :bar}
作为参数,可以省略散列的大括号。当散列的键是符号时,冒号可以移到符号后面,胖箭头可以省略。这使得哈希看起来更像JSON对象
4.1)Ruby 2.0引入了关键字参数,abc::xyz
可以显示为参数abc
,默认值为:xyz
hash = {abc: :xyz} #hash key and value all are symbol.
xyz = "just a test"
hash = {abc: xyz} #hash key is symbol, value is string.
xyz = "just a test"
hash = {:abc => xyz} # same with (5), just another representation
def my_method(abc: :xyz)
puts abc
end
5) 好的,它与4)相同,只是散列的值是一个局部变量或一个方法调用
6) 与第5条相同)
7) 三元运算符
x = true ? 1 : 0
1) 不能单独使用abc:
。参见4)了解原因
2) :xyz
是一个符号。它与“xyz”非常相似,只是:xyz
是不可变的,而“xyz”是可变的,并且内存中总是只有一个:xyz
(可能这不再是真的,因为Ruby 2.2引入了符号GC?)
3) abc::xyz
很少出现,但是abc::xyz
非常常见。这就是您引用类/模块的内部类/模块/常量Xyz
的方式<代码>:可以但不应该用于调用类/模块方法
如果您真的想了解abc::xyz
的可能用法,那么
abc = Module.new do
def self.xyz; end
end
abc::xyz # Call the module method xyz of the anonymous module.
4) 在Ruby 2.0之前,abc::xyz只能作为传递给方法调用的参数出现。作为参数,这是散列或散列的一部分。以下4个表达式相同:
p abc: :xyz, foo: :bar #=> prints {:abc => :xyz, :foo => :bar}
p(abc: :xyz, foo: :bar) #=> prints {:abc => :xyz, :foo => :bar}
p({abc: :xyz, foo: :bar}) #=> prints {:abc => :xyz, :foo => :bar}
p({:abc => :xyz, :foo => :bar}) #=> prints {:abc => :xyz, :foo => :bar}
作为参数,可以省略散列的大括号。当散列的键是符号时,冒号可以移到符号后面,胖箭头可以省略。这使得哈希看起来更像JSON对象
4.1)Ruby 2.0引入了关键字参数,abc::xyz
可以显示为参数abc
,默认值为:xyz
hash = {abc: :xyz} #hash key and value all are symbol.
xyz = "just a test"
hash = {abc: xyz} #hash key is symbol, value is string.
xyz = "just a test"
hash = {:abc => xyz} # same with (5), just another representation
def my_method(abc: :xyz)
puts abc
end
5) 好的,它与4)相同,只是散列的值是一个局部变量或一个方法调用
6) 与第5条相同)
7) 三元运算符
x = true ? 1 : 0
我收集到1)在散列的上下文中?我收集到1)在散列的上下文中?
abc:xyz
不同于abc:xyz
xyz
在abs:xyz
中应该是一个名为“xyz”的变量是的,你是对的,我应该描述得更清楚更精确。abc:xyz
不同于abc:xyz
xyz
在abs:xyz
中应该是一个名为“xyz”的变量是的,你是对的,我应该更清楚、更精确地描述。