:: 有什么作用? [英] What does :: do?

查看:59
本文介绍了:: 有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些正在修改的继承代码.但是,我看到了一些奇怪的东西(对我来说).

I have some inherited code that I am modifying. However, I am seeing something strange(to me).

我看到一些这样的代码:

I see some code like this:

::User.find_by_email(params[:user][:email]).update_attributes(:mag => 1)

我从未见过这样的东西(我是 Ruby on Rails 的新手).这有什么作用,为什么我的 User.find_by_email(params[:user][:email]).update_attributes(:mag => 1) 不起作用?该错误说明了有关 User 常量的内容.

I have never seen something like this(I am new to Ruby on Rails). What does this do and why doesn't my User.find_by_email(params[:user][:email]).update_attributes(:mag => 1) work? The error says something about the User constant.

如果有帮助,我正在使用 Rails 2.3.5.

I am using Rails 2.3.5 if that helps.

推荐答案

:: 是一个作用域解析操作符,它有效的意思是在命名空间中",所以 ActiveRecord::Base 的意思是Base,在ActiveRecord"的命名空间中

:: is a scope resolution operator, it effectively means "in the namespace", so ActiveRecord::Base means "Base, in the namespace of ActiveRecord"

在任何命名空间之外解析的常量意味着它听起来就像一个常量 - 一个根本不在任何命名空间中的常量.

A constant being resolved outside of any namespace means exactly what it sounds like - a constant not in any namespace at all.

在没有它的情况下代码可能不明确的地方使用它:

It's used in places where code may be ambiguous without it:

module Document
  class Table # Represents a data table

    def setup
      Table # Refers to the Document::Table class
      ::Table # Refers to the furniture class
    end

  end
end

class Table # Represents furniture
end

这篇关于:: 有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆