Equivalent of C# readonly

Has there ever been discussion of adding this to Ruby? I couldn’t find
anything by searching this forum or the issue log.

attr_reader is great but sometimes it’s good to prevent assignment of
@field within a class outside initialize.

I am not sure what this does. Can you explain what the C# readonly thing
is doing exactly?

If I understood you correctly, you mean a way to disable using an
auto-generated setter method?

It doesn’t impact code outside the class so it’s not related to
setters/attr_accessor. It prevents assignments to that instance variable
(field in C#) outside the constructor/initializer. It doesn’t prevent
mutating what’s already assigned (that would be an immutable
collection’s job in C# or I guess .freeze’s job in Ruby).

I think the const keyword in ES6 does something similar.

It can help control mutation a bit. They(MSFT) also provide a Lazy
helper such that you can setup a value to be lazily initialized, but you
still can restrict what that value will eventually be in the
constructor/initializer.

As a C# developer i think what you looking for in ruby is Object#freeze

Take a look at docs how it works.

http://ruby-doc.org/core-2.3.0/Object.html#method-i-freeze

Fabio, freeze does not take care of this scenario.

This example below would not compile in C# with an error on the
accidental method.

Example:

class Howdy
attr_reader :stuff

def initialize
    @stuff = 'howdy'.freeze
end

def accidental
    # A C# read only field would not let me do this
    @stuff = 'nope'.freeze
end

end

obj = Howdy.new
obj.accidental
puts obj.stuff


Ruby, I suppose it would need to be a attr_init_only type decorator that
prevents doing @field = with anything declared as attr_init_only

Hi Brady,

There is no idiomatic readonly equivalent in ruby. It is completely
different concept and language.

You can get a ‘get’ accessors if you need them by either creating a
method or using ‘attr_reader’ function.

Ruby is not one of that ‘type safe’ languages and you should change the
way you think of programming using ruby.

In order to stop holy war my question would be, what for do you need
readonly? I think there is a better/simpler way to do thing you need.

@Taras

I know Ruby doesn’t have the equivalent. That’s why I wrote this post to
generate discussion about adding it.

I don’t see this as a typed vs. non typed issue. It’s a mutability
issue. It has very little to do with locking down based on a type or
anything like that.

Get accessors don’t really solve the problem readonly does because
readonly is all about controlling mutation within the class itself.

I don’t think there is a holy war here. Just a discussion about what
might be a good idea to pull (or not) from other languages. I think the
value of it is that it allows you to make parts of your classes more
immutable without going to Haskell, Clojure, etc. I can freeze something
and that helps but all the benefit of freeze is gone if I accidentally
reassign @ivar to something else.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs