J
Jon Hurst
(newbie) I can't for my life figure out how to create bang methods. Please
help.
~Jon Hurst
help.
~Jon Hurst
(newbie) I can't for my life figure out how to create bang methods. Please
help.
~Jon Hurst
[...] I would like to create bang methods of some math functions
(mostly abs), but I don't know how to modify the reciever in place
because the value of self cannot be changed.
On reflection, perhaps I have not been clear enough. I would like to create
bang methods of some math functions (mostly abs), but I don't know how to
modify the reciever in place because the value of self cannot be changed.
It's not really important that I be able to write these bang methods, but I
would very much like to understand how one would do it.
Class Numeric
def abs!
??
end
end
~Jon Hurst
Jon said:On reflection, perhaps I have not been clear enough. I would like to create
bang methods of some math functions (mostly abs), but I don't know how to
modify the reciever in place because the value of self cannot be changed.
It's not really important that I be able to write these bang methods, but I
would very much like to understand how one would do it.
Class Numeric
def abs!
??
end
end
Even if you had a way to implement some bang-abs, what would you
expect -4.abs! to be?
Jon Hurst said:On reflection, perhaps I have not been clear enough. I would like to create
bang methods of some math functions (mostly abs), but I don't know how to
modify the reciever in place because the value of self cannot be changed.
It's not really important that I be able to write these bang methods, but I
would very much like to understand how one would do it.
Class Numeric
def abs!
??
end
end
This is also why there are no ++ and -- operators in Ruby. 1++ would mean
that the actual number 1 would henceforth mean 2.
Ara.T.Howard said:i think we should try to talk matz into this for a minor release. maybe the
release slated for 2005-04-01?
Gavin Kistner said:[...] I would like to create bang methods of some math functions
(mostly abs), but I don't know how to modify the reciever in place
because the value of self cannot be changed.
This problem bit me repeatedly. It's sort of a design feature, not a
flaw, as I understand it.
Certain base classes are not meant to be mutable, per the design of the
language. The 'best' way to do this, I've been told, is what I did for
my MutableTime class, to wit:
a) Create a new class (that doesn't inherit from the other)
b) Keep your own instance variable that is the base class
c) When you need to modify the instance, swap out your instance,
otherwise
d) Pass method calls on to the instance.
For example, something like
class MyNumber
def initialize( initialValue=nil )
@n = initialValue || 0
end
def abs!
@n = @n.abs
end
def method_missing(meth, *args, &block) # :nodoc:
@n.send(meth, *args, &block)
end
Jean-Hugues ROBERT said:Well... something++ could get expanded into (something = something.++()) ?
And ++something get expanded into (tmp = something, something =
something.++(), tmp)
Pure syntax sugar.
Then you can:
def ++()
self + 1
end
I think += does that already, doesn't it ?
There are other reasons why there is no ++ in Ruby.
Well, I'll take your word for it, but apparently Matz is keeping them
secret I'm just summarizing the reasons Matz has talked about in
the past (the illogic of incrementing integers, which are unique and
immutable, and which are present as immediate values in the variables
that represent them).
Of course the parser could be taught to do what you describe -- it's
just that it wouldn't make sense. Keep in mind that these two things
are equivalent:
1++
and
x = 1
x++
so if the former makes no sense, then neither does the latter. Making
some sort of special case where, suddenly, integers are not immmediate
values strikes me as not very sugary
David
Jean-Hugues ROBERT said:Well... something++ could get expanded into (something = something.++()) ?
And ++something get expanded into (tmp = something, something =
something.++(), tmp)
Pure syntax sugar.
Then you can:
def ++()
self + 1
end
I think += does that already, doesn't it ?
There are other reasons why there is no ++ in Ruby.
Just like +=, ++ can not be defined as a method on an object.
But you are correct that there is no /technical/ reason preventing ++a
from being treated an abbreviation for a=a+1.
However, one must balance the advantages against the disadvantages. Does
the convenience make up for the complexity of yet more special syntax.
Does it fit well with the rest of the language? (for example: += works
well with strings, what should ++ do with strings? What should ++ do with
time values: increment by an hour, a minute, a second, a day?).
In this matter of balance, everyone will weigh the sides differently. So
far, Matz has decided that the convenience of saving one character doesn't
balance the extra complexity involved. So be it. Many people agree, some
people dont'. I can live with that (and I'm glad it is Matz deciding and
not me!).
Let me rephrase.Jean-Hugues ROBERT said:
+ is a method. It is sent to an object. (e.g. 1+2 === 1.+(2) )
= is not a method. It is never sent to an object. It binds objects to
names within a scope. (e.g. x=1 means that the name "x" is now bound
to the value "1")
++ cannot be a method because it changes bindings, just like =.
Let me rephrase.
If ruby was to understand:
this++
++that
as (syntax sugar for):
(tmp = this, this = something.postplusplus(), tmp)
(that = that.preplusplus())
then one could redefine methods postplusplus() and preplusplus()
(whose default definitions should be: self.plusplus(); in a decent
world).
I understand that = is not a method as of today. And need not to
be for ++ to be re definable (as rephrased).
BTW: Making = a method (of class Binding I guess) would open
interesting perspectives... ;-)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.