Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 理解RubyonRails中的类变量和方法_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 理解RubyonRails中的类变量和方法

Ruby on rails 理解RubyonRails中的类变量和方法,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我是Ruby和RubyonRails的新手,来自类似C语言的背景。 下面是我在application\u controller.rb文件中找到的一些代码: def current_user @current_user ||= Renter.find(session[:user_id]) if session[:user_id] end helper_method :current_user def authorize_user redirect_to '/login' unle

我是Ruby和RubyonRails的新手,来自类似C语言的背景。
下面是我在
application\u controller.rb
文件中找到的一些代码:

def current_user
    @current_user ||= Renter.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user

def authorize_user
    redirect_to '/login' unless current_user
end
我不明白的是:
-在第4行,是
:current_user
调用
current_user
实例方法,还是直接访问
@current_user
实例变量?
-在第7行,
current\u user
是调用
current\u user
实例方法,还是直接访问
@current\u user
实例变量?

-在第2行,
:user\u id
是一个变量还是更像一个字符串文字被用作键?有点像在JavaScript中,可以编写
session[“user\u id”]
来获取
session
对象的
user\u id
属性。

让我们一次解决一个问题

  • 在第4行,
    :current_user
    是一种方法,最有可能用于返回
    current_user
    ,因此您可以访问
    用户名
    ,或
    电子邮件
    ,或用户拥有的任何值

  • 在第7行,它仍然是相同的方法。在本例中,Ruby正在检查是否存在
    当前用户
    对象。您可以将
    除非
    视为
    如果不是
    。因此,如果当前用户为false,代码将重定向到login,如果
    当前用户==nil
    ,则会发生这种情况。如果用户已登录,
    当前用户!=nil
    ,则不会发生重定向

  • 在第2行,
    :user\u id
    是一个符号,
    session
    是一个散列,它是一个键值对,例如{a:1,b:2},您可以使用
    []
    方法使用键访问值,因此
    session[:user\u id]
    返回用户id的值。在Ruby中,您可以使用任何东西作为键,使用符号是因为它们总是唯一的,
    :two
    :two
    的对象id相同,而“two”和“two”的id不同


类方法在本例中不相关-此处不使用它们

实例变量在get/set时永远不会调用方法

尽管有时会发生相反的情况。为实例变量创建getter/setter方法是一种非常常见的模式,非常常见,以至于在ruby core中定义了attr reader/writer/accessor helpers。如果编写
attr\u accessor:foo
,则
foo=
foo
将获取/设置实例变量

但这并非默认情况

要回答您的另一个问题:

符号
:user_id
以冒号开头,类似于字符串。符号和字符串之间的区别似乎是任意的,但这在Ruby中是一个重要的概念,在头脑中进行区分是一个好主意


回应你的评论:

第4行,<代码> HelpRyToMult:CurrutsUpUs>代码>确实是Rails所特有的,如果你喜欢的话,可以考虑它是“Rails魔法”。实际上,这使得您的

当前用户可以从视图中调用
方法(而默认情况下,它只在控制器中可用)<代码>:当前\u用户
是一个符号,用于引用
当前\u用户
方法。您不必完全了解某些细节,只需知道
helper\u method
使用与方法同名的符号,并使该方法可供视图使用即可。据我所知,它只与Rails控制器相关

在Ruby中,使用引用方法名的符号有点常见。这是一个更中间的概念。您可以在
send
中看到另一个示例:

def asd
  return 0
end

class Foo
  def instance_method_bar
    return 0
  end
  def self.class_method_bar
    return 0
  end
end

# how the methods are typically called
asd
Foo.new.instance_method_bar
Foo.class_method_bar

# another way to call them, using send
send(:asd)
Foo.new.send(:instance_method_bar)
Foo.send(:class_method_bar)
除非您需要,否则我不建议您使用
send
,但希望它能更清楚地说明符号
:current\u user
是如何在
helper\u方法中使用的


第7行是被调用的
当前用户
方法

一,。为什么第4行和第7行的方法调用看起来不同(一个前面有冒号,另一个没有)?2.当前用户是符号的声明吗?3.符号是像字符串一样的原语,还是像变量一样有值?你可以把
:current\u user
看作一个参数,它告诉
helper\u method
要调用哪些方法,所以它更像是一个标识符。不带冒号的
当前用户
实际上正在调用方法本身。第4行的
:当前用户
和第7行的
当前用户
是什么?
:current_user
也是一个符号吗?我建议您阅读一本相当深入的ruby教科书(不需要做示例,只需理解概念即可)。并不是说你不这样做就无法学习Rails,但是当调试Rails或解构“魔法”时,对Ruby有一个坚实的理解真的很有用。非常感谢。关于
:当前用户
仍然是一个符号的那一部分确实消除了困惑。有一段时间我在想,也许冒号运算符会导致在某些上下文中调用函数,而在其他上下文中它表示符号。但现在我知道这是一个神奇的东西。有点像在PHP中,我将字符串作为参数传递,其中包含将被调用的函数的名称。我发现有趣的是,您指出:“在本例中,类方法不相关-它们在这里不被使用。”如果是这样的话,那么什么是
@current\u user
?让我们来看看。