【问题标题】:Saving and calling multiple variables from ruby method从 ruby​​ 方法保存和调用多个变量
【发布时间】:2012-09-25 15:42:39
【问题描述】:

也许我一直盯着我的 Ruby 书看,以至于文字不再具有任何意义,但我想我还是问问吧。

我想要做的是,不是通过我的函数头传递大量变量,或者不是使用全局变量,而是希望将我的变量保存在一个方法中,并多次调用它们,贯穿我的职能。我实际上遇到的问题是范围。

def DateGrab()
 print "\nEnter the date you're looking for (Month/Day): "
 longdate = gets.strip.split(/\/| /)
 if longdate[0].length > 3
   month = longdate[0].slice(0..2)
 else
   month = longdate[0]
 end
  day = longdate[1]
  year = `date | awk '{print $6}'`.strip
  grepdate = "#{day}/#{month}/#{year}"
  date = Date.parse("#{day}-#{month}-#{year}").strftime('%m%d%Y').strip
end

我正在寻找通过多个函数传递“grepdate”和“date”,我觉得使用方法会更容易,但是每次我尝试调用变量时,我都会得到一个“未定义的局部变量或方法“ 错误。

【问题讨论】:

    标签: ruby variables methods


    【解决方案1】:

    您想查看实例变量。您可以通过@grepdate = "something" 设置它们。这些实例变量可在您的整个类以及该类的所有方法中访问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 2011-09-02
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 2014-03-06
      • 1970-01-01
      相关资源
      最近更新 更多