Thought question: Where does "new" come from?

“MikkelFJ” mikkelfj-anti-spam@bigfoot.com wrote in message
news:3d5d07d8$0$33591$edfadb0f@dspool01.news.tele.dk…

Thanks for the clarification.

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
}

class << anon
def my_name
puts @my_name
end
end

anon.new.inst_meth # an_inst
anon.my_name # anon
A.const # anon’s_const

/Christoph

“Christoph” wrote

  • ups;)
···

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
}

class << anon
def my_name
puts @my_name
end
end

anon.new.inst_meth # an_inst
anon.my_name # anon
A.const # anon’s_const

/Christoph

“Christoph” chr_news@gmx.net wrote in message
news:ajj3ro$c72$00$1@news.t-online.com

What about anonymous classes ? They sort of
make your argument mood IMHO.

Ruby is very dynamically type so you can only stretch the analogy so far.

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.

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

class A; end

The need to do this actually stresses the point that it has a static nature.

Mikkel

“MikkelFJ” wrote

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

class << anon
def my_name
puts @my_name
end
end

anon.new.inst_meth # an_inst
anon.my_name # anon
anon2.const # anon’s_const

just for the heck of it, deanonymize anon …

p anon # #Class:0x277b6f8

A = anon

p anon # A

A.my_name # anon

/Christoph

I hope that you know that you are not forced to use the evil eval :slight_smile:

anon.class_eval <<-ENd

   anon.class_eval do |klass|

is just fine :slight_smile:

Guy Decoux

“Christoph” chr_news@gmx.net wrote in message
news:ajlet2$llu$05$1@news.t-online.com

just for the heck of it, deanonymize anon …

p anon # #Class:0x277b6f8

A = anon

there it was - you did at again - you made it constant (i.e. static) :slight_smile:

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).

Mikkel

“ts” wrote

I hope that you know that you are not forced to use the evil eval :slight_smile:

Yeah, but some people seems to have made a pact with the devil;-)

anon.class_eval <<-ENd

anon.class_eval do |klass|

In this case you actually need the evil eval - at least if want to
get the silly output I intended to;-). Lets say (in 1.7 )

···
anon = Class.new {
    const_set :Const, "Anons Const"
}

Const = “Outer Const”
class A; end

anon.class_eval {
def A.I_am_picking_up_the_Outer_Const
p Const
end
}
anon.class_eval %{
def A.I_am_picking_up_Anons_Const
p Const
end
}

A.I_am_picking_up_the_Outer_Const # => “Outer Const”
A.I_am_picking_up_Anons_Const # => “Anons Const”


/Christoph

“MikkelFJ” wrote in

p anon # #Class:0x277b6f8

A = anon

there it was - you did at again - you made it constant (i.e. static) :slight_smile:

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.

/Christoph