Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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 如何使用ruby方法。演示?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何使用ruby方法。演示?

Ruby on rails 如何使用ruby方法。演示?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我在rails应用程序中有这样一种方法: def current_user return @current_user if @current_user.present? @current_user = current_user_session && current_user_session.record end 我以前没有使用过.present?方法,所以我进入了我的交互式ruby shell来使用它 无论何时使用xxx.present?它都会返

我在rails应用程序中有这样一种方法:

def current_user
        return @current_user if @current_user.present?
        @current_user = current_user_session && current_user_session.record
end
我以前没有使用过
.present?
方法,所以我进入了我的交互式ruby shell来使用它

无论何时使用
xxx.present?
它都会返回一个命名错误。不管xxx是否是字符串、数字或数组

如何使用此方法?

是rails方法,而不是ruby。

尝试使用
rails c
而不是
irb
在控制台中使用它。

present
ActiveSupport
提供的一种方法。它不是核心ruby的一部分。这就是为什么不能在ruby shell中使用它。要使用它,您需要在控制台中使用相关库(例如irb/pry):


这样,您就可以访问由
activesupport

提供的所有实用程序方法,如上所述,
.present
不在ruby标准库中。相反,它处于主动支持状态(.)


要使用active_支持,首先安装gem,然后在irb或ruby脚本中要求它。有关如何有选择地加载activesupport扩展的更多信息,请参见。

如果
@current\u user.present?
为true,则该方法的第二行将不会运行,我是否可以正确地说?i、 e.
@当前用户
将从第一行返回?是。该函数将从第一行返回@current_user。您也可以将其写入一行:@current_user | |=current_user_session&¤t_user_session.recordHad还需要特定的扩展名。就我而言,我要求他们所有人都做这项工作<代码>需要“主动支持”
需要“主动支持/core\u ext”
require 'active_support'
require 'active_support/core_ext'