Including classes

Hi

Can I also include a class (instead of a module?)
class A
  def a; end
  def self.sa; end
end

class B # same as A with #b, #sb

What I want: # how can I do that?
class N < A end
(and) class N < B end

Futhermore, how can I "include" class methods (seems not working using a module, too?)

use case: ?? (just playing with Ruby)

thanks!
Opti

Can I also include a class (instead of a module?)

Did you try? (Gives a TypeError.)

class A
def a; end
def self.sa; end
end

class B # same as A with #b, #sb

What I want: # how can I do that?
class N < A end
(and) class N < B end

1. "class N < A end" gives a syntax error.
2. You can only inherit from one class.
3. You can include many modules.

Futhermore, how can I "include" class methods (seems not working using a
module, too?)

It does work; do some reading on `include` vs. `extend`
(there are numerous posts around).

I would also highly recommend Russ Olsen's book "Eloquent Ruby".

Regards,
Marcus

···

Am 30.01.2017 um 21:46 schrieb Die Optimisten:

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Hello,
Ok, trying with a module (instead of a class):
methods work,
BUT @vars and @@vars (accessed from the methods defined in the module) aren't accessible?!

=> Is there another way for
  class Ext1 ... end
  class Ext2 ... end

   class A < Ext1 end
     AND
   class A < Ext2 end

thanks
Opti

Hello,
Ok, trying with a module (instead of a class):
methods work,
BUT @vars and @@vars (accessed from the methods defined in the module)
aren't accessible?!

are you sure? (did you try?)

=> Is there another way for
class Ext1 ... end
class Ext2 ... end

  class A < Ext1 end
    AND
  class A < Ext2 end

As I already explained, that doesn't work.
And (again), not a single line of your example "code"
is valid Ruby syntax.

Please

1. explain what you want to achieve (an actual use case)
2. provide a minimal *working* code example that demonstrates
   what you already tried, and in what way it doesn't do what
   you expect or need

Regards,
Marcus

···

Am 31.01.2017 um 12:28 schrieb Die Optimisten:

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

Hello,
Ok, trying with a module (instead of a class):
methods work,
BUT @vars and @@vars (accessed from the methods defined in the module)
aren't accessible?!

are you sure? (did you try?)

Yes, I got nil instead the values I set (vars in the module!).

=> Is there another way for
  class Ext1 ... end
  class Ext2 ... end

   class A < Ext1 end
     AND
   class A < Ext2 end

As I already explained, that doesn't work.
And (again), not a single line of your example "code"
is valid Ruby syntax.

I know, therefore the heading was "I want", "How can I achive" "Is there a way for..."
- that was the description what I want. (It wouldn't be a question if that worked :slight_smile:

Opti

···

On 2017-01-31 18:57, sto.mar@web.de wrote:

Am 31.01.2017 um 12:28 schrieb Die Optimisten:

Hello,
Ok, trying with a module (instead of a class):
methods work,
BUT @vars and @@vars (accessed from the methods defined in the module)
aren't accessible?!

are you sure? (did you try?)

Yes, I got nil instead the values I set (vars in the module!).

?!?

(not very helpful)

It works for me... please provide some actual code you tried!

(Or we can go on forever with "it doesn't work" - "it should" -
"but it didn't" - "it did for me" - ... you get the picture.)

**Again:**

1. explain what you want to achieve (an actual use case)
2. provide a minimal *working* code example that demonstrates
   what you already tried, and in what way it doesn't do what
   you expect or need

And please, provide more *context*.

Your posts often seem like asking "how can I use a screwdriver
to get a nail in a piece of wood", while, if you would provide
some context, someone could point out to you that using a
hammer would be much better. Well, the example is not too good,
but I hope you get the point:

Don't ask how you can inherit from two classes (you can't),
but explain what you actually want to do, and someone can most
probably come up with the "Ruby way" to do it.

=> Is there another way for
class Ext1 ... end
class Ext2 ... end

  class A < Ext1 end
    AND
  class A < Ext2 end

Possibly using modules. Maybe not.

That's the best I can come up with, regarding the lack of information.

Regards,
Marcus

···

Am 31.01.2017 um 19:44 schrieb Die Optimisten:

On 2017-01-31 18:57, sto.mar@web.de wrote:

Am 31.01.2017 um 12:28 schrieb Die Optimisten:

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A