ruby-on-rails – 如何使用ruby方法.present?
发布时间:2020-12-17 04:35:44 所属栏目:百科 来源:网络整理
导读:我在rails app中有这样的方法: def current_user return @current_user if @current_user.present? @current_user = current_user_session current_user_session.recordend 我还没有用过.present?方法之前,我进入我的交互式ruby外壳玩它. 每当我使用xxx.pr
我在rails app中有这样的方法:
def current_user return @current_user if @current_user.present? @current_user = current_user_session && current_user_session.record end 我还没有用过.present?方法之前,我进入我的交互式ruby外壳玩它. 每当我使用xxx.present?它返回NoMethodError.如果xxx是字符串,数字,数组,则无关紧要. 我该如何使用这种方法? 解决方法
present是ActiveSupport提供的方法.它不是核心ruby的一部分.这就是为什么你不能在ruby外壳中使用它.要使用它,您需要在控制台中需要相关的库(例如irb / pry):
require 'active_support' require 'active_support/core_ext' 这样,您就可以访问activesupport提供的所有实用程序方法. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |