is there any way to specifiy that a method can not be overrided? perfereably
without throwing an error; rather if you try to override the method it simply
dosen’t happen.
···
–
tom sawyer, aka transami
transami@transami.net
is there any way to specifiy that a method can not be overrided? perfereably
without throwing an error; rather if you try to override the method it simply
dosen’t happen.
–
tom sawyer, aka transami
transami@transami.net
Kind of evil, but works:
batsman@tux-chan:/tmp$ expand -t 2 f.rb
module NoOverloading
@@no_override =
def no_redef(*args)
args.each { |i| @@no_override << i }
end
def method_added(id)
return if @@no_override.index(id) == nil
remove_method id
end
end
class A
extend NoOverloading
def a; puts “A#a”; end
def b; puts “A#b”; end
no_redef :a, :b # do it after defining the methods ![]()
end
class B < A
def a; puts “B#a”; end
def b; puts “B#b”; end
end
b = B.new
b.a
b.b
batsman@tux-chan:/tmp$ ruby f.rb
A#a
A#b
On Sun, Feb 02, 2003 at 04:51:08PM +0900, Tom Sawyer wrote:
is there any way to specifiy that a method can not be overrided? perfereably
without throwing an error; rather if you try to override the method it simply
dosen’t happen.
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ /| __/ __| '__ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
‘Ooohh… “FreeBSD is faster over loopback, when compared to Linux
over the wire”. Film at 11.’
– Linus Torvalds
[…]
Forget that code, the following is much better as now:
And it gives a nice example of what class instance variables are good
for ![]()
Drawback: method definition is somewhat slower, but only definition.
batsman@tux-chan:/tmp$ expand -t 2 f.rb
module NoOverloading
def no_redef(*args)
@no_override ||=
args.each { |i| @no_override << i }
end
def no_redef_list
supmethods =
(ancestors - [self]).each do |i|
begin
supmethods += ( i.no_redef_list || )
rescue NameError
end
end
(@no_override || ) + supmethods
end
def inherited sub
class << sub
def method_added(id)
return if no_redef_list.index(id) == nil
remove_method id
end
end
end
end
class A
extend NoOverloading
no_redef :a, :b
def a; puts “A#a”; end
def b; puts “A#b”; end
def c; puts “A#c”; end
def d; puts “A#d”; end
end
class B < A
def a; puts “B#a”; end
def b; puts “B#b”; end
def c; puts “B#c”; end
def d; puts “B#d”; end
no_redef :c
end
class C < B
def a; puts “C#a”; end
def b; puts “C#b”; end
def c; puts “C#c”; end
def d; puts “C#d”; end
end
c = C.new
c.a
c.b
c.c
c.d
batsman@tux-chan:/tmp$ ruby f.rb
A#a
A#b
B#c
C#d
On Sun, Feb 02, 2003 at 06:41:45PM +0900, Mauricio Fernández wrote:
On Sun, Feb 02, 2003 at 04:51:08PM +0900, Tom Sawyer wrote:
is there any way to specifiy that a method can not be overrided? perfereably
without throwing an error; rather if you try to override the method it simply
dosen’t happen.Kind of evil, but works:
–
_ ___ __ | | ___ _ __ ___ __ _ _ __
'_ \ /| __/ __| '__ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
martin@bdsi.com (no longer valid - where are you now, Martin?)
– from /usr/src/linux/drivers/cdrom/mcd.c
your NoOverloading code looks fantastic!
i must say i expected to be told it wasn’t possible. awesome that you were
able to put together a nice solution so quickly. thank you. thank you. thank
you. definitely going into my general library of tools. and truly this (or
its kin written in c) should be considered for inclusion in Ruby proper!
-transmai
On Sunday 02 February 2003 03:34 am, Mauricio Fernández wrote:
Forget that code, the following is much better as now:
- you can use no_redef before defining methods in the base class, only
derived classes are affected- you can “stack” the restrictions, ie if you have C < B < A, you can
specify that some methods of A shouldn’t be overloaded, and later
that some in B shouldn’t be either, and it’ll just work.And it gives a nice example of what class instance variables are good
forDrawback: method definition is somewhat slower, but only definition.
batsman@tux-chan:/tmp$ expand -t 2 f.rb
module NoOverloading
def no_redef(*args)
@no_override ||=
args.each { |i| @no_override << i }
enddef no_redef_list
supmethods =
(ancestors - [self]).each do |i|
begin
supmethods += ( i.no_redef_list || )
rescue NameError
end
end
(@no_override || ) + supmethods
enddef inherited sub
class << sub
def method_added(id)
return if no_redef_list.index(id) == nil
remove_method id
end
end
endend
–
tom sawyer, aka transami
transami@transami.net
I’m quite happy with that code, as I get to mix some of my all-time
favorite idioms/features into it ![]()
In a three words, singletons classes rule!
I felt so satisfied to get this to work that I put it in the Wiki
Now comes the important question: why do you need that?
If you find a good use-case scenario, you can start lobbying to get
people to use it and then convince matz to make it standard and render
me somewhat famous ![]()
On Sun, Feb 02, 2003 at 09:35:26PM +0900, Tom Sawyer wrote:
On Sunday 02 February 2003 03:34 am, Mauricio Fernández wrote:
your NoOverloading code looks fantastic!
i must say i expected to be told it wasn’t possible. awesome that you were
able to put together a nice solution so quickly. thank you. thank you. thank
you. definitely going into my general library of tools. and truly this (or
its kin written in c) should be considered for inclusion in Ruby proper!
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ /| __/ __| '__ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
Software is like sex; it’s better when it’s free.
– Linus Torvalds
It looks much like the effect of “final” keyword in Java, however
without an error message. May be useful in some cases.
Thanks,
Gennady.
On Sunday, Feb 2, 2003, at 08:01 US/Pacific, Mauricio Fernández wrote:
On Sun, Feb 02, 2003 at 09:35:26PM +0900, Tom Sawyer wrote:
On Sunday 02 February 2003 03:34 am, Mauricio Fernández wrote:
your NoOverloading code looks fantastic!
i must say i expected to be told it wasn’t possible. awesome that you
were
able to put together a nice solution so quickly. thank you. thank
you. thank
you. definitely going into my general library of tools. and truly
this (or
its kin written in c) should be considered for inclusion in Ruby
proper!I’m quite happy with that code, as I get to mix some of my all-time
favorite idioms/features into it
- class instance variables: have been in love for a long time w/ them
- singleton methods, especially when added dynamically to a class
- nesting a singleton method def inside a method definition
In a three words, singletons classes rule!
I felt so satisfied to get this to work that I put it in the Wiki
http://www.rubygarden.org/ruby?PreventingOverloadingNow comes the important question: why do you need that?
If you find a good use-case scenario, you can start lobbying to get
people to use it and then convince matz to make it standard and render
me somewhat famous–
_ ___ __ | | ___ _ __ ___ __ _ _ __
'_ \ /| __/ __| '__ \ / ` | ’
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot comSoftware is like sex; it’s better when it’s free.
– Linus Torvalds
Now comes the important question: why do you need that?
in my case it is because i have a class that gets some dynamically created
methods based on whether or not certain criteria are met. if it is then these
methods need to be non-overridable, if not then the methods defined in the
subclasses can be used. i now that there’s surly other ways to deal with
this, but it occured to me the have methods that can’t be overrided was one
way to do it and i was wondering if it could be done…and sure enough!
If you find a good use-case scenario, you can start lobbying to get
people to use it and then convince matz to make it standard and render
me somewhat famous
well, if i think of something succinct (sp?) then i will let the world know
for your sake ![]()
On Sunday 02 February 2003 09:01 am, Mauricio Fernández wrote:
–
tom sawyer, aka transami
transami@transami.net
I’ve been thinking on how to make this ressilient even to malicious
attempts to redefine things (I guess one possible application would be
creating a sandbox for rubyrobots or such).
It turns out it is quite easy to break as it stands:
class D < A
class << self
def method_added id
end
end
def a; puts “I won!”; end
end
D.new.a # ==> “I won!”
So here’s the fix:
module NoOverloading
def inherited << sub
def method_added(id)
return if no_redef_list.index(id) == nil
remove_method id
end
self.freeze
end
end
A.freeze
NoOverloading.freeze
this makes absolutely impossible to redefine #a, AFAIK. However, it also
means that you can no longer create class methods.
Something more elegant would have been
module NoOverloading
def inherited sub
class << sub
class << self
# the singleton’s singleton class!!!
def method_added id
remove_method id if id == :method_added
end
self.freeze
end
end
def method_added(id)
return if no_redef_list.index(id) == nil
remove_method id
end
end
end
But this doesn’t work as it seems the singleton’s singleton method method_added is
not called when a new singleton method is added.
Now, what are singletons’ singletons useful for?
On Mon, Feb 03, 2003 at 06:23:35AM +0900, Tom Sawyer wrote:
On Sunday 02 February 2003 09:01 am, Mauricio Fernández wrote:
Now comes the important question: why do you need that?
in my case it is because i have a class that gets some dynamically created
methods based on whether or not certain criteria are met. if it is then these
methods need to be non-overridable, if not then the methods defined in the
subclasses can be used. i now that there’s surly other ways to deal with
this, but it occured to me the have methods that can’t be overrided was one
way to do it and i was wondering if it could be done…and sure enough!
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ /| __/ __| '__ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
It turns out it is quite easy to break as it stands:
class D < A
class << self
def method_added id
end
end
def a; puts “I won!”; end
endD.new.a # ==> “I won!”
So here’s the fix:
module NoOverloading
def inherited << sub
…
self.freeze
end
endA.freeze
NoOverloading.freezethis makes absolutely impossible to redefine #a, AFAIK. However, it also
means that you can no longer create class methods.
For the record, there’s a second way to break it: using alias_method
or alias. You can even remove alias_method from Module, but alias is
a keyword so there’s no way to get rid of that.
On Tue, Feb 04, 2003 at 12:26:37AM +0900, Mauricio Fernández wrote:
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ /| __/ __| '__ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
C:> WIN
Bad command or filename
C:> LOSE
Loading Microsoft Windows …
Just a basic question – what is a tuple? How is it different from an
array in Ruby (if at all)? Everything I’ve read in my research on
google seems to assume that the reader knows what a tuple is and why it
is useful.
Regards,
Mark Wilson
“Mark Wilson” mwilson13@cox.net wrote in message
news:535620B1-37BE-11D7-BE1F-000393876156@cox.net…
Just a basic question – what is a tuple? How is it different from an
array in Ruby (if at all)? Everything I’ve read in my research on
google seems to assume that the reader knows what a tuple is and why it
is useful.Regards,
Mark Wilson
Tuples are immutable lists.
“Tuples have many uses. For example: (x, y) coordinate pairs, employee
records from a database, etc. Tuples, like strings, are immutable: it is not
possible to assign to the individual items of a tuple (you can simulate much
of the same effect with slicing and concatenation, though). It is also
possible to create tuples which contain mutable objects, such as lists.”
← from Python docs
A row.
On Tue, 4 Feb 2003, Mark Wilson wrote:
Just a basic question -- what is a tuple? How is it different from an
array in Ruby (if at all)? Everything I've read in my research on
google seems to assume that the reader knows what a tuple is and why it
is useful.Regards,
Mark Wilson
Thank you for the prompt response. Could one then implement a tuple in
Ruby as follows:
class Tuple
attr_reader :tuple
def initialize(*args)
@tuple = *args
end
end
On Monday, February 3, 2003, at 04:35 PM, Bob X wrote:
[snip]
Tuples are immutable lists.
“Tuples have many uses. For example: (x, y) coordinate pairs, employee
records from a database, etc. Tuples, like strings, are immutable: it
is not possible to assign to the individual items of a tuple (you can
simulate much of the same effect with slicing and concatenation,
though). It is also possible to create tuples which contain mutable
objects, such as lists.”
← from Python docs
In Haskell,
lists are homogeneous and arbitrary length.
tuples are heterogeneous and fixed length.
Neither is mutable.
Nothing in Haskell is mutable.
no, that’s not immutable, you need something more like this:
module Enumerable
def deepfreeze
self.each do |o|
if o != self then
o.deepfreeze
else
o.freeze
end
end
self.freeze
end
end
class Object
def deepfreeze
self.freeze
end
end
class Tuple < Array
def initialize(*stuff)
super()
self.push(*stuff)
self.deepfreeze
end
end
(comes from here)
http://sourceforge.net/project/shownotes.php?group_id=50485&release_id=124575
Mark Wilson (mwilson13@cox.net) wrote:
On Monday, February 3, 2003, at 04:35 PM, Bob X wrote:
[snip]
Tuples are immutable lists.
“Tuples have many uses. For example: (x, y) coordinate pairs, employee
records from a database, etc. Tuples, like strings, are immutable: it
is not possible to assign to the individual items of a tuple (you can
simulate much of the same effect with slicing and concatenation,
though). It is also possible to create tuples which contain mutable
objects, such as lists.”
← from Python docsThank you for the prompt response. Could one then implement a tuple in
Ruby as follows:class Tuple
attr_reader :tuple def initialize(*args) @tuple = *args endend
–
Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
Heterogenous, but they have a fixed type for each position
martin
MetalOne jcb@iteris.com wrote:
In Haskell,
lists are homogeneous and arbitrary length.
tuples are heterogeneous and fixed length.
WARNING: This e-mail contains an opinion.
Nothing in Haskell is readable either.
Then again… I do find Haskell a refreshing change.
-----Original Message-----
From: MetalOne [mailto:jcb@iteris.com]
Sent: Wednesday, February 05, 2003 11:05 AM
To: ruby-talk ML
Subject: Re: What is a tuple?
In Haskell,
lists are homogeneous and arbitrary length.
tuples are heterogeneous and fixed length.
Neither is mutable.
Nothing in Haskell is mutable.
“MetalOne” jcb@iteris.com wrote in message
news:92c59a2c.0302041648.a0608ce@posting.google.com…
In Haskell,
lists are homogeneous and arbitrary length.
tuples are heterogeneous and fixed length.Neither is mutable.
Nothing in Haskell is mutable.
is tuple same as perl’s %hash?
Wow - talk about mileage. Personally, Haskell has the most pleasing
syntax I’ve seen in any language.
martin
Brian brian@save-the-orcs.com wrote:
WARNING: This e-mail contains an opinion.
Nothing in Haskell is readable either.
Then again… I do find Haskell a refreshing change.
Nothing like it! A tuple is a fixed-length list, with some other
properties. (See rest of thread.)
Gavin
On Thursday, February 6, 2003, 11:30:35 PM, J(ust) wrote:
“MetalOne” jcb@iteris.com wrote in message
news:92c59a2c.0302041648.a0608ce@posting.google.com…In Haskell,
lists are homogeneous and arbitrary length.
tuples are heterogeneous and fixed length.Neither is mutable.
Nothing in Haskell is mutable.
is tuple same as perl’s %hash?