[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby of the future: Goodbye to methods... PLEASE

netytan

5/29/2006 7:30:00 PM

Hey all,

Ruby is supposed to be purely OO, if that's the case then how come
methods aren't objects; If they are then why are they second class?

I'm confused, Ruby has both methods and function objects (Lambda or
Proc, though the two aren't strictly the same). These function objects
are first class citizens of the language, like all objects in Ruby...
methods as stated aren't.

Why does Ruby have a second class entity as such a major part of the
language? There must be some reason for it but from my point of view it
makes the language far more complicated than it has to be, and removes
a lot of flexibility from the mix. Sure, you can pass the symbol names
around but that hardly cuts it.

What are the chances that Ruby will rectify this mistake sometime in
the future? I don't know I guess it depends on the developers. I know
at last three Python programmers who refuse to move to Ruby just
because of the ugliness required to get a first class function.

As a Lisper myself I do feel this a lot when I program in Ruby, I just
can't get do even the most basic things with functions, in what I'd
consider an elegant manner. OO isn't always the best solution :).

What do you think? Will it happen?

Thanks guys,

Mark.

44 Answers

List Recv

5/29/2006 8:36:00 PM

0

+1, although I think it will be better received if it were toned down.

gabriele renzi

5/29/2006 8:51:00 PM

0

netytan ha scritto:
> Hey all,
>
> Ruby is supposed to be purely OO, if that's the case then how come
> methods aren't objects; If they are then why are they second class?

they are objects, what do you mean by "not first class" ?

> I'm confused, Ruby has both methods and function objects (Lambda or
> Proc, though the two aren't strictly the same). These function objects
> are first class citizens of the language, like all objects in Ruby...
> methods as stated aren't.
>
> Why does Ruby have a second class entity as such a major part of the
> language? There must be some reason for it but from my point of view it
> makes the language far more complicated than it has to be, and removes
> a lot of flexibility from the mix. Sure, you can pass the symbol names
> around but that hardly cuts it.

I suppose you mean that you can't write something like

array.each(print)

The answer is that this is not compatible with the uniform access
principle, where you can call a method withouth parenthesis.


> What are the chances that Ruby will rectify this mistake sometime in
> the future? I don't know I guess it depends on the developers. I know
> at last three Python programmers who refuse to move to Ruby just
> because of the ugliness required to get a first class function.

I guess the right answer is "then you just dislike ruby", python is a
nice language so they can be happy anyway :)

The choice that guido went with python is nicer in some cases, but worst
in others. For example I can't use "print" in python lambdas because it
is a statement, and it is not a function cause python's handling of
methods would have required it to be always used with parenthesis.

foo.bar is a dictionary access in python and *can* be made into a
specific method call redefining foo.bar.__get__ and having understood
descriptors (or with property(), which is the same), which seems over
complicated to me. And then you still would not be able to do foo(bar)
with bar being a method.

Sadly, it is a trade off, matz played with another one in the past but
it is unlikely that you will ever be able to get a single namespace for
methods and variables in ruby.

> As a Lisper myself I do feel this a lot when I program in Ruby, I just
> can't get do even the most basic things with functions, in what I'd
> consider an elegant manner. OO isn't always the best solution :).

I guess you're a schemer, common lisp do have a different namespace for
functions, doesn't it?

> What do you think? Will it happen?

I prefer the kogut[1] convention, but it is unlikely that ruby will change



[1] http://kokogut.sourceforge.net/...
"obj.foo" is always a method call
"foo a " is always a method call
"foo() " is always a method call
"foo " is always the foo object

netytan

5/30/2006 12:15:00 AM

0

Hey,

I'm sorry if it came off as being rude, It wasn't my intention. I'll
try and tone it down in the future. I have nothing against Ruby, it
handles a lot of things in a cleaner way than Python does and the use
of blocks is very nice, as in Smalltalk. The functional approach is
obviously a valued one, and very useful, I like it :).

My problem is that there are things that you can't do with methods,
which are very useful and for which lambda is just messy.


Methods aren't first class because you can't pass them or return them,
only there names.


For instance if you want to write a function or two that you'll pass to
a function or method then firstly you have to use a totally different
syntax from that of method definition, then you have to send the call
method to them. Alternatively you can define methods and pass them by
name; again there is another syntax for doing this.

If methods were just Proc object in classes then you'd get functions
for free, lambda is amazing but the distinction between methods and
functions make the whole thing seems a little rough around the edges.
Why should the programmer have to think about whether it's a method or
a function?

The distinction removes a lot of possibilities - which incidentally
aren't taken advantage of in Python either :).


Ruby is already happily taking advantage of functional ways of doing
things. I don't think there's anything to loose in doing this. If
anything it would make Ruby more OO, if that's what you want, but it
would also make it more functional.


I wasn't really talking about namespaces or the call syntax. Though yes
Common Lisp is a Lisp 2, meaning it has a separate namespace for
functions. Scheme is a Lisp 1 and has more in common with the original
Lisp. Both are good :).

I'll take a look at the link,

Take care

Mark.

Dave Burt

5/30/2006 2:35:00 AM

0

netytan wrote:
> I'm sorry if it came off as being rude, It wasn't my intention.

Your post was rude because you strongly condemn a key aspect of the
language without considering the other side, and this as a newcomer to
the Ruby community. If you want to criticize Ruby, use it first, and
discuss it in the community. Discussion involves asking questions
assuming there _might_ be an answer you're not aware of.

> Methods aren't first class because you can't pass them or return them,
> only there names.
> ...
> If methods were just Proc object in classes then you'd get functions
> for free...

You obviously don't know the method method.

irb(main):001:0> s = "foo"
=> "foo"
irb(main):002:0> append = s.method("<<")
=> #<Method: String#<<>
irb(main):003:0> append.call "bar"
=> "foobar"
irb(main):004:0> def yld(x) yield x end
=> nil
irb(main):005:0> yld("baz", &append)
=> "foobarbaz"

Methods are not _automatically_ first-class objects in Ruby for
performance reasons, but they can easily be realized as such using the
method method, and these Method objects can be used pretty much
interchangeably with Proc objects.

Cheers,
Dave

Trans

5/30/2006 4:31:00 AM

0

But using #method isn't quite the same. They don't carry state b/c

method(:foo) != method(:foo)

There's an extension in Facets that provides a fix for this, but I
should point out there are some complications with Ruby b/c it
distinguishes between bound and unbound methods.

T.

netytan

5/30/2006 4:42:00 AM

0

Hi Dave,

Point taken, I have to admit that it's a bad habit of mine to jump in
and start fights, even unintentionally.

I'm going to speak my mind, even if this is considered to be
condemning key aspects of the language. Being a major part of the
language doesn't mean that it couldn't be improved, or that the
language wouldn't be better of if it were removed - classes from
C++ anyone ;).


I've been using Ruby on and off for quite a while and I've used
similar languages for a long time before that. I am aware of method()
but that hardly cuts it IMO. Wrapping second-class entities in first
class ones doesn't make them first class, if anything it's a very cute
hack to solve a problem that wouldn't exist otherwise.

You say that Method objects can be used pretty much interchangeably
with Proc, and the same can be said about Lambda. So what, you have is
three slightly different entities that do the same thing at the
implementation level :(.

Replacing methods (second class citizens) with objects (first class
citizens) would remove this complexity and make Ruby more flexible.
Someone please tell me how that could be a bad thing and why you
wouldn't want it?

It's just something that I'm less than happy with and it should be
taken as personal opinion. It's about the only thing in Ruby that I'm
REALLY not happy about; it's obviously a win for everything in a
language to first class. Overall, Ruby is a very nice language.

Errr...

I have to say that if this is done for performance reasons then it's a
bad case of premature optimization. There are languages that do have
first class methods and are much faster than Ruby, it has to be a bad
design choice to disable the language in order to solve implementation
problems which I'm sure will be resolved in due course :).

It's a poor excuse no, I doubt that's Matz reason.


Personally I'd prefer to use a slightly slower language that lets me do
some very powerful and interesting things easily. Ruby as it exists now
doesn't strictly hinder, but it does make some things very unclear.

I'm aware that Ruby emphasizes OO programming, but I think that it
also does a good job of making it obvious just how much better code can
be when functional programming techniques are properly applied to
problems.

Making this change in Ruby would make for a better synergy between the
two approaches.

Either way I take it that Ruby will never fix this? Or it isn't on
the bill for Ruby 2.0?

Thanks for any additional Info,

Take care,

Mark.

Dave Burt

5/30/2006 7:18:00 AM

0

Mark wrote:
> ... I am aware of method()
> but that hardly cuts it IMO. Wrapping second-class entities in first
> class ones doesn't make them first class, if anything it's a very cute
> hack to solve a problem that wouldn't exist otherwise.

It's not a hack, it's simply a different paradigm. Ruby is not primarily
a functional language, it's primarily object-oriented.

If all you want is to be able to refer to a method as "obj.method_name"
rather than "obj.method(:method_name)", Ruby will never let you, because
(in an OO language) we prefer calling methods to passing them around as
functions, and so the simple syntax is reserved for that.

Of course, there's no reason to my mind why the syntax "obj::foo" might
not be syntactic sugar for "obj.method(:foo)" in the same way as "for"
is for "each". There's still absolutely no reason the Method should be
reified before such a call.

Your assertion that Object#method is "wrapping second-class entities in
first class ones" is ridiculous. It is that in exactly the same sense as
procs, integers, classes and all other Ruby objects are. A first-class
entity is simply one that's accessible as an Object instance, and a
Method instance is that.

> You say that Method objects can be used pretty much interchangeably
> with Proc, and the same can be said about Lambda. So what, you have is
> three slightly different entities that do the same thing at the
> implementation level :(.

Two: Method and Proc.
(lambda {}).class #=> Proc

Why is this a bad thing? Would it be better if they were incompatible?
In an object-oriented language, we need methods (bound to instances);
and unbound functions (procs, blocks) are useful as well -- it's just a
different way to create a (first-class) function.

If you do really want just one, you can easily make a Proc that calls a
method:
f = proc {|*args| obj.foo(*args) }

> Replacing methods (second class citizens) with objects (first class
> citizens) would remove this complexity and make Ruby more flexible.
> Someone please tell me how that could be a bad thing and why you
> wouldn't want it?

First, please tell me why it's a good thing and why you do want it.

As I said, obj.method(:foo) gives you exactly the first-class object you
want.

> It's just something that I'm less than happy with and it should be
> taken as personal opinion. It's about the only thing in Ruby that I'm
> REALLY not happy about; it's obviously a win for everything in a
> language to first class. Overall, Ruby is a very nice language.

That's fine, of course, but I think you're missing something if it irks
you that much.

> I have to say that if this is done for performance reasons then it's a
> bad case of premature optimization. There are languages that do have
> first class methods and are much faster than Ruby, it has to be a bad
> design choice to disable the language in order to solve implementation
> problems which I'm sure will be resolved in due course :).
>
> It's a poor excuse no, I doubt that's Matz reason.

What languages have "methods are objects" as you're proposing and are
faster than Ruby?

Anyway, although the performance cost of reifying methods is definitely
a factor, another one (I'm still not sure exactly what you want here)
might be syntax. As I said above, maybe what you want here is for the
method call syntax to be co-opted into producing a reference to your
method object, and that's not going to happen. "obj.foo" calls the foo
method.

> Personally I'd prefer to use a slightly slower language that lets me do
> some very powerful and interesting things easily. Ruby as it exists now
> doesn't strictly hinder, but it does make some things very unclear.
> ...
> Making this change in Ruby would make for a better synergy between the
> two approaches.

Exactly what things does it make "very unclear"?

Exactly what change are you after? Re-reading your first email, it seems
you just want a Method object. We have that already.

Why not post some Ruby code you don't like, and the syntax you would
like to use to accomplish the same task, and the accompanying semantics?

> Either way I take it that Ruby will never fix this? Or it isn't on
> the bill for Ruby 2.0?

I still don't see what needs fixing, or how you're proposing it be fixed.

Cheers,
Dave

gabriele renzi

5/30/2006 2:54:00 PM

0

Trans ha scritto:
> But using #method isn't quite the same. They don't carry state b/c
>
> method(:foo) != method(:foo)
>
> There's an extension in Facets that provides a fix for this, but I
> should point out there are some complications with Ruby b/c it
> distinguishes between bound and unbound methods.

I don't think this is related to the Unbound/bound distinction,thatb one
is meaningful, and python has the same one, but with equality check done
right.
Anyway, I'd like it if UnboundMethods were callable and had #to_proc :)

gabriele renzi

5/30/2006 3:02:00 PM

0

netytan ha scritto:


> I've been using Ruby on and off for quite a while and I've used
> similar languages for a long time before that. I am aware of method()
> but that hardly cuts it IMO. Wrapping second-class entities in first
> class ones doesn't make them first class, if anything it's a very cute
> hack to solve a problem that wouldn't exist otherwise.

I still don't understand what you mean by first and second class. Method
object are first class, they can be passed around as much as you want
just like in python or lisp. You just access them differently.
Numbers in python are object, even if you can't write 1.__class__ , no?

You say that the problem is that there are things that can't be done
with methods, would you elaborate?

> You say that Method objects can be used pretty much interchangeably
> with Proc, and the same can be said about Lambda. So what, you have is
> three slightly different entities that do the same thing at the
> implementation level :(.

Proc's and Lambda's are the same thing, and methods have key differences
such as being bound to a method and not retaining the lexical closure,
so I think they should stay split.

> Replacing methods (second class citizens) with objects (first class
> citizens) would remove this complexity and make Ruby more flexible.
> Someone please tell me how that could be a bad thing and why you
> wouldn't want it?

you did read my previous post, right? ;)

S P Arif Sahari Wibowo

5/30/2006 3:48:00 PM

0