stillenat
stillenat

Reputation: 943

Is it okay to assign an instantiated class to a class variable in Ruby?

Is it okay to instantiate a class A and assign it to a class variable in class B to call A's methods in B?

class A
  ...
end

class B
  @@a = A.new

  def method_B
    @@a.method_A
  end
end

Upvotes: 0

Views: 32

Answers (1)

owade
owade

Reputation: 306

You can wrap methods in a module and include them in your class(For instance methods only)

module Foo
  def method_A
    p 'hello'
  end
end

class B
  include Foo
  def method_B
    method_A
  end
end

B.new.method_B

Upvotes: 1

Related Questions