带有默认值的参数哈希的 Ruby 方法:如何 DRY?

发布于 2024-11-05 16:03:46 字数 713 浏览 0 评论 0原文

我有一个 initialize 方法,它接受哈希值来设置一些实例变量。这基本上就是我想要做的:

class Ad
  DEFAULT_PAGE = 'index'.freeze

  DEFAULT_CHANNEL = 'general'.freeze

  DEFAULT_AREA = 'general'.freeze

  attr_accessor :page, :area, :channel

  def initialize args={}
    @page = args[:page] || DEFAULT_PAGE
    @area = args[:area] || DEFAULT_AREA
    @channel = args[:channel] || DEFAULT_CHANNEL
  end

  # ...
end

我看到了一个允许动态设置实例变量的提示,但我不确定如何也包含默认值...

  def initialize args={}
    args.each do |attr,val|
      instance_variable_set("@#{attr}", val) unless val.nil?
    end
  end

我可以动态引用常量吗?或者有任何更好的方法来做这类事情,欢迎!

...我还意识到 attr_accessor 变量可以单独设置。但我只想这样做。 :)

I have an initialize method that accepts a hash to set some instance variables. This is basically what I'm trying to do:

class Ad
  DEFAULT_PAGE = 'index'.freeze

  DEFAULT_CHANNEL = 'general'.freeze

  DEFAULT_AREA = 'general'.freeze

  attr_accessor :page, :area, :channel

  def initialize args={}
    @page = args[:page] || DEFAULT_PAGE
    @area = args[:area] || DEFAULT_AREA
    @channel = args[:channel] || DEFAULT_CHANNEL
  end

  # ...
end

I saw a tip to allow dynamic setting of instance variables, but I'm not sure how to also include the default values...

  def initialize args={}
    args.each do |attr,val|
      instance_variable_set("@#{attr}", val) unless val.nil?
    end
  end

Can I refer to a constant dynamically? Or any better ways of doing this sort of thing are welcome!

... I also realize that attr_accessor variables can be set individually. But I just want to do it like this. :)

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

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

发布评论

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

评论(2

樱花细雨 2024-11-12 16:03:46

如果实例变量位于您的 defaults 哈希中,则此变量也仅创建它们,这样您就不会意外创建/覆盖其他实例变量。

我假设您的意思是除非 val.nil?

def initialize(args={})
  defaults = {
    :page    => DEFAULT_PAGE,
    :area    => DEFAULT_AREA,
    :channel => DEFAULT_CHANNEL
  }.merge(args).each do |attr, val|
    instance_variable_set("@#{attr}", val) if defaults.has_key?(attr) && (not val.nil?)
  end if args
end

This one also only creates the instance variables if they are in your defaults hash, so that you don't accidentally create/overwrite other instance variables.

I'm assuming you meant to say unless val.nil?:

def initialize(args={})
  defaults = {
    :page    => DEFAULT_PAGE,
    :area    => DEFAULT_AREA,
    :channel => DEFAULT_CHANNEL
  }.merge(args).each do |attr, val|
    instance_variable_set("@#{attr}", val) if defaults.has_key?(attr) && (not val.nil?)
  end if args
end
娜些时光,永不杰束 2024-11-12 16:03:46

试试这个:

  def initialize args={}
    defaults = {
      :page    => DEFAULT_AREA, 
      :area    => DEFAULT_AREA, 
      :channel => DEFAULT_CHANNEL
    }
    args.reverse_merge(defaults).each do |attr,val|
      instance_variable_set("@#{attr}", val) unless val.nil?
    end
  end

Try this:

  def initialize args={}
    defaults = {
      :page    => DEFAULT_AREA, 
      :area    => DEFAULT_AREA, 
      :channel => DEFAULT_CHANNEL
    }
    args.reverse_merge(defaults).each do |attr,val|
      instance_variable_set("@#{attr}", val) unless val.nil?
    end
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文