Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I want to create a simple linked list type of an object in ruby; where an instance variable in class points to another instance of same type.</p> </blockquote> <p>Just a quick note: the word <em>type</em> is a very dangerous word in Ruby, especially if you come from Java. Due to an historic accident, the word is used both in dynamic typing and in static typing to mean two only superficially related, but very different things.</p> <p>In dynamic typing, a type is a label that gets attached to a <em>value</em> (<em>not</em> a reference).</p> <p>Also, in Ruby the concept of type is much broader than in Java. In Java programmer's minds, "type" means the same thing as "class" (although that's not true, since Interfaces and primitives are also types). In Ruby, "type" means "what can I do with it".</p> <p>Example: in Java, when I say something is of type <em>String</em>, I mean it is a direct instance of the <code>String</code> class. In Ruby, when I say something is of type <em>String</em>, I mean it is either</p> <ul> <li>a direct instance of the <code>String</code> class or </li> <li>an instance of a subclass of the <code>String</code> class or </li> <li>an object which responds to the <code>#to_str</code> method or </li> <li>an object which behaves indistinguishably from a String.</li> </ul> <blockquote> <p>I want to populate and link all nodes; before the constructor is called and only once. Something that we'd usually do in Java Static block.</p> </blockquote> <p>In Ruby, everything is executable. In particular, there is no such thing as a "class declaration": a class body is just exectuable code, just like any other. If you have a list of method definitions in your class body, those are not declarations that are read by the compiler and then turned into a class object. Those are expressions that get executed by the evaluator one by one.</p> <p>So, you can put any code you like into a class body, and that code will be evaluated when the class is created. Within the context of a class body, <code>self</code> is bound to the class (remember, classes are just objects like any other).</p> <blockquote> <p>Initialize method is a constructor signature in ruby. Are there any rules around them? Like in Java you cannot call another constructor from a constructor if its not the first line (or after calling the class code?)</p> </blockquote> <p>Ruby doesn't <em>have</em> constructors. Constructors are just factory methods (with stupid restrictions); there is no reason to have them in a well-designed language, if you can just use a (more powerful) factory method instead.</p> <p>Object construction in Ruby works like this: object construction is split into two phases, <em>allocation</em> and <em>initialization</em>. Allocation is done by a public class method called <code>allocate</code>, which is defined as an instance method of class <code>Class</code> and is generally <em>never</em> overriden. It just allocates the memory space for the object and sets up a few pointers, however, the object is not really usable at this point.</p> <p>That's where the initializer comes in: it is an instance method called <code>initialize</code>, which sets up the object's internal state and brings it into a consistent, fully defined state which can be used by other objects.</p> <p>So, in order to fully create a new object, what you need to do is this:</p> <pre><code>x = X.allocate x.initialize </code></pre> <p>[Note: Objective-C programmers may recognize this.]</p> <p>However, because it is too easy to forget to call <code>initialize</code> and as a general rule an object should be fully valid after construction, there is a convenience factory method called <code>Class#new</code>, which does all that work for you and looks something like this:</p> <pre><code>class Class def new(*args, &amp;block) obj = alloc obj.initialize(*args, &amp;block) return obj end end </code></pre> <p>[Note: actually, <code>initialize</code> is private, so reflection has to be used to circumvent the access restrictions like this: <code>obj.send(:initialize, *args, &amp;block)</code>]</p> <p>That, by the way, is the reason why to construct an object you <em>call</em> a public class method <code>Foo.new</code> but you <em>implement</em> a private instance method <code>Foo#initialize</code>, which seems to trip up a lot of newcomers.</p> <p>To answer your question: since an initializer method is just a method like any other, there are absolutely no restrictions as to what you can do whithin an initializer, in particular you can call <code>super</code> whenever, wherever, however and how often you want.</p> <p>BTW: since <code>initialize</code> and <code>new</code> are just normal methods, there is no reason why they need to be called <code>initialize</code> and <code>new</code>. That's only a convention, although a pretty strong one, since it is embodied in the core library. In your case, you want to write a collection class, and it is quite customary for a collection class to offer an alternative factory method called <code>[]</code>, so that I can call <code>List[1, 2, 3]</code> instead of <code>List.new(1, 2, 3)</code>.</p> <p>Just as a side note: one obvious advantage of using normal methods for object construction is that you can construct instances of anonymous classes. This is not possible in Java, for absolutely no sensible reason whatsoever. The only reason why it doesn't work is that the constructor has the same name as the class, and anonymous classes don't have a name, ergo there cannot be a constructor.</p> <p>Although I am not quite sure why you would need to run anything before object creation. Unless I am missing something, shouldn't a list basically be</p> <pre><code>class List def initialize(head=nil, *tail) @head = head @tail = List.new(*tail) unless tail.empty? end end </code></pre> <p>for a Lisp-style cons-list or </p> <pre><code>class List def initialize(*elems) elems.map! {|el| Element.new(el)} elems.zip(elems.drop(1)) {|prv, nxt| prv.instance_variable_set(:@next, nxt)} @head = elems.first end class Element def initialize(this) @this = this end end end </code></pre> <p>for a simple linked list?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload