-
Notifications
You must be signed in to change notification settings - Fork 1
Example: Association Class
Andrias Meisyal edited this page Jul 16, 2017
·
3 revisions
A class can have a relationship with another class. One of relationship between classes is association. For example, there are two classes associated each other, Class1
and Class2
.
If you generate Ruby code using this extension, there will be two separated .rb
files:
class1.rb
require_relative 'class2.rb'
class Class1
attr_accessor :class2
def initialize()
@class2 = Class2.new
end
def to_s
"Your string representation of the object will be written here."
end
end
class2.rb
require_relative 'class1.rb'
class Class2
attr_accessor :class1
def initialize()
@class1 = Class1.new
end
def to_s
"Your string representation of the object will be written here."
end
end
Another example is two classes associated each other (same with previous example), but a class in a package. Let's say, Util
package.
The extensions will generate two separated .rb
files as well, but one file is in a folder. That folder refers to a class that belongs to Util
package.
class1.rb
require_relative 'util/class2.rb'
class Class1
attr_accessor :class2
def initialize()
@class2 = Class2.new
end
def to_s
"Your string representation of the object will be written here."
end
end
util/class2.rb
require_relative '../class1.rb'
module Util
class Class2
attr_accessor :class1
def initialize()
@class1 = Class1.new
end
def to_s
"Your string representation of the object will be written here."
end
end
end
Note that examples above use default configuration of the extension.