相关疑难解决方法(0)

在ruby中创建惯用对象

在ruby中,我经常发现自己编写以下内容:

class Foo
  def initialize(bar, baz)
    @bar = bar
    @baz = baz
  end

  << more stuff >>

end
Run Code Online (Sandbox Code Playgroud)

甚至

class Foo
  attr_accessor :bar, :baz

  def initialize(bar, baz)
    @bar = bar
    @baz = baz
  end

  << more stuff >>

end
Run Code Online (Sandbox Code Playgroud)

我总是希望尽可能地减少样板 - 所以有没有更惯用的方式在ruby中创建对象?

ruby

14
推荐指数
2
解决办法
1963
查看次数

标签 统计

ruby ×1