Final in ruby

Hi!

I’ve seen so far, that a ruby method can be hacked to be
“unchangeable”.
Will ruby some day support some kind of predicate or modifier, so I can
protect my method beeing overwritten?

Gergo

±[Kontra, Gergely @ Budapest University of Technology and Economics]-+

    Email: kgergely@mcl.hu,  kgergely@turul.eet.bme.hu          |

URL: turul.eet.bme.hu/~kgergely Mobile: (+36 20) 356 9656 |
±------“Olyan langesz vagyok, hogy poroltoval kellene jarnom!”-------+
.
Magyar php mirror es magyar php dokumentacio: http://hu.php.net

Why would you want that?

  • Ryan
···

On 2002.10.15, Kontra, Gergely kgergely@mlabdial.hit.bme.hu wrote:

Hi!

I’ve seen so far, that a ruby method can be hacked to be
“unchangeable”.

Will ruby some day support some kind of predicate or modifier, so I can
protect my method beeing overwritten?

Will ruby some day support some kind of predicate or modifier, so I can
protect my method beeing overwritten?

Why do you want this, this can be usefull to modify a method. Imagine that
someone write a stupid module like this :

pigeon% cat b.rb
#!/usr/bin/ruby
require 'mwrap'

Array.mwrap(:push, :stop) do |args|
   if args == 12
      raise ArgumentError, "12 is not valid",
   end
   next_mwrapper 12
end

a =
4.times {|i| a.push i }
p a
begin
   a.push 12
rescue
   p $!
end

Array.munwrap :push
a.push 6
p a
pigeon%

pigeon% b.rb
[12, 12, 12, 12]
#<ArgumentError: 12 is not valid>
[12, 12, 12, 12, 6]
pigeon%

this can be usefull :slight_smile:

Guy Decoux

Kontra, Gergely wrote:

Hi!

I’ve seen so far, that a ruby method can be hacked to be
“unchangeable”.
Will ruby some day support some kind of predicate or modifier, so I can
protect my method beeing overwritten?

Gergo

±[Kontra, Gergely @ Budapest University of Technology and Economics]-+

    Email: kgergely@mcl.hu,  kgergely@turul.eet.bme.hu          |

URL: turul.eet.bme.hu/~kgergely Mobile: (+36 20) 356 9656 |
±------“Olyan langesz vagyok, hogy poroltoval kellene jarnom!”-------+
.
Magyar php mirror es magyar php dokumentacio: http://hu.php.net

Java gives you this and it can do so because it is a statically typed
language.

Ruby is a dynamically untyped language.

If you really need this feature then you will have to use Java.

The whole nature of Ruby would have to be changed if this feature was
added to it. And all you would have is Java by another name.

“Manure by any other name would smell just as foul”

Hi!

I’ve seen so far, that a ruby method can be hacked to be
“unchangeable”.
Will ruby some day support some kind of predicate or modifier, so I can
protect my method beeing overwritten?

Gergo

How would your method get overwritten anyway? (I know it’s possible, so in
some sense it’s a rhetorical question, but by answering it we can discuss some
design philosophies.)

I have some sympathy for your concern, but I have learned to adopt the
following attitudes:

  • if other people overwrite my method, that’s their business; the
    warranty is void
  • if I overwrite my own method accidentally, I either have a messy
    design, a bad approach, or a small brain :slight_smile:

Gavin

···

From: “Kontra, Gergely” kgergely@mlabdial.hit.bme.hu

Will ruby some day support some kind of predicate or modifier, so I can
protect my method beeing overwritten?

Why would you want that?

Because I want to protect everyone (including myself) to overwrite some
methods of my object.
Or should I design it bad? Should I split this object into two
objects, one for the “from-the-self methods” and one for the
user-defined methods?

Gergo

±[Kontra, Gergely @ Budapest University of Technology and Economics]-+

    Email: kgergely@mcl.hu,  kgergely@turul.eet.bme.hu          |

URL: turul.eet.bme.hu/~kgergely Mobile: (+36 20) 356 9656 |
±------“Olyan langesz vagyok, hogy poroltoval kellene jarnom!”-------+
.
Magyar php mirror es magyar php dokumentacio: http://hu.php.net

Why do you want this, this can be usefull to modify a method. Imagine that
someone write a stupid module like this :

pigeon% cat b.rb
#!/usr/bin/ruby
require ‘mwrap’

Array.mwrap(:push, :stop) do |args|
[…]
mwrap? What is it?

this can be usefull :slight_smile:

Well accessing class variables can be useful too, but they are all
private by default… What about the program design?

Gergo

±[Kontra, Gergely @ Budapest University of Technology and Economics]-+

    Email: kgergely@mcl.hu,  kgergely@turul.eet.bme.hu          |

URL: turul.eet.bme.hu/~kgergely Mobile: (+36 20) 356 9656 |
±------“Olyan langesz vagyok, hogy poroltoval kellene jarnom!”-------+
.
Magyar php mirror es magyar php dokumentacio: http://hu.php.net

I guess for the same reasons that Java has provided such facility :slight_smile:

Bill

···

==========================================================================
Ryan King rking@panoptic.com wrote:

On 2002.10.15, Kontra, Gergely kgergely@mlabdial.hit.bme.hu wrote:

Will ruby some day support some kind of predicate or modifier, so I can
protect my method beeing overwritten?

Why would you want that?

Hello Peter,

Monday, October 14, 2002, 7:56:18 PM, you wrote:

The whole nature of Ruby would have to be changed if this feature was
added to it. And all you would have is Java by another name.

and this is universal problem to any question, where someone don’t
know the answer :wink:

right answer is that this can be done with hack, and so don’t need to
be added to language core. same as private/public “keywords” is really
just methods written in ruby itself. of course, this hack will not
guarantee against another hack that rehack this hack :wink: due to
dynamic character of language

···


Best regards,
Bulat mailto:bulatz@integ.ru

How would your method get overwritten anyway? (I know it’s possible, so in
some sense it’s a rhetorical question, but by answering it we can discuss some
design philosophies.)

Singleton methods in an object, which is derived from a kind of template
class, which has some method, which does some tasks, but relies on
defining some methods. (callbacks)
But there must be some easier situations…

I have some sympathy for your concern, but I have learned to adopt the
following attitudes:

  • if other people overwrite my method, that’s their business; the
    warranty is void
    Well, but it is nice, if you warn them with an exception…
  • if I overwrite my own method accidentally, I either have a messy
    design, a bad approach, or a small brain :slight_smile:
    Programmers are LAZY and have NO ((big)) brain. That’s why they can
    work. So your reasoning is not fair :slight_smile:

Gergo

±[Kontra, Gergely @ Budapest University of Technology and Economics]-+

    Email: kgergely@mcl.hu,  kgergely@turul.eet.bme.hu          |

URL: turul.eet.bme.hu/~kgergely Mobile: (+36 20) 356 9656 |
±------“Olyan langesz vagyok, hogy poroltoval kellene jarnom!”-------+
.
Magyar php mirror es magyar php dokumentacio: http://hu.php.net

“Gavin Sinclair” gsinclair@soyabean.com.au wrote in message news:030e01c2739d$343eb620$666332d2@nosedog

How would your method get overwritten anyway? (I know it’s possible, so in
some sense it’s a rhetorical question, but by answering it we can discuss some
design philosophies.)

I have some sympathy for your concern, but I have learned to adopt the
following attitudes:

  • if other people overwrite my method, that’s their business; the
    warranty is void
  • if I overwrite my own method accidentally, I either have a messy
    design, a bad approach, or a small brain :slight_smile:

For what it’s worth, that was the same revelation that made me stop
compulsively checking argument types in my ‘utility’ classes. I
decided that I was doing inherently single-programmer stuff, and
anyone else using the class would also be doing single-programmer
stuff; there would never be an issue of, say, two people independently
modifying code in the same runtime program. Therefore, there was a
definite ‘end user’ programmer, and it was up to them to see that all
their arguments had the proper functionality. Saved a lot of useless
(and ugly) ifs and asserts.

martin

Array.mwrap(:push, :stop) do |args|

[...]
mwrap? What is it?

method wrapper this is based on function wrapper (fwrap) from Allegro

Guy Decoux

In java this is mainly a speed issue: no virtual method lookup mechanism
is needed, if a final method is found.

Matthias

···

WDT == William Djaja Tjokroaminata billtj@z.glue.umd.edu wrote:

I guess for the same reasons that Java has provided such facility :slight_smile:


Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos & More
http://faith.yahoo.com

Because I want to protect everyone (including myself) to overwrite some
methods of my object.

Freeze your class

pigeon% ruby -e 'class A; def a() end end; A.freeze;class A; def a() end end'
-e:1: can't modify frozen class (TypeError)
pigeon%

Guy Decoux

Admit it: you’re learning ‘test/unit’ and are waiting a green bar on
assert_equal(limit, people.patience).

Cheer up, you’ve almost made it!

···

On Tue, Oct 15, 2002 at 01:19:21AM +0900, Bulat Ziganshin wrote:

The whole nature of Ruby would have to be changed if this feature was
added to it. And all you would have is Java by another name.

and this is universal problem to any question, where someone don’t
know the answer :wink:

right answer is

“K” == Kontra, Gergely kgergely@mlabdial.hit.bme.hu
writes:
Because I want to protect everyone (including myself) to
overwrite some methods of my object.
Freeze your class

pigeon% ruby -e ‘class A; def a() end end; A.freeze;class A; def
a() end end’
-e:1: can’t modify frozen class (TypeError)
pigeon%

I think that what he’s wanting to do is freeze a method, not a whole
class…

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.10.14 at 13.10.54

···

On Tue, 15 Oct 2002 02:05:04 +0900, ts wrote:

Admit it: you’re learning ‘test/unit’ and are waiting a green bar on
assert_equal(limit, people.patience).

Cheer up, you’ve almost made it!

That almost made me spit my gum out!
Thanks for the funniest thing I’ve read in a while.

James

···

Hello Massimiliano,

Monday, October 14, 2002, 11:52:36 PM, you wrote:

The whole nature of Ruby would have to be changed if this feature was
added to it. And all you would have is Java by another name.
Admit it: you’re learning ‘test/unit’ and are waiting a green bar on
assert_equal(limit, people.patience).

i think that answers “whole nature of ruby would have to be changed”
have no meaning. as you can see in this particular case, “whole nature
of ruby” already include such feature for entire class/module. so i
will laugh each time when i see such argument

···


Best regards,
Bulat mailto:bulatz@integ.ru

I think that what he's wanting to do is freeze a method, not a whole
class...

He just need to put the methods that he want protect in a module and then
freeze this module

Guy Decoux

Then that does not reflect author’s desire:

module M
  def foo; p 0; end
end
M.freeze
  
class Foo
  include M
end
Foo.new.foo #=> 0

class Foo
  def foo; p 1; end
end
Foo.new.foo #=> 1

Of course `def M.foo; …; end’ is rejected since that’s a method
definition on a frozen module, but Foo itself is still opened.

···

In message 200210141713.g9EHDrm03577@moulon.inra.fr decoux@moulon.inra.fr writes:

I think that what he’s wanting to do is freeze a method, not a whole
class…

He just need to put the methods that he want protect in a module and then
freeze this module


kjana@dm4lab.to October 15, 2002
Slow and steady wins the race.