Ruby setter 习语

发布于 2024-08-13 03:43:08 字数 850 浏览 3 评论 0原文

我正在开发一个 Chart 类,它有一个 margin 参数,其中包含 :top:bottom:right:left 值。我的第一个选择是使 margin 成为一个 setter 并设置如下值:

# Sets :left and :right margins and doesn't alter :top and :bottom 
chart.margins = {:left => 10, :right => 15}

这很好,因为它显然是一个 setter,但是,经过一番思考,我认为它也可能会令人困惑:用户可能会认为margins 仅包含 :left:right 值,这是不正确的。另一种选择是消除 = 并使其成为普通方法:

chart.margins(:left => 10, :right => 15)

使用这种语法,很容易弄清楚发生了什么,但它不是标准设置器,并且与 margins 冲突吸气剂。还有另一种选择:

chart.margins(:left, 10)
chart.margins(:right, 15)

我不知道该怎么想。对我来说,很明显该方法是一个 setter,但这次我无法仅通过一次调用来设置多个值,并且 getter 再次出现问题。我对 Ruby 比较陌生,还没有习惯所有的习惯用法。那么,你们觉得怎么样?哪个是最好的选择?

I'm working on a Chart class and it has a margin parameter, that holds :top, :bottom, :right and :left values. My first option was to make margin a setter and set values like this:

# Sets :left and :right margins and doesn't alter :top and :bottom 
chart.margins = {:left => 10, :right => 15}

It's nice, because it is clearly a setter, but, after some thought, I think it could be confusing too: the user might think that margins contains only :left and :right values, what is not right. Another option is eliminate = and make it an ordinary method:

chart.margins(:left => 10, :right => 15)

With this syntax, it's easy to figure out what is happening, but it is not a standard setter and conflicts with margins getter. And there's still another option:

chart.margins(:left, 10)
chart.margins(:right, 15)

I don't know what to think about this. For me, it is obvious the method is a setter, but this time I cannot set multiple values with just one call and there's the problem with getter again. I'm relatively new to Ruby and I haven't got used to all the idioms yet. So, what do you think guys? Which is the best option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清引 2024-08-20 03:43:08

您还可以创建一个 Margin 类来享受以下清晰的语法:

class Margin
    attr_accessor :left, :right, :top, :bottom
    ...
end

class Chart
    attr_accessor :margins
    ...
 end


chart.margins.left = 10
puts chart.margins.right

You could also make a Margin class to enjoy the following clear syntax:

class Margin
    attr_accessor :left, :right, :top, :bottom
    ...
end

class Chart
    attr_accessor :margins
    ...
 end


chart.margins.left = 10
puts chart.margins.right
窝囊感情。 2024-08-20 03:43:08

不太确定这是否是您想要提供的语法(如果不是,抱歉:)

#!/usr/bin/ruby
class Margins < Struct.new(:top, :bottom, :left, :right) 
end

class Chart
  attr_reader :margins

  def initialize()
    @margins = Margins.new(0,0,0,0)
  end

  def margins=(hash)
    [:top, :bottom, :left, :right].each do |dir|
      if (hash[dir])
        @margins[dir] = hash[dir]
      end
    end
  end
end

c = Chart.new
c.margins.left = 10
c.margins={:top=>12,:bottom=>13}
puts c.margins.left
# 10
puts c.inspect;
# #<Chart:0xb7caaf8c @margins=#<struct Margins top=12, bottom=13, left=10, right=0>>

# However c.margins.foo = 12 would give you an error

Not so sure if this is the kind of syntax that you would like to make available (sorry if not : )

#!/usr/bin/ruby
class Margins < Struct.new(:top, :bottom, :left, :right) 
end

class Chart
  attr_reader :margins

  def initialize()
    @margins = Margins.new(0,0,0,0)
  end

  def margins=(hash)
    [:top, :bottom, :left, :right].each do |dir|
      if (hash[dir])
        @margins[dir] = hash[dir]
      end
    end
  end
end

c = Chart.new
c.margins.left = 10
c.margins={:top=>12,:bottom=>13}
puts c.margins.left
# 10
puts c.inspect;
# #<Chart:0xb7caaf8c @margins=#<struct Margins top=12, bottom=13, left=10, right=0>>

# However c.margins.foo = 12 would give you an error
徒留西风 2024-08-20 03:43:08

除了范例的答案之外,您还可以向 Margins 类添加一个方法来支持:

chart.margins.set :left => 10, :right => 15

您可以扩展 margins= 方法来处理数字参数:

chart.margins = 20

作为糖:

chart.margins = Margins.new(20, 20, 20, 20)

In addition to paradigmatic's answer, you could add a method to the Margins class to support:

chart.margins.set :left => 10, :right => 15

You could extend the margins= method to treat a numeric argument:

chart.margins = 20

as sugar for:

chart.margins = Margins.new(20, 20, 20, 20)
挽袖吟 2024-08-20 03:43:08

我不认为为 Margin 创建一个类是一种矫枉过正的做法。您始终可以使用 to_hash 或类似的方法将其值公开为哈希值。

另外,如果你愿意,你可以让它以 DSL 风格工作:

chart.margins do |m|
  m.left 10
  m.right 20
  m.vertical 5 # sets both top and bottom margin
end

但我想我无论如何都会选择范例的方法......

I don't think creating a class for Margin is an overkill. You can always expose its values as a hash using to_hash or something similar.

Also, if you like, you can make it work in DSL-style:

chart.margins do |m|
  m.left 10
  m.right 20
  m.vertical 5 # sets both top and bottom margin
end

But I guess I would choose paradigmatic's approach anyway...

乖乖哒 2024-08-20 03:43:08

您也可以坚持最初使用的内容并使用正常的哈希语法。

margins["left"] = 10  #to set just one without changing the others

You could also stick with what you had first and use the normal hash syntax.

margins["left"] = 10  #to set just one without changing the others
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文