3

Does the Crystal programming language have an equivalent to Ruby's attribute accessor methods? More specifically, does Crystal have equivalents to the following?

  • attr_accessor
  • attr_reader
  • attr_writer

?

1
  • 2
    You mean Object#getter, Object#setter, and Object#property. Sure its in the Docs May 10 at 20:52

1 Answer 1

9

Yes, they're defined as macro.

Basically:

ruby crystal
attr_accessor property
attr_reader getter
attr_writer setter

Example

class Person
  property name
end

is equal to

class Person
  def name=(@name)
  end

  def name
    @name
  end
end

For more details see the Reference

3
  • It is my sincere wish that you are currently utilizing Crystal. I have a straightforward inquiry: Is it now possible to run Crystal on a Windows operating system? The last time I checked, this was not supported. May 11 at 9:49
  • 2
    @Rajagopalan I am using Crystal, and it's possible to use it on Windows, but unfortunately I might not be a good judge on how enjoyable it really is (dll support etc) because I myself don't often use Windows. It can always be used to almost full potential if you use WSL, which might or not qualify as proper "Windows". Long story short, it doesn't have first class support yet but its getting there and ppl are pushing hard to get this done. Quote: "All the main platform features for Windows are finished!" Read about it here: github.com/crystal-lang/crystal/issues/5430 May 11 at 10:40
  • 1
    Thanks @Andre_Wildberg. I should have known Crystal would make it that easy. May 11 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.