Ruby Grammar - "do"

Hey everyone

I work for a software development company that is currently writing an
in-house grammar for parsing Ruby. I’ve been frustrated by it many,
many, many times. Maybe it’s easy to program in, but it’s a nightmare to
parse.

My particular question (right now :P) is in reference to the do keyword.
I was sure that if there was a “do” eg.

expression.each do | [
name
]+
|
body
end

there would be an “end”. But now I’ve run into this particular example.

def _tags
rv = [
“title”,
“link”,
“description”,
“language”,
“copyright”,
“managingEditor”,
“webMaster”,
“rating”,
“docs”,
“skipDays”,
“skipHours”,
“image”,
“textInput”,
“cloud”,
].delete_if do |x|
send(x).nil?
end.collect do |elem|
[nil, elem]
end

@item.each do
rv << [nil, “item”]
end
rv
end

Can someone please enlighten me as to why this particular do is special?

Bihal

Bihal wrote:

Hey everyone

I work for a software development company that is currently writing an
in-house grammar for parsing Ruby. I’ve been frustrated by it many,
many, many times. Maybe it’s easy to program in, but it’s a nightmare to
parse.

Agreed.

My particular question (right now :P) is in reference to the do keyword.
I was sure that if there was a “do” eg.

[snip]

Can someone please enlighten me as to why this particular do is special?

I’m confused – I see three do’s and three end’s, don’t I?
Why is any of them special?

Hal

On Apr 9, 2006, at 11:06 PM, Bihal wrote:

Hey everyone

I work for a software development company that is currently writing an
in-house grammar for parsing Ruby. I’ve been frustrated by it many,
many, many times. Maybe it’s easy to program in, but it’s a
nightmare to
parse.

This list may help you:

http://rubyforge.org/mailman/listinfo/rubygrammar-grammarians

there would be an “end”. But now I’ve run into this particular
example.

Every do has an end in your example.

def _tags
rv = [
[…]
].delete_if do |x|
^^
send(x).nil?
end.collect do |elem|
^^^ ^^
[nil, elem]
end
^^^

@item.each do
^^
rv << [nil, “item”]
end
^^^
rv
end

This is def’s end.

PS: Whoever wrote that example code should be taken out for parking-
lot therapy.


Eric H. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

Okay. My next question is then

end.collect

I’ve just spent all day re-doing my grammar to ignore “end.” as an end
keyword. I’ll see if I can find the example where it didn’t seem to
work.

If you guys would be so kind as to tell me what it is and what it is
doing, that would be rgeat.

Bihal

Seems I must have misread that first instance of “end.”

I’d still like to know why it’s there!

Bihal wrote:

Seems I must have misread that first instance of “end.”

I’d still like to know why it’s there!

OK… look at it this way.

A block (whether {} or do/end) is attached to a method call.

A method call can (does) return a value.

That value is an object which can then have other methods
called on it.

This ugly bit of code

mymeth(a,b) do |x|
this
that
end.other_method(c,d,e)

might be better understood as

mymeth(a,b) {|x| this; that }.other_method(c,d,e)

or even

temp = mymeth(a,b) {|x| this; that }
temp.other_method(c,d,e)

Does that help?

Hal

Bihal wrote:

Okay. My next question is then

end.collect

I’ve just spent all day re-doing my grammar to ignore “end.” as an end
keyword. I’ll see if I can find the example where it didn’t seem to
work.

If you guys would be so kind as to tell me what it is and what it is
doing, that would be rgeat.

Bihal

Isn’t it just resolving the previous do?

It’s calling the operation:

[
“title”,
…etc…
“cloud”,
].delete_if do |x|
send(x).nil?
end

And then calling the “collect” method for the result of that operation.

Jeff

And Eric, sorry if I mislead you, by example code I meant any code I
could get my hands on to test the grammar on, not one someone wrote as
an example.

Thanks Hal, that explains it.

Jeff C.man wrote:

Isn’t it just resolving the previous do?

It’s calling the operation:

[
“title”,
…etc…
“cloud”,
].delete_if do |x|
send(x).nil?
end

And then calling the “collect” method for the result of that operation.

Jeff

It is. I mis-read the code and wasted an afternoon. The Ruby editor I
was using didn’t highlight the end. as being the end keyword, and I
didn’t look too closely. Not that I consider it easy to read anyway.

Bihal

Not that it is impossible, but I would guess that trying to
learn to parse a language before you understand the language
is perhaps only going to lead to premature grey hair, or perhaps
no hair at all. Not that anyone on a mailing list bothers
to look in a mirror more than once a month anyway…

Maybe thats just me, ok ok, but anyway…then again, perhaps
writing a parser is a fantastic way to learn a new language,
what the heck to I know…

On Mon, 2006-04-10 at 18:58 +0900, Alex C. wrote:

Not that it is impossible, but I would guess that trying to
learn to parse a language before you understand the language
is perhaps only going to lead to premature grey hair, or perhaps
no hair at all. Not that anyone on a mailing list bothers
to look in a mirror more than once a month anyway…

Maybe thats just me, ok ok, but anyway…then again, perhaps
writing a parser is a fantastic way to learn a new language,
what the heck to I know…

I like to think I’m an experienced developer, and I like to presume I
know a bit about Ruby by now. I’m interested in parsing and language
processing in general.

Probably for these reasons, I still wouldn’t relish the prospect of
writing a Ruby parser. Those Grammarians have bigger, umm, skill-sets,
than me :slight_smile:

On 4/10/06, Ross B. [email protected] wrote:

I like to think I’m an experienced developer, and I like to presume I
know a bit about Ruby by now. I’m interested in parsing and language
processing in general.

Probably for these reasons, I still wouldn’t relish the prospect of
writing a Ruby parser. Those Grammarians have bigger, umm, skill-sets,
than me :slight_smile:

Perhaps but it is not the size of your skillset, but how you apply it,
that matters

Have you considered joining the rubygrammar-grammarians mailing list?

It’s usually pretty quiet, but if you have grammar questions, there are
quite a few other people working on Ruby grammars lurking.

http://rubyforge.org/mailman/listinfo/rubygrammar-grammarians

-mental

MenTaLguY wrote:

Have you considered joining the rubygrammar-grammarians mailing list?

It’s usually pretty quiet, but if you have grammar questions, there are
quite a few other people working on Ruby grammars lurking.

http://rubyforge.org/mailman/listinfo/rubygrammar-grammarians

-mental

Yes, I’ve subscribed, thanks.

As for whether you need to know the language or not, I know as well as
anyone else here, which is not at all. :stuck_out_tongue: But writing a grammar does
give you a different perspective on a language.

Bihal