English 中文(简体)
访问/ 交易Ruby 中的变量
原标题:Accessing/Dealing with Variables in Ruby
  • 时间:2012-05-22 02:53:01
  •  标签:
  • ruby

首先,我要说,我是一位“新”程序员,一个信息技术员,在通过各种辅导工作之后,试图解决他的第一个“真实”问题。

这就是我想做的。 我正在看一个目录, 用于. csv 文件 - 它将以这种格式: 99999_ 888_ filename. csv

我想将“ _” 文件名的每个部分都作为变量返回到另一个程序/ 标本, 用于其它任务 。 我找到了以下代码 w/ :

require  rubygems 
require  fssm 

class Watcher

def start
  monitor = FSSM::Monitor.new(:directories => true)
  monitor.path( /data/testing/uploads ) do |path|
    path.update do |base, relative, ftype| 
      output(relative)
    end
    path.create do |base, relative, ftype| 
      output(relative)
    end
    path.delete { |base, relative, ftype| puts "DELETED #{relative} (#{ftype})" }   
  end
  monitor.run
end


def output(relative)
  puts "#{relative} added"
  values    = relative.split( _ ,)
  sitenum  = values[0]
  numrecs  = values[1]
  filename = values[2]
  puts sitenum
 end
end

我的第一个“ puts” 给了我完整的文件名( 只是在那里让我看脚本在起作用 ), 而第二则将返回站点num 。 我希望能够访问此输出方法的“ 外边 ” 。 我在一个 libs/ 文件夹中有这个文件( 名为 watcher. rb), 而在名为 root 的 root 中, 我有一个第二个文件, 叫做 root.rb, 它仅包含 :

require  ./lib/watcher 

watcher = Watcher.new
watcher.start

我无法找到如何从这个文件中访问我的站点纳姆、 numrecs 和文件名 。 我不确定它是否需要一个变量、 实例变量或什么的。 我玩过一些W/ attr_ aaccessible and other things, 却一无所有。 我决定在这里问, 因为我一直在旋转我的车轮, 做一些事情, 我开始用自己的搜索来混淆自己。

事先感谢各位的帮助或建议。

最佳回答

watcher 类的顶端,您将再次想要定义三种 atr_accessor 声明,这些声明给出了您想要的行为。 (如果您只读,则 atr_reader ,如果您只写,则 atr_accessor ,如果两者都写,则atr_accessor 。 )

class Watcher
    attr_accessor :sitenum, :numrecs, :filename

    ... 
    # later on, use @ for class variables
    ...
        @sitenum = 5
    ...
end

现在您应该对 观察者.sitenum 等没有异议。 这里的 < a href=>" http://codepad.org/bhG3Qbw8" rel=“ no follow” > a example 。

<强度 > EDIT: 一些打字机。

问题回答

除了约旦比额表的答复之外,这些变量应初始化

class Watcher
  attr_accessor :sitenum, :numrecs, :filename

  def initialize
    @sitenum =  default value 
    @numrecs =  default value 
    @filename =  default value 
  end

  ...
end

否则您就会得到无信息价值 nil





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签