def blah do |x| -- alternate method definition syntax

J

joe07734

Hi all,

This came up in a discussion about Ruby with a friend who is learning...

Why aren't method definitions more like blocks? Perhaps it's easier to
explain with some examples. Consider the method blah:

def blah(x)
...
end

And this proc:

lambda do |x|
...
end

Friend wondered why a method definition couldn't be like this:

def blah do |x|
...
end

And I didn't have a good answer, except that "it just is." Maybe I
could have gone into the semantic difference between methods and
lambda procs, but does semantic difference necessitate syntactic
difference?

I tried to simulate this like so:

blah = lambda do |x|
...
end

Which I realize is going down a different path, but still. Now I wish
I could do this:

blah(5)

But I can't. I have to blah.call.

I'm not complaining at all. This isn't bait. It's just me loving Ruby
and wondering aloud. Any opinions?

Joe
 
D

dan.macdaddy+ruby

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Which I realize is going down a different path, but still. Now I wish
I could do this:

blah(5)

But I can't. I have to blah.call.

I'm not complaining at all. This isn't bait. It's just me loving Ruby
and wondering aloud. Any opinions?

Joe
You don't have to do blah.call, you can do blah[5] which is pretty close :)
 
D

dan.macdaddy+ruby

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Apologies...

Somehow i quoted myself out:

You don't have to do blah.call, you can do blah[5] which is pretty close :)
 
T

tho_mica_l

|def blah do |x|
| ...
|end

Would it be possible, hypothetically speaking, to make the parser
digest something like:

def bla do(x, y=1, z=2)
...
end

and (maybe instead of or additionally to the experimental -> syntax)

a = arr.map def(x, y=1) ... end

fn = def(x, y=1) ... end

or

a = arr.map do(x, y=1) ... end

fn = do(x, y=1) ... end

I personally am not very fond of curly braces, vertical bars, and
arrows. Maybe it's because I learned Pascal first.
 
B

bermonruf

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

I think its almost perfect the way it is, but i would really enjoy blocks
with optional parameters.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top