Loading ...
Sorry, an error occurred while loading the content.
 

Re: Learning Ruby

Expand Messages
  • Ravi Mohan
    - ... whoa !! ruby has macros ? news to me!! some references please ? Thanks, ravi
    Message 1 of 12 , Mar 6 9:18 PM
      -
      > The other thing that I love about Ruby is its support for macros...

      whoa !! ruby has macros ? news to me!! some references please ?
      Thanks, ravi
    • Yogi
      ... Ruby s eval and class_eval methods provides support for macros e.g. the attr_reader, attr_writer and attr_accessor are macros that generate getter/setter
      Message 2 of 12 , Mar 6 11:49 PM
        > whoa !! ruby has macros ? news to me!! some references please ?

        Ruby's eval and class_eval methods provides support for macros e.g.
        the attr_reader, attr_writer and attr_accessor are macros that
        generate getter/setter methods and instance variables for each symbol
        passed to them.

        So the foll code:
        class Foo
        attr_reader :bar
        end

        will allow you to do:
        f = Foo.new
        f.bar = "x"
        puts f.bar

        Heres an example from the activesupport lib (thats used by rails)
        which creates class-level attributes similar to the attr* methods:

        # Extends the class object with class and instance accessors for class
        attributes,
        # just like the native attr* accessors for instance attributes.
        class Class # :nodoc:
        def cattr_reader(*syms)
        syms.each do |sym|
        class_eval <<-EOS
        if ! defined? @@#{sym.id2name}
        @@#{sym.id2name} = nil
        end

        def self.#{sym.id2name}
        @@#{sym}
        end

        def #{sym.id2name}
        @@#{sym}
        end

        def call_#{sym.id2name}
        case @@#{sym.id2name}
        when Symbol then send(@@#{sym})
        when Proc then @@#{sym}.call(self)
        when String then @@#{sym}
        else nil
        end
        end
        EOS
        end
        end

        def cattr_writer(*syms)
        syms.each do |sym|
        class_eval <<-EOS
        if ! defined? @@#{sym.id2name}
        @@#{sym.id2name} = nil
        end

        def self.#{sym.id2name}=(obj)
        @@#{sym.id2name} = obj
        end

        def self.set_#{sym.id2name}(obj)
        @@#{sym.id2name} = obj
        end

        def #{sym.id2name}=(obj)
        @@#{sym} = obj
        end
        EOS
        end
        end
      • Avik Sengupta
        Well, since even excel has macros (and so does textpad), the term is probably overloaded... So apparently ruby does NOT have macro s in the language in the
        Message 3 of 12 , Mar 6 11:53 PM
          Well, since even excel has macros (and so does textpad), the term is
          probably overloaded...

          So apparently ruby does NOT have macro's in the language in the Lisp
          sense of the term, but arguably you can do almost everything that you'd
          need macros for using blocks etc... See discussion starting here
          http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/108059 for
          arguments both for and against that hypothesis (and according to Paul
          Graham, if you add macro's to the language, you cant say you'd invented
          a new language, you'd just have a dialect of lisp :)

          In particular, the domain language used by Active Record is really the
          kind of thing that you'd use macro's for. I have no idea how its
          implemented ... presumably a combination of blocks/ method_missing/
          mixins etc (above thread also shows how to emulate some form of macro
          functionality in pure ruby). So while a lisp'er will cringe at calling
          it a macro... "Anything that quacks like a macro - is a macro" ??

          :)

          -
          Avik

          disclaimer - All the above may be b****hit... i'm still trying to figure
          out what a continuation is... so a lot of this is over my head :)

          On Mon, 2005-03-07 at 05:18 +0000, Ravi Mohan wrote:
          >
          > -
          > > The other thing that I love about Ruby is its support for macros...
          >
          > whoa !! ruby has macros ? news to me!! some references please ?
          > Thanks, ravi
          >
          >
          >
          >
          >
          >
          >
          > Yahoo! Groups Links
          >
          >
          >
          >
          >
          >
          >
          --
        • Yogi
          You re right, my mistake... when I first saw eval and the way Ruby uses symbols to do neat stuff like create methods, it seemed very much like the lisp
          Message 4 of 12 , Mar 7 2:44 AM
            You're right, my mistake... when I first saw eval and the way Ruby
            uses symbols to do neat stuff like create methods, it seemed very much
            like the lisp approach of building the language up to the domain, and
            I (wrongly) jumped to the conclusion that ruby has full-blown macros.

            But it doesn't. At least not like lisp, where the parameters to a
            macro are immediately quoted and passed in as a data structure which
            the macro can use to form another SExp. Essentially lisp delays the
            evaluation of a macro's parameters until they are expanded by the
            macro.

            While you can use ruby eval to build the language up to the domain
            (the active record example you mentioned), you cannot easily extend
            the core syntax of ruby as you can in lisp (well at least not that
            easily or cleanly... in the link you mentioned the defmacro method
            takes a regular expression as a parameter to do replacements in the
            body of the method, which is then passed to eval... which is actually
            really clever, but a bit hacky).

            Thanks for pointing that out! Ravi, did I get that right?


            On Mon, 07 Mar 2005 13:23:53 +0530, Avik Sengupta
            <avik.sengupta@...> wrote:
            >
            > Well, since even excel has macros (and so does textpad), the term is
            > probably overloaded...
            >
            > So apparently ruby does NOT have macro's in the language in the Lisp
            > sense of the term, but arguably you can do almost everything that you'd
            > need macros for using blocks etc... See discussion starting here
            > http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/108059 for
            > arguments both for and against that hypothesis (and according to Paul
            > Graham, if you add macro's to the language, you cant say you'd invented
            > a new language, you'd just have a dialect of lisp :)
            >
            > In particular, the domain language used by Active Record is really the
            > kind of thing that you'd use macro's for. I have no idea how its
            > implemented ... presumably a combination of blocks/ method_missing/
            > mixins etc (above thread also shows how to emulate some form of macro
            > functionality in pure ruby). So while a lisp'er will cringe at calling
            > it a macro... "Anything that quacks like a macro - is a macro" ??
            >
            > :)
            >
            > -
            > Avik
            >
            > disclaimer - All the above may be b****hit... i'm still trying to figure
            > out what a continuation is... so a lot of this is over my head :)

            --
            - Yogi
            Developer, www.thoughtworks.com
          • Ravi Mohan
            hello, ... yes. eval + block != macros (in the lisp sense) . see
            Message 5 of 12 , Mar 7 3:11 AM
              hello,



              > So apparently ruby does NOT have macro's in the language in the Lisp
              > sense of the term,

              yes.
              eval + block != macros (in the lisp sense) .

              see
              http://groups-beta.google.com/group/comp.lang.ruby/browse_thread/thread/5c74e91cd8af3b18/5120b01cc72dbeaa?q=ruby+lisp+macros#5120b01cc72dbeaa

              for those too lazy to browse the thread ,
              Matz , the author of Ruby says.. (message 25 in the above thread)

              ( IGNORING (lisp's) reader macros ...),
              quote begins

              * about 50% of macro usage in Lisp (extending syntax a bit) can
              accomplish using blocks in Ruby. we don't need inlining functions
              when we have other rooms for optimization, for example, C extensions
              quote ends .

              just above that piece is another that on close reading will explain
              why ruby eval + block combos is different from lisp style syntax
              extending macros .


              * Lisp does not have syntax. They only have meta syntax
              (i.e. S-expression), Lisp macro do not change its (meta) syntax to
              rely on (I'm ignoring reader macro here). In comparison, Ruby
              does have syntax but no meta syntax, so that you can easily loose
              syntax to rely on by macros in Ruby. You will feel like a stranger
              when you see Ruby programs with a lot of macros. I don't think
              you feel same way when you see Lisp programs with macros.


              a couple of interesting points .

              1.the reason why matz qualifies the claim to avoid reader macros is
              that in lisp , reader macros allow you to change even the meta syntax
              by manipulating the lexer parser combination .This allows you to
              REDEFINE the language syntax/semantics to anything you want it to be .
              need less to say this power can be abused very badly.

              At least till the time ruby asts, lexer and parser are manipulatable
              *from ruby* (hard to do since teh ruby interpreter is writen in C as
              of today . maybe teh yarv efort will lead in this direction later )
              reader macro like functionality is impossible in concept , leave alne
              in implementation . (not that that is a bad thing) .


              2. eval + blocks != (lisp style) macros . a *part* of what lisp
              macros can accomplish can be duplicated with
              the eval , class eval + blocks combination is present in smalltalk
              too . But this does not mean that smalltalk has (lisp style) macros .


              I was thrown because in *my mind* "language X has macros" implies
              it has lisp style macros (or something VERY close ) . this is entirely
              MY interpretation and was probably not Yogi's intended meaning .

              Anyway , as the quote above shows , ruby is still a way off from
              having lisp style macros .Aparently matz has ruled out lisp style
              macros for ruby and that's probably a good thing (from a pov within
              the Ruby universe ) .

              as avik said
              > So while a lisp'er will cringe at calling
              > it a macro... "Anything that quacks like a macro - is a macro" ??
              i would just add a word "like" to that sentence to say

              "Anything that quacks like a macro - is LIKE a macro" .
              :-)

              Anyhow , since my doubts have been clarified , and since the intention
              is not to start one of those never ending arguments on "which language
              is better " , i will take matz word (matz was worked with lisp for a
              long time ) that about 50 % of lisp macro usage is possible in ruby
              (which is REALLY good imho) and stop this thread here .


              Regds,
              Ravi


              PS : repeat:- the interpretation of the word "macros" in yogi's mail
              to mean lisp style macros is mine, not his .

              PPS : yes avik , the word "macros" is very overloaded!! good point .

              PPPS : where/when is the ruby-india group meeting ? i can see we'll
              have some intereting sessions




              >
            • Ravi Mohan
              I posted a reply to this but yahoo seems to have swallowed it . here it is again. apologies to anyone who gets this twice original post yogi said ... I wrote
              Message 6 of 12 , Mar 7 3:38 AM
                I posted a reply to this but yahoo seems to have swallowed it . here
                it is again. apologies to anyone who gets this twice


                original post

                yogi said
                >
                > Thanks for pointing that out! Ravi, did I get that right?
                >

                I wrote a rather long winded reply to Avik's earlier mail before i
                saw this post .you nailed it spot on .

                Regds, Ravi
              Your message has been successfully submitted and would be delivered to recipients shortly.