ruby-on-rails - Ruby on Rails 中的冒号放置

标签 ruby-on-rails ruby

有什么区别:

  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
       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) 三元运算符:

abc = 1
xyz = 2
result = abc > xyz ? abc : xyz
=> result = 2

关于ruby-on-rails - Ruby on Rails 中的冒号放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30315021/

相关文章:

ruby-on-rails - Rails-嵌套 content_tag

ruby-on-rails - ActiveRecord 上的作用域关系

ruby-on-rails - 有没有办法使用 ruby​​ gem aws-s3 从 s3 下载文件?

ruby - 连接池返回 Redis 的实例而不是 ConnectionPool

ruby-on-rails - 如果我在我的机器上运行不同版本的 Ruby,我如何测试从 GitHub 克隆的某人的 Rails 应用程序?

ruby - Logstash 中的 ruby​​ 过滤器中的舍入 float

javascript - 如何将 jQuery 代码转换为 Vanilla JS

ruby-on-rails - 编辑和测试现有 Ruby gem(即 Gollum)的最佳方法是什么?

ruby - 如何配置 Rspec 以在 stub 或模拟不存在的方法时引发错误?

ruby-on-rails - 如何在 ruby​​ 中使用 AES 256 ECB PKCS5Padding 加密数据