Tag Archives: oop

Seter and Geter di Ruby

Sering buat seter geter di Java kan? kalo di ruby bisa kayak gini..

[ruby]
class Mobil
%w{jenis merek bensin}.each { |prop|
define_method(("get"+prop.capitalize).to_sym) {
return self.instance_variable_get "@"+prop
}
define_method(("set"+prop.capitalize).to_sym) { |value|
self.instance_variable_set "@"+prop, value
}
}
end

a = Mobil.new
a.setMerek("Honda")
a.setBensin("Pertamax")
a.setJenis("Sedan")

a.getMerek
a.getBensin
a.getJenis
[/ruby]

Sumber : kaskus credit to agan magico