The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Not quite a SuperStruct, maybe a SuperClass? automatic attributes and initialization

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eigen Class

Posts: 358
Nickname: eigenclass
Registered: Oct, 2005

Eigenclass is a hardcore Ruby blog.
Not quite a SuperStruct, maybe a SuperClass? automatic attributes and initialization Posted: May 15, 2006 10:01 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Eigen Class.
Original Post: Not quite a SuperStruct, maybe a SuperClass? automatic attributes and initialization
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Eigen Class
Latest Posts From Eigenclass

Advertisement

ruby-talk:192757 reminded me of SuperStruct. I was going to reply with a pointer to that library, but I realized that the usual

class MyClass < Struct.new(:a,:b)
  def initialize(*a)
    super
    # stuff
  end
end

idiom and SuperStruct still left an important part of the solution space uncovered.

When you inherit from a Struct, the resulting class doesn't use plain instance variables, so you're forced to use the accessors. SuperStruct addresses that while emulating Struct quite closely, and also introduces OpenStruct-like features, so you get hash access with numeric, String and Symbol keys, #members, #to_a, pretty printing... But what if you don't need all that?

I'm guessing the OP would have wanted to do (at least that's what I wanted):

class Foo < InitializedClass.new(:a, :b, :c)
  def initialize(*a)
    super
    @c ||= 10       # this is how we manage default values
  end
  
  def bar
    a + b * c
  end
end

Foo.new(1,2,3).bar                                 # => 7

And while we're at it, why not get named arguments for free too?

Foo.new(:a => 1, :b => 2).bar                      # => 21

This (and a few additional features) takes 56 lines in my first implementation, including #instance_exec, which I already wrote about.

Inheritance

One thing neither Struct nor SuperStruct can do is create a class derived from an existing one. Well, of course module inclusion is but another form of inheritance (and multiple too), but some times you might want the real thing, with matz's blessing.

Say you have

class Baz
  def initialize(foo, bar)
    @foo = a
    @bar = b
  end
end

and want to create a subclass with a number of attributes, but you also need to be able to run Baz#initialize. If we were just subclassing manually,

 super(whatever)

would do, but then there's no automatic accessors nor named arguments. Is it possible at all to get both at once?


Read more...

Read: Not quite a SuperStruct, maybe a SuperClass? automatic attributes and initialization

Topic: Bitch Previous Topic   Next Topic Topic: Flickr Lightbox

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use