I
Intransition
First of all, I am curious. Is there any way to do the following but
still retain backward compatibility?
class Module
# unfortunately super doesn't work
alias ublicize ublic
alias rivatize rivate
alias rotect rotected
def public(name, &code)
define_method(name, &code) if code
publicize(name)
end
def private(name, &code)
define_method(name, &code) if code
privatize(name)
end
def protected(name, &code)
define_method(name, &code) if code
protect(name)
end
end
Example
class X
public :x do |*a|
p a
end
private :y do |*a|
p a
end
protected :z do |*a|
p a
end
end
Secondly, I guess 'def' was just nice because of it's uniformity
throughout modules and classes, but I have to wonder if the more Java
like to use of public/private/protected might ultimately be a bit more
beneficial (especially if they could be generalized to allow the
definition of others via meta-programming). Classes might look more
like:
class X
public_accessor :a
private_reader :b
protected_writer :c
public x(*a)
p a
end
private y(*a)
p a
end
protected z(*a)
p a
end
end
still retain backward compatibility?
class Module
# unfortunately super doesn't work
alias ublicize ublic
alias rivatize rivate
alias rotect rotected
def public(name, &code)
define_method(name, &code) if code
publicize(name)
end
def private(name, &code)
define_method(name, &code) if code
privatize(name)
end
def protected(name, &code)
define_method(name, &code) if code
protect(name)
end
end
Example
class X
public :x do |*a|
p a
end
private :y do |*a|
p a
end
protected :z do |*a|
p a
end
end
Secondly, I guess 'def' was just nice because of it's uniformity
throughout modules and classes, but I have to wonder if the more Java
like to use of public/private/protected might ultimately be a bit more
beneficial (especially if they could be generalized to allow the
definition of others via meta-programming). Classes might look more
like:
class X
public_accessor :a
private_reader :b
protected_writer :c
public x(*a)
p a
end
private y(*a)
p a
end
protected z(*a)
p a
end
end