English 中文(简体)
Ruby 最佳做法:与班级合作
原标题:Ruby best practice: working with classes
  • 时间:2012-05-25 10:27:54
  •  标签:
  • ruby

See the example below, i suppose it is best to use the second method but the first also works. Which method is best and what are the consquences of using the other ?

class Test 
  def start 
    p "started"
  end
  test = Test.new 
  test.start 
end 

class Test2
  def start 
    p "started"
  end
end
test2 = Test2.new 
test2.start 
最佳回答

你可以做很多事情,这取决于你,这很有趣...

class Test 
  def start 
    p "started"
  end
  new 
end.start

更严重的是,您的第一个示例将所有内容都包含在单类中。对一个脚本来说,这是可以的;它将所有内容放入您自己的命名空间,并且大多避免神秘的主对象上下文。如果需要,您可以定义嵌套类。

然而,第二种办法是比较传统的。

问题回答

我要肯定地说,第二个变式更有意义。 第一个变式不会造成错误,但物体即时化完全过时,毫无意义。 外部变量在类别范围内是看不到的:

var = "string"

class A
  var = A.new
end

puts var #=> string

s 没有关闭, 外部 var 与该类中不同。 这意味着您的对象在创建后“ 丢失”, 无法再进入, 最终会受到 GC 约束 。

当您说第一个示例“ 工作 ”, 在这种背景下工作, 意指它“ em> is

如果您不需要日后使用的引用, 而且您真的想要做这样的“ 一发” 操作, 使用一种无需即时调用对象即可调用的类方法, 或者做 < code> 初始化 < / code > 中需要做的事情, 如果这是每次即时化必须做的事情, 则会更特殊 。

Defining a class that creates an object of itself when it s load is not a good idea. The object would only be available to itself and not to it s parent s scope (of course you could still access it via ObjectSpace::each_object).

Some Best Practices for Ruby Classes

  • Use a consistent structure in your class definitions.
  • Don t nest multi line classes within classes. Try to have such nested classes each in their own file in a folder named like the containing class.
  • Prefer modules to classes with only class methods. Classes should be used only when it makes sense to create instances out of them.
  • Favor the use of module_function over extend self when you want to turn a module s instance methods into class methods.
  • When designing class hierarchies make sure that they conform to the Liskov Substitution Principle.
  • Try to make your classes as SOLID as possible.
  • Always supply a proper to_s method for classes that represent domain objects.
  • Use the attr family of functions to define trivial accessors or mutators.
  • Avoid the use of attr. Use attr_reader and attr_accessor instead.
  • Consider using Struct.new, which defines the trivial accessors, constructor and comparison operators for you.
  • Don t extend an instance initialized by Struct.new. Extending it introduces a superfluous class level and may also introduce weird errors if the file is required multiple times.
  • Consider adding factory methods to provide additional sensible ways to create instances of a particular class.
  • Prefer duck-typing over inheritance.
  • Avoid the usage of class (@@) variables due to their "nasty" behavior in inheritance.
  • Assign proper visibility levels to methods (private, protected) in accordance with their intended usage. Don t go off leaving everything public (which is the default). After all we re coding in Ruby now, not in Python.
  • Indent the public, protected, and private methods as much as the method definitions they apply to. Leave one blank line above the visibility modifier and one blank line below in order to emphasize that it applies to all methods below it.
  • Use def self.method to define class methods. This makes the code easier to refactor since the class name is not repeated.

这是红宝石最佳实践的最佳工具:https://github.com/bbatsov/ruby-stype-guide





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

热门标签