English 中文(简体)
将红宝石代码换成类时出错
原标题:error during wrapping the ruby code into a class
  • 时间:2012-05-25 05:56:19
  •  标签:
  • ruby
  • class

我是RoR新人,我在红宝石上发明了一个小应用, 它有小功能执行代码。

例如:

 def abc(xyz)
    some code
 end

 def ghi(xyz)
    some code
 end

 def jkl(output)
    some code
 end

 xyz = abc[ARGV(0)]
 output = ghi(xyz)
 puts jkl(output)

现在,当我使用 Ruby.rb 命令运行此代码时, 它会执行良好, 并返回预期结果。 但是当我试图创建一个类, 并添加整个代码到它上, 比如 。

 class Foo
     def abc(xyz)
    some code
 end

 def ghi(xyz)
    some code
 end

 def jkl(output)
    some code
 end

 xyz = abc[ARGV(0)]
 output = ghi(xyz)
 puts jkl(output)
 end

它生成错误, 如 Foo: Class ( NoMethodError) 的“ 强” “ 未定义方法 abc ”

我想问的是,我该如何把这个代码添加到一个班级上,以便它能够更容易插上,并取得预期的结果。

提前感谢。

最佳回答

写此作时, 这些都是实例方法。 您需要将这些类方法像这两个示例一样, 或者您可以保留它们, 并创建一个类实例 。 无论是哪种方式, 您也许应该将最后三个语句移到类定义之外 。

class Foo
  class << self
    def abc(xyz)
      some code
    end

    def ghi(xyz)
      some code
    end

    def jkl(output)
      some code
    end
  end
end
xyz = Foo.abc( something )
output = Foo.ghi(xyz)
puts Foo.jkl(output)

或... 。 。

class Foo
  def self.abc(xyz)
    some code
  end

  def self.ghi(xyz)
    some code
  end

  def self.jkl(output)
    some code
  end
end
xyz = Foo.abc( something )
output = Foo.ghi(xyz)
puts Foo.jkl(output)

在评论中回答你的问题, 这就是你如何对课程进行即时处理, 并使用实例方法调用 。

class Foo
  def abc(xyz)
    some code
  end

  def ghi(xyz)
    some code
  end

  def jkl(output)
    some code
  end
end
bar = Foo.new
xyz = bar.abc( something )
output = bar.ghi(xyz)
puts bar.jkl(output)

如果您还没有鲁比学习材料, 您可能想要查看 < a href=" http:// pine. fm/ Learn ToProgram/" rel=" no follown noreferr" > Chris Pine s traitorial , 其中包括一个关于< a href=" "http:// pine. fm/Learn ToProgram/?Capt=09" rel="no follow noreferrer" > class 及其工作方式的章节。 至于书籍, < a href=" https://rads. stackoverflowrm.com/amzn/ kick/com/ 031584104" rel=" norefrefrererr" rel= “ norerefref=" https://stackoverflow.com/ q/1490/ 366051" 。

问题回答

暂无回答




相关问题
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 ...

热门标签