The term “static” is misleading. Static (as used in Java and C++)
refers to stand-alone functions that are in the scope of a class, but
can be invoked without any reference to an object instance. No such
thing exists in Ruby.
This isn’t exactly true. The C++ class can have static data members. If you
like, you can call these part of the static object instance. You can call
the functions without a reference to an instance because the compiler
implicitly knows the location of the static instance, still you give the
name of class, just as you do in Ruby. When I use the term static it relates
to the statically allocated nature (or preallocated globally known nature).
What about anonymous classes ? They sort of
make your argument mood IMHO.
···
anon = Class.new
class A; end
anon.class_eval { @my_name = “anon”
def inst_meth
puts “an inst”
end
Const = “anon’s_const”
def A.const
puts Const
end
}
Ruby is very dynamically type so you can only stretch the analogy so far.
I am worried that your analogy might be rather misleading.
The scoping rules for so called ``nested’’ classes or modules
are radically different (they are only an illusion based on coding
convention and strictly speaking don’t exist at all ) from the
so called static scoping rules of C++'s or Java’s inner
classes …
class << anon
def my_name
puts @my_name
end
end
What you do in the above is to access the Singleton class of the
instantiated object, which is different.
To you mean different to accessing the Singleton class
class << A
def an_other_meth
end
end
of the non anonymous Class object A in order to define
a (supposably static) class method? These two construct
neither look nor work any differently …
anon.new.inst_meth # an_inst
anon.my_name # anon
A.const # anon’s_const
This is the static class, but it is just the same as before. After all you
did earlier declare
My point was that the defining scope'' the class method const"
of the static'' Class object "A" was a local Class object anon’’ -
b.t.w. other A class methods might have a different
“defining scope”.
class A; end
The need to do this actually stresses the point that it has a static nature.
There was no need to do this. I might as well have written
···
anon = Class.new
anon2 = Class.new
anon.class_eval <<-ENd @my_name = “anon”
def inst_meth
puts “an inst”
end
Const = “anon’s_const”
def anon2.const
puts Const
end
ENd
there it was - you did at again - you made it constant (i.e. static)
I get your point - class definitions are dynamic in nature and what I call
static behaves like any other class definition. But that was not my focus at
all. My concern was how classes get created automatically - the mysterious
origin of self.
Anyway I do not have any strong feelings for the word static - I just argued
why I originally use the term. I still see constant class instance
associated with a class as being somewhat static - but I have now learned
that the correct term is Meta Class (which incidently doesn’t make
particularly more sense to me than static).
there it was - you did at again - you made it constant (i.e. static)
I get your point - class definitions are dynamic in nature and what I call
static behaves like any other class definition. But that was not my focus at
all. My concern was how classes get created automatically - the mysterious
origin of self.
Anyway I do not have any strong feelings for the word static - I just argued
why I originally use the term. I still see constant class instance
associated with a class as being somewhat static - but I have now learned
If you want to be picky there is no such thing as a ``constant class instance’’
its more like that there are constants whose value happens to be a class
and Ruby’s constants aren’t that constant after all.
···
class A;end
this defines a constant on the class Object level -i.e.
p Object.const_get(:A) # A
lets get rid of this constant but make sure that the
associated class is not GC’ed into obviation
anon_A = A
class Object
remove_const :A
end
begin
p A
rescue NameError => mes
puts mes # some complain about missing constant A
end
the A constant slot is free again …
class A;end
and we get
p A # A
p anon_A # A
however
p A != anon_A # true
In the end I agree with you that in practice Ruby seems to work
pretty much like a ``static Class system’’ as far as inner classes
are concernt - but it’s all an intended illusion - apparently good
enough for Dave to pretend in RDoc that Ruby inner class
scopes work like C++ or Java ones.