The Crystal Programming Language Forum

Private properties

Is it possible to make a property private like

class Car
  property color : String
  private property price : Float64
  .
end

Yes that code works fine. It would only allow setting/getting the value within Car. Another option if the property is only going to be used within Car is to just define the ivar without a getter/setter.

class Car
  property color : String
  @price : Float64

  def initialize(@price : Float64, @color : String); end

  def do_something
    @price = 1.123

    @price * 10
  end
end

This would make it so the price is only set when instantiating the class.

Now I feel silly, I should have tried it first instead of asking.
Thanks for the help.