From: The Higgs bozo Date: 2010-01-25T02:40:45+09:00 Subject: Re: Writing proper getter in a Ruby way > I don't have that: When I said "A few things about define_method blocks," I meant this, class P define_method :m do |*args, &b| [args, b] end end class C < P define_method :m do |*args, &b| p [args, b] p super end end C.new.m(:a, :b) { |e| e } Ruby 1.9: #=> in `block in ': implicit argument passing of super from method defined by define_method() is not supported. Specify all arguments explicitly. (RuntimeError) Ruby 1.8.7: #=> [[:a, :b], #] #=> [[:a, :b], nil] > So another way, probably less 'overkill' you made me think is: > > class Test > attr_reader :var > def initialize(*args) > if args.length > 0 > @var = :value > end > end > > alias :get_var :var > def var > get_var || 3 > end > end > > p Test.new.var #=> 3 > p Test.new(:arg1, :arg2).var #=> :value I dunno, it looks convoluted. Might as well go back to "old school" style, or if it becomes redundant you can use the previously mentioned technique to abstract like this, initialized_attr_reader :var, 3 Going further, you can change the '3' into a block evaluated by newly created instances. Or maybe Struct is all you need for initializing variables, Struct.new(:a, :b).new(2, 3) #=> # a=2, b=3> Struct.new(:a, :b).new #=> # a=nil, b=nil> -- Posted via http://www.ruby-forum.com/.