Add or remove members from a Struct class

Hi,

I have a class defines from a Struct:
  class A < Struct.new(:a, :b, :c)
  end

I have a second class:
  class B < A
  end

Is there a way to remove a member (say c) from A in B?
Or is there a way to do the inverse (declare B with 2 members, and make
A be a subclass of B with a new third member) ?

Regards,

Sylvain

You could use Module#undef_method:

class B < A
  undef_method :c
end

I hope this helps

Stefano

···

On Wednesday, 11 January 2017 13:59:50 CET Sylvain Daubert wrote:

Hi,

I have a class defines from a Struct:
  class A < Struct.new(:a, :b, :c)
  end

I have a second class:
  class B < A
  end

Is there a way to remove a member (say c) from A in B?
Or is there a way to do the inverse (declare B with 2 members, and make
A be a subclass of B with a new third member) ?

Regards,

Sylvain

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

I have a class defines from a Struct:
  class A < Struct.new(:a, :b, :c)
  end

I have a second class:
  class B < A
  end

Is there a way to remove a member (say c) from A in B?
Or is there a way to do the inverse (declare B with 2 members, and make
A be a subclass of B with a new third member) ?

If things start to get that complicated then Struct is probably not
the right tool for the job. You can as easily define classes with
multiple members by using attr_accessor and use inheritance etc. Or
you use modules to mix in.

To come up with more concrete advice we probably need to know more
about the use case.

You could use Module#undef_method:

class B < A
  undef_method :c
end

If you find yourself frequently removing methods from classes then I'd
say there is something wrong in the design or in the way the language
is used. The fact that it can be done in Ruby does not mean one should
make it a habit.

Kind regards

robert

···

On Wed, Jan 11, 2017 at 2:59 PM, Stefano Crocco <stefano.crocco@alice.it> wrote:

On Wednesday, 11 January 2017 13:59:50 CET Sylvain Daubert wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

I have a class defines from a Struct:
  class A < Struct.new(:a, :b, :c)
  end

I have a second class:
  class B < A
  end

Is there a way to remove a member (say c) from A in B?
Or is there a way to do the inverse (declare B with 2 members, and make
A be a subclass of B with a new third member) ?

If things start to get that complicated then Struct is probably not
the right tool for the job. You can as easily define classes with
multiple members by using attr_accessor and use inheritance etc. Or
you use modules to mix in.

To come up with more concrete advice we probably need to know more
about the use case.

This is for my gem PacketGen. I need to create 2 classes to handle DNS
objects one a subset of the other. I used a modified Struct (StructFu,
from PacketFu gem) as base class to handle reading from a binary string
or generating data to send on network.

But I need to redefine getters and setters for each struct member to
hide complexity.
As my first class is a subset (3 members) of the second (6 members), i
try to not repeat myself.

You could use Module#undef_method:

class B < A
  undef_method :c
end

If you find yourself frequently removing methods from classes then I'd
say there is something wrong in the design or in the way the language
is used. The fact that it can be done in Ruby does not mean one should
make it a habit.

Sure, removing methods is not the good way to do it.
I tried to avoid a mixin module to not add another complexity layer,
but, finally, it seems the better way to do it.

Thanks for your answers.

Sylvain

···

Le 11/01/2017 à 16:55, Robert Klemme a écrit :

On Wed, Jan 11, 2017 at 2:59 PM, Stefano Crocco <stefano.crocco@alice.it> wrote:

On Wednesday, 11 January 2017 13:59:50 CET Sylvain Daubert wrote: