Encapsulation in Ruby


class Encapsulation
    def get_value
        @value
    end

    def set_value value
        @value = value
        return nil
    end
end

encapsulation = Encapsulation.new
encapsulation.set_value(15)
puts encapsulation.get_value

In this code, you cannot mess around with the state of its variable @value. The only way you can access it is through get & set method (get_value, set_value). This is the encapsulation

Sample Result

15
Leave a comment

0 Comments.

Leave a Reply

( Ctrl + Enter )