Forum: Ruby Ruby Grammar - "do"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
65b23f7757747463b55afc326956d27d?d=identicon&s=25 Bihal (Guest)
on 2006-04-10 08:06
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
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 Hal Fulton (Guest)
on 2006-04-10 08:16
(Received via mailing list)
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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-04-10 08:19
(Received via mailing list)
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-...

>
> 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 Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
65b23f7757747463b55afc326956d27d?d=identicon&s=25 Bihal (Guest)
on 2006-04-10 08:26
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
65b23f7757747463b55afc326956d27d?d=identicon&s=25 Bihal (Guest)
on 2006-04-10 08:34
Seems I must have misread that first instance of "end."

I'd still like to know why it's there!
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 Hal Fulton (Guest)
on 2006-04-10 09:08
(Received via mailing list)
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
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-10 09:11
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
65b23f7757747463b55afc326956d27d?d=identicon&s=25 Bihal (Guest)
on 2006-04-10 09:17
Thanks Hal, that explains it.

Jeff Coleman 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
65b23f7757747463b55afc326956d27d?d=identicon&s=25 Bihal (Guest)
on 2006-04-10 09:21
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.
622fa8560c82dfaa59c91ec75efb0c19?d=identicon&s=25 Alex Combas (Guest)
on 2006-04-10 12:01
(Received via mailing list)
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..
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-04-10 13:12
(Received via mailing list)
On Mon, 2006-04-10 at 18:58 +0900, Alex Combas 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 :)
622fa8560c82dfaa59c91ec75efb0c19?d=identicon&s=25 Alex Combas (Guest)
on 2006-04-10 15:04
(Received via mailing list)
On 4/10/06, Ross Bamford <rossrt@roscopeco.co.uk> 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 :)

Perhaps but it is not the size of your skillset, but how you apply it,
that matters
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 MenTaLguY (Guest)
on 2006-04-10 18:24
(Received via mailing list)
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-...

-mental
65b23f7757747463b55afc326956d27d?d=identicon&s=25 Bihal (Guest)
on 2006-04-11 01:17
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-...
>
> -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. :-P But writing a grammar does
give you a different perspective on a language.

Bihal
This topic is locked and can not be replied to.