Since you are saving the proc instance as
@description
, it is better to access that via a variable rather than usingyield
.def description &pr @description = pr end
You need to evaluate
@description
in the appropriate environment, i.e. within the instance ofCar
.def show_description instance_exec(&@description) end
With the fixes as above, it will work as you intended.
c = Car.new("Ford")
c.description{"#{@model} is very good."}
puts c.show_description
# => Ford is very good.
By the way, you can simplify "#{@model} is very good."
to "#@model is very good."
.