'Uninitialized' attr_reader at class returning name of class instead of nil

Hi,

Just out of curiosity:

···

~~~
2.4.0 :001 > class T
2.4.0 :002?> attr_reader :name
2.4.0 :003?> end
2.4.0 :004 > T.name
=> "T"
~~~

Why is it that Ruby returns the name of the class instead of nil in this case?

Many thanks for any hints!

Regards,
Michael

Quoting Michael Schwarze (michael@schwarze-web.de):

Just out of curiosity:

~~~
2.4.0 :001 > class T
2.4.0 :002?> attr_reader :name
2.4.0 :003?> end
2.4.0 :004 > T.name
=> "T"
~~~

Why is it that Ruby returns the name of the class instead of nil in
this case?

Because with attr_reader you define an instance method, not a class
method.

If you write

t::new.name

you will get nil (the method is called on the newly created
object). If you call T.name you invoke the 'name' method on the class
object (which returns the name of the class).

Carlo

···

Subject: 'Uninitialized' attr_reader at class returning name of class instead of nil
  Date: Thu 27 Jul 17 12:54:29PM +0200

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

Hi, excuse me Im a beginner, but as i may think it should return a name
exactly because you told at attr_reader, and its class name

···

On Thu, Jul 27, 2017 at 1:54 PM, Michael Schwarze <michael@schwarze-web.de> wrote:

Hi,

Just out of curiosity:

~~~
2.4.0 :001 > class T
2.4.0 :002?> attr_reader :name
2.4.0 :003?> end
2.4.0 :004 > T.name
=> "T"
~~~

Why is it that Ruby returns the name of the class instead of nil in this
case?

Many thanks for any hints!

Regards,
Michael

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Because "Class.name" will always return the classes name, it does not touch
the attribute at all :slight_smile:
That aside, attributes are defined on instance level, so if you would have
called your attribute anything else than "name", it would have raised an
"undefined method" error.

···

On Thu, Jul 27, 2017 at 7:54 PM, Michael Schwarze <michael@schwarze-web.de> wrote:

Hi,

Just out of curiosity:

~~~
2.4.0 :001 > class T
2.4.0 :002?> attr_reader :name
2.4.0 :003?> end
2.4.0 :004 > T.name
=> "T"
~~~

Why is it that Ruby returns the name of the class instead of nil in this
case?

Many thanks for any hints!

Regards,
Michael

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Hi Fabian,

Because "Class.name" will always return the classes name, it does not touch the attribute at all :slight_smile:
That aside, attributes are defined on instance level, so if you would have called your attribute anything else than „name", it would have raised an "undefined method" error.

Thanks a lot, that makes it perfectly clear!

Regards,
Michael

···

Am 27.07.2017 um 13:05 schrieb Fabian Zitter <fabian.zitter@gmail.com>:

On Thu, Jul 27, 2017 at 7:54 PM, Michael Schwarze <michael@schwarze-web.de> wrote:
Hi,

Just out of curiosity:

~~~
2.4.0 :001 > class T
2.4.0 :002?> attr_reader :name
2.4.0 :003?> end
2.4.0 :004 > T.name
=> "T"
~~~

Why is it that Ruby returns the name of the class instead of nil in this case?

Many thanks for any hints!

Regards,
Michael

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Thank you, Fabian Zitter, good to know

···

On Thu, Jul 27, 2017 at 2:09 PM, Maria Tikhonova <ritz.rkraft@gmail.com> wrote:

Hi, excuse me Im a beginner, but as i may think it should return a name
exactly because you told at attr_reader, and its class name

On Thu, Jul 27, 2017 at 1:54 PM, Michael Schwarze <michael@schwarze-web.de > > wrote:

Hi,

Just out of curiosity:

~~~
2.4.0 :001 > class T
2.4.0 :002?> attr_reader :name
2.4.0 :003?> end
2.4.0 :004 > T.name
=> "T"
~~~

Why is it that Ruby returns the name of the class instead of nil in this
case?

Many thanks for any hints!

Regards,
Michael

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

And if you really want to define that method, you have options (of course).

# Reopen the eigenclass of 'T'
class <<T
  attr_reader :name
end

# .. or nested ..
class T
  class <<self
    attr_reader :name
  end
end

I don't actually know what these will break, but there you go.

Within the scope of the reopened eigenclass (aka singleton class)
methods like `attr_reader` and keywords like `alias` and `private`
(and `self`) make sense, and there's no ambiguity about @ivars either.

If you're cool with the weirdness of scopes and bindings, you can also
do this sort of thing:

def T.name
  'Dave'
end

class T
  def self.name
    'Fred'
  end
end

Cheers

···

On 27 July 2017 at 21:04, Carlo E. Prelz <fluido@fluido.as> wrote:

        Subject: 'Uninitialized' attr_reader at class returning name of class instead of nil
        Date: Thu 27 Jul 17 12:54:29PM +0200

Quoting Michael Schwarze (michael@schwarze-web.de):

Just out of curiosity:

~~~
2.4.0 :001 > class T
2.4.0 :002?> attr_reader :name
2.4.0 :003?> end
2.4.0 :004 > T.name
=> "T"
~~~

Why is it that Ruby returns the name of the class instead of nil in
this case?

Because with attr_reader you define an instance method, not a class
method.

If you write

t::new.name

you will get nil (the method is called on the newly created
object). If you call T.name you invoke the 'name' method on the class
object (which returns the name of the class).

Carlo

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/