Hacker News new | ask | show | jobs
by muchabi 4323 days ago
To add another example to the list, Ruby lets you list which attributes can be read/written and which ones can only be read on top. Something like.

  class Car
      attr_reader :model, :company # only read
      attr_accessor :color # read and write
  end
1 comments

It's worth noting that in Ruby the difference between attr_reader and attr_writer is not the same as read only and write only. You can write to something set with attr_reader. All attr_writer does is allow complete reassignment of that particular instance variable.

This is pretty trivial so bear with me... If you have something that looks like this...

    class Person
      attr_reader :children

      def initialize
        @children = []
      end
    end
You can then do this...

    x = Person.new
    x << "Bobby"
You will have then altered the value of @children without completely reassigning it.