C
Csaba Henk
One of the things I deeply like in ruby that succintness is an intrinsic
design principle.
Ruby is just the opposite of that what the infamous standard Java
"hello world" encapsulates. In general. But it seems to me that there
are some problem with proper access to constants and class methods and
succintness.
See a naive code:
####
class A
C = 0
def self.bar
""
end
def foo
[A.bar,C]
end
end
class B < A
end
B.new.foo # => ["",0]
####
-- this is succint, and works as it should. But what if we make up our
mind so that B should roll its own?
####
class B
C = 1
def self.bar
"x"
end
end
B.new.foo # => ["",0]
# bummer...
####
If we want the reference to the constant and the class method be
properly dispatched on B (provided B has them), we have to modify
A#foo as follows:
####
class A
def foo
[self.class.bar, self.class::C]
end
end
B.new.foo # => ["x",1]
# that's it!
####
But succintess is lost here. Does anyone know a better way of doing it?
What's extra annoyance that you have to write the longish "self.class",
and not just "class", as this latter would be mistaken with the reserved
word "class". Why is there no an alternative for Object#class which
doesn't collide with a reserved word? (There is Object#type, but that's
deprecated, and you are warned to use Object#class.)
Csaba
design principle.
Ruby is just the opposite of that what the infamous standard Java
"hello world" encapsulates. In general. But it seems to me that there
are some problem with proper access to constants and class methods and
succintness.
See a naive code:
####
class A
C = 0
def self.bar
""
end
def foo
[A.bar,C]
end
end
class B < A
end
B.new.foo # => ["",0]
####
-- this is succint, and works as it should. But what if we make up our
mind so that B should roll its own?
####
class B
C = 1
def self.bar
"x"
end
end
B.new.foo # => ["",0]
# bummer...
####
If we want the reference to the constant and the class method be
properly dispatched on B (provided B has them), we have to modify
A#foo as follows:
####
class A
def foo
[self.class.bar, self.class::C]
end
end
B.new.foo # => ["x",1]
# that's it!
####
But succintess is lost here. Does anyone know a better way of doing it?
What's extra annoyance that you have to write the longish "self.class",
and not just "class", as this latter would be mistaken with the reserved
word "class". Why is there no an alternative for Object#class which
doesn't collide with a reserved word? (There is Object#type, but that's
deprecated, and you are warned to use Object#class.)
Csaba