热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将构造函数参数转换为实例变量[duplicate]-Turningconstructorargumentsintoinstancevariables[duplicate]

PossibleDuplicate:Idiomaticobjectcreationinruby可能重复:在ruby中创建惯用对象Therearemanyoccai

Possible Duplicate:
Idiomatic object creation in ruby

可能重复:在ruby中创建惯用对象

There are many occaisions when I have an initialize method that looks like this:

当我有一个如下所示的initialize方法时,有很多次:

class Foo  
    def initialize bar, buz, ...
        @bar, @buz, ... = bar, buz, ...
    end
end

Is there a way to do this with a simple command like:

有没有办法用一个简单的命令来做到这一点,如:

class Foo
    attr_constructor :bar, :buz, ...
end

where the symbols represent the name of the instance variables (with the spirit/flavor of attr_accessor, attr_reader, attr_writer)?

其中符号表示实例变量的名称(具有attr_accessor,attr_reader,attr_writer的精神/味道)?


I was wondering if there is a built in way or a more elegant way of doing something like this:

我想知道是否有内置方式或更优雅的方式做这样的事情:

class Class
    def attr_constructor *vars
        define_method("initialize") do |*vals|
            vars.zip(vals){|var, val| instance_variable_set("@#{var}", val)}
        end
    end
end

so that I can use it like this:

所以我可以像这样使用它:

class Foo
    attr_constructor :foo, :bar, :buz
end

p Foo.new('a', 'b', 'c')      # => #
p Foo.new('a', 'b', 'c', 'd') # => #
p Foo.new('a', 'b')           # => #

3 个解决方案

#1


2  

Would this work for you?

这对你有用吗?

class Foo  
    def initialize(hash)
        hash.each { |k,v| instance_variable_set("@#{k}", v) }
    end
end

#2


2  

I'd use OpenStruct:

我使用OpenStruct:

require 'ostruct'

class Foo  "baz")
f.bar
#=> "baz"

Edit: Ah OK, sorry misunderstood you. How about just:

编辑:啊好的,抱歉误解了你。怎么样:

class Foo   
  def initialize(*args)
    @baz, @buz = args
  end
end

#3


2  

Interesting question. A little meta-programming should take care of it.

有趣的问题。一个小的元编程应该照顾它。

module Attrs
  def self.included(base)
    base.extend ClassMethods
    base.class_eval do
      class < self.class.attrs.size
    # Loop through each arg, assigning it to the appropriate attribute (based on the order)
    args.each_with_index do |val, i|
      attr = self.class.attrs[i]
      instance_variable_set "@#{attr}", val
    end
  end
end

class Foo
  include Attrs
  has_attrs :bar, :buz
end

f = Foo.new('One', 'Two')
puts f.bar
puts f.buz

Of course the downside to this is inflexibility - you have to pass your constructor arguments in a specific order. Of course that's how most programming languages are. Rails people might argue you should instead do

当然,缺点是缺乏灵活性 - 您必须按特定顺序传递构造函数参数。当然,大多数编程语言都是如此。 Rails人们可能会争辩说你应该这样做

f = Foo.new(:bar => 'One', :baz => 'Two')

which would allow you to pass in attrs in any order, as well as strip away most of the meta-programming. But that is a lot more to type.

这将允许您以任何顺序传入attrs,以及剥离大部分元编程。但要打字要多得多。


推荐阅读
author-avatar
yzxnha_975
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有