ruby - Creating an object of a different class within a class method -


i trying call method create object of class. when do, feedback object has been created, can't seem to object (such call method objects class). here example:

class     def initialize      end      def generate         var = b.new     end end  class b     def initialize      end      def declare         puts "i exist!"     end end  test = a.new test.generate var.declare 

this returns "undefined local variable or method 'var'" error. going wrong? best can figure creating object within instance, when try doing stuff in instance comes undefined. there way not thinking of, or doing wrong? thanks!

var "visible" within generatemethod. move out of class.

class     def generate         b.new     end end  class b     def declare         puts "i exist!"     end end  test = a.new var  = test.generate  # var instance of b var.declare # => exist! 

method chaining possible, avoiding variables.

a.new.generate.declare # => exist! 

Comments