2

In the answer to this SO question Jörg W Mittag says:

There is no such thing as a constructor in Ruby.

I didn't think much of that, until I read the Wikipedia article on constructors, which states:

Immutable objects must be initialized in a constructor.

Since symbols are immutable in Ruby, how are they made? Wikipedia would seem to think they must be made with a constructor, yet Jörg says there are no constructors in Ruby.

I'm fairly new to OOP concepts and programming in general, so it possible I am missing something fairly basic, but from my perspective it there is a contradiction between these sources.

Community
  • 1
  • 1
trosborn
  • 1,658
  • 16
  • 21
  • 2
    A Wikipedia page like that on general concepts in OOP is not going to read perfectly correctly for every OOP language, especially when it seems to be written much more from the perspective of C++ than anything else. – Linuxios Feb 02 '15 at 22:42
  • 1
    Agreed. Unfortunately, the Wikipedia overgeneralizes because it kind of has to. Maybe "Immutable objects must be initialized in a constructor-like manner" would be a better statement. – Steve Feb 02 '15 at 22:50

2 Answers2

2

When people say

There is no such thing as a constructor in Ruby.

they mean that there is no special method type corresponding to a constructor, the way there is in C++ and Java.

In Ruby, the initialize method is, by convention, used to initialize a new object. However, it is not a special method -- it's a method just like any other method in Ruby.

mipadi
  • 398,885
  • 90
  • 523
  • 479
2

Symbols are 'initialized' in some dark inside part of the ruby runtime, that you don't need to worry about.

If you make your own immutable class in ruby, it would have to be initialized in it's "constructor", which in ruby means in it's #initialize method. The initialize method may or may not technically be a constructor, but it's the method that is called when the object is instantiated, which is the point the wikipedia article is making -- since an immutable object can't be changed once created, any state has to be set when it's created, not later.

Wikipedia article calls this 'a constructor', but different languages use different terminology or exact constructs, the wikipedia article wasn't written with ruby in mind. For ruby, "place you can set state on object instantiation" is typically initialize method. (I say 'typically', because you can do all kinds of crazy things in ruby if you try). The wikipedia article is still right that if an object is truly immutable, any setting of state happens to happen on object creation, not afterwords -- that's just the meaning of 'immutable', right?

That's for immutable classes implemented by ruby source code. But good luck finding an 'initialize' method for symbols. They don't work that way. They are just provided by ruby itself, by the runtime, and we don't need to worry about the details.

jrochkind
  • 22,799
  • 12
  • 59
  • 74
  • 1
    The thing is: since `initialize` is just a method like any other method (it's just a naming convention, after all, not in any way part of the language), this means that if `initialize` can mutate the state of the object (and it has to, even for immutable objects, in order to properly initialize), then every other method can do so, too. You can, of course, `freeze` the object either in `initialize` or `new`. So, "immutable" in Ruby is just a convention, apart from some "magic" objects like `Symbol`s, `Float`s, `Integer`s, etc. – Jörg W Mittag Feb 02 '15 at 23:09
  • It's true there is no built-in support for immutable objects in ruby. (Other than freeze, which is more of a tool you can use than finished built-in support, it's not usually sufficient on it's own). But I wouldn't say immutability is a 'convention', exactly. At any rate, this question wasn't about "How do I create immutable objects in ruby", if it were we could go into more depth about techniques, pitfalls, limitations, etc. There isn't really any built-in support for immutable objects in Java either -- you just write an implementation that results in immutability, same as ruby. – jrochkind Feb 02 '15 at 23:49