0

Consider this code:

  Campus = ImmutableStruct.new(:id, :name, :timezone) do
    def hash; id end
  end

  Merchant = ImmutableStruct.new(:id, :name, :campus) do
    def hash; id end
  end

Notice the duplication of the hash method. I want to remove this duplication with a new class ImmutableStructWithId. That new class would allow the above 2 lines to be rewritten as follows:

Campus = ImmutableStructWithId.new(:id, :name, :timezone)
Merchant = ImmutableStructWithId.new(:id, :name, :campus) 

If ruby's initializer worked like factory functions (they don't), something like the following is what I'd want:

class ImmutableStructWithId
  def initialize(*args)
    ImmutableStruct.new(*args) do
      def hash; id end
    end
  end
end

I am aware the above won't work, because initializers don't return the object they are creating, they just initialize it. But if they did work like factory functions, the above is what I'd want to do.

What is the correct way to achieve the same effect in ruby?

6
  • Also, I don't see any constructor specifics. You just don't want to repeat method definition, no? Commented Nov 25, 2016 at 16:17
  • Correct. Ultimately I want to avoid repeating the hash definition, and just use a specific class that has that baked in. Commented Nov 25, 2016 at 16:23
  • Decorators/delegators then. Commented Nov 25, 2016 at 16:26
  • so I have to create ImmutableStruct as a member, and then delegate everything to it? That is so nasty... Is there not a better way? Commented Nov 25, 2016 at 16:29
  • None that I can think of right now :) Commented Nov 25, 2016 at 16:32

1 Answer 1

4

IMO this should work for you:

require 'immutable-struct'

module ImmutableStructWithId
  def self.new(*args)
    ImmutableStruct.new(*args) do
      def hash; id; end
    end
  end
end

Campus = ImmutableStructWithId.new(:id, :name, :timezone)
campus = Campus.new(id: '1', name: 'foo', timezone: 'UTC')
#=> #<Campus:0x007f8ed581de20 @id="1", @name="foo", @timezone="UTC">
campus.hash
#=> "1"
Sign up to request clarification or add additional context in comments.

7 Comments

perfect. wasn't aware you could re-define new like that. does the above only work with module -- ie, is there a reason you didn't use class here?
I used a module, because I knew that I do not want to create an instance of it. In this context you might want to consider another name, perhaps ImmutableStructWithIdFactory?
@Jonah: new is just a method like any other method. Why wouldn't you be able to define your own? The one you usually inherit from Class just looks like this: class Class; def new(*args, &block) obj = allocate; obj.send(:initialize, *args, &block); obj end end.
@JörgWMittag i just stupidly assumed it was special for no good reason
@Jonah: Ruby is exactly a poster child of simplicity, regularity and orthogonality, that is certainly true. But in this case, it is much simpler than it is usually made out to be: there is exactly one kind of methods, namely instance methods. There are no class methods, no static methods, no constructors, no initializers, no functions, no procedures. Unlike e.g. Java, which has three different kinds of "methods" (instance methods, static methods, constructors), Ruby has only one. And they are always dynamically dispatched, always virtual, always overridable (and also always re-definable).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.