What does "ARGF.class" mean?

J

Joey Zhou

ruby 1.9.2p180 (2011-02-18) [i386-mingw32]

irb(main):001:0> ARGF.class
=> ARGF.class

What does "ARGF.class" mean? Isn't ARGF a special IO object, whose class
is "IO", just like STDIN?

irb(main):002:0> STDIN.class
=> IO
 
D

David Jacobs

[Note: parts of this message were removed to make it a legal post.]

From the source code, I see this:

#define argf_of(obj) (*(struct argf *)DATA_PTR(obj))
#define ARGF argf_of(argf)


# ... ... later on

/*
* Hack to get rdoc to regard ARGF as a class:
* rb_cARGF = rb_define_class("ARGF", rb_cObject);
*/
rb_cARGF = rb_class_new(rb_cObject);
rb_set_class_path(rb_cARGF, rb_cObject, "ARGF.class");
rb_define_alloc_func(rb_cARGF, argf_alloc);

That's not a definitive answer, but it does look like the implementers are aware that some of ARGF's edges are hacks.
 
J

Joey Zhou

David Jacobs wrote in post #985706:
That's not a definitive answer, but it does look like the implementers
are aware that some of ARGF's edges are hacks.

I was confused with its class name "ARGF.class". If ARGF is an instance
of "ARGFClass", like nil is an instance of "NilClass", I won't
misunderstand it. "ARGF.class" doesn't seem to be a class name.
Thank you.
 
D

David Jacobs

[Note: parts of this message were removed to make it a legal post.]

Right, I understand. I'm just pointing out that ARGF is, in fact, not a real class, and that Ruby treats ARGF differently from real classes.
 
7

7stud --

Joey Zhou wrote in post #985705:
ruby 1.9.2p180 (2011-02-18) [i386-mingw32]

irb(main):001:0> ARGF.class
=> ARGF.class

What does "ARGF.class" mean? Isn't ARGF a special IO object, whose class
is "IO", just like STDIN?

irb(main):002:0> STDIN.class
=> IO

First note that your output is not ARGFCLASS. You really are getting no
output--if what you posted is correct; irb just output what you typed
in.

In ruby 1.8.6, I get Object for the output, but as you mentioned I would
have expected ARGF to be some kind of IO object.
 
G

Gary Wright

Joey Zhou wrote in post #985705:
ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
=20
irb(main):001:0> ARGF.class
=3D> ARGF.class
=20
What does "ARGF.class" mean? Isn't ARGF a special IO object, whose = class
is "IO", just like STDIN?
=20
irb(main):002:0> STDIN.class
=3D> IO
=20
First note that your output is not ARGFCLASS. You really are getting = no=20
output--if what you posted is correct; irb just output what you typed=20=

This is not a correct interpretation of the 1.9.2 output.

ARGF is an instance of a class and the class has the name 'ARGF.class'.
That is a very unusual class name which makes it a bit difficult to
interpret the IRB output.

IRB prints the result of calling #inspect on the last expression entered =
and
for a class that is just the name of the class:

ruby-1.9.2-p0 > ARGF.class
=3D> ARGF.class=20
ruby-1.9.2-p0 > a =3D ARGF.class; 1
=3D> 1=20
ruby-1.9.2-p0 > a.inspect
=3D> "ARGF.class"=20
ruby-1.9.2-p0 > a.class
=3D> Class=20
ruby-1.9.2-p0 > a.name
=3D> "ARGF.class"=20
ruby-1.9.2-p0 > ARGF.class.superclass
=3D> Object=20
ruby-1.9.2-p0 >=20

So ARGF is an instance of a class named 'ARGF.class' and that class is
just a subclass of Object. ARGF does respond to many of the same =
methods
that an instance of IO would respond to but that is just an example
of duck typing rather than inheritance.

ruby-1.9.2-p0 > (ARGF.class.instance_methods(false) & =
IO.instance_methods(false))
=3D> [:fileno, :to_i, :to_io, :each, :each_line, :each_byte, =
:each_char, :lines, :bytes, :chars, :read, :readpartial, :readlines, =
:gets, :readline, :getc, :getbyte, :readchar, :readbyte, :tell, :seek, =
:rewind, :pos, :pos=3D, :eof, :eof?, :binmode, :binmode?, :close, =
:closed?, :lineno, :lineno=3D, :external_encoding, :internal_encoding, =
:set_encoding]=20
ruby-1.9.2-p0 > ARGF.class.included_modules
=3D> [Enumerable, Kernel]=20


Gary Wright
 
7

7stud --

Gary Wright wrote in post #985859:
This is not a correct interpretation of the 1.9.2 output.

ARGF is an instance of a class and the class has the name 'ARGF.class'.
That is a very unusual class name which makes it a bit difficult to
interpret the IRB output.


Thanks for the correction...but I'll stand by my statement:
irb just output what you typed in, which is not at
all helpful.

Having a class actually named "ARGF.class" makes me think that ruby is
an ass.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,843
Latest member
WizcraftEntertainmentAgen

Latest Threads

Top