Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Macros in Ruby

Reply
Thread Tools

Macros in Ruby

 
 
George Moschovitis
Guest
Posts: n/a
 
      08-02-2004
Hello everyone,

one of the features of the LISP family of languages that is missing from
Ruby are macros. I think they are useful on a lot of occasions so
I would like to see Ruby support macros in the future.

However i think it is quite easy to emulate some form of macro
functionality in Ruby. Here is some simple code:

macros.rb:
----------

$__macros__ = {}
$__required__ = {}

module Kernel

alias_method ld_require, :require
def require(path)
return if $__required__[path]

source = open(path) { |f|
f.sysread(f.stat().size())
}

# parse macro
source.gsub!(/defmacro\s*\/(.*?)\/\s(.*?)endmacro/m) {
$__macros__[Regexp.new($1)] = $2 ; ""
}

# expand macros
$__macros__.each { |match, replace|
source.gsub!(match, replace)
}

$__required__[path] = true

eval(source)
end

end

require "test1.rb"
require "test2.rb"


test1.rb:
---------

defmacro /my_macro\((.*)\)/
begin
my_function(\1)
rescue => ex
puts ex
end
endmacro

# php style foreach
defmacro /foreach\s*\((.*)\s*as(.*)\)/
for \2 in \1
endmacro

def my_function(str)
puts str
end

class TestClass
def another_test
words = %w{ simple test }
foreach(words as word)
puts k
end
end
end


test2.rb:
---------

value = "Hello World!"
my_macro(value)

numbers = [1, 2, 3, 4]

foreach (numbers as i)
puts i
end

a = TestClass.new
a.another_test()



Once again i was suprised to find out that Ruby is so powerful!

However I would like to hear your opinion on this code. Is there any
problem I have overlooked? Is there a better implementation? Could this
be better designed?

If anyone finds it usefull I could create a small package with
doc+examples and upload it to RAA.


have fun,
George Moschovitis

--
www.navel.gr
www.joy.gr
 
Reply With Quote
 
 
 
 
jim@freeze.org
Guest
Posts: n/a
 
      08-02-2004
* George Moschovitis <(E-Mail Removed)> [2004-08-02 18:16:35 +0900]:

> Hello everyone,
>
> one of the features of the LISP family of languages that is missing from
> Ruby are macros. I think they are useful on a lot of occasions so
> I would like to see Ruby support macros in the future.
>
> However i think it is quite easy to emulate some form of macro
> functionality in Ruby. Here is some simple code:


I've heard Matz say that Ruby will not support macros.
They are too easily abused and can mutate the language.
Besides, you can achieve the same powerful affect using
blocks.

Jim


 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      08-02-2004
George Moschovitis wrote:
> Hello everyone,
>
> one of the features of the LISP family of languages that is missing from
> Ruby are macros. I think they are useful on a lot of occasions so
> I would like to see Ruby support macros in the future.


What you have defined are more like C macros, rather than LISP macros,
which are hygienic (they operate at the level of syntactic elements,
rather than characters).


 
Reply With Quote
 
George Moschovitis
Guest
Posts: n/a
 
      08-02-2004
> I've heard Matz say that Ruby will not support macros.
> They are too easily abused and can mutate the language.


I 've heard that, but macros ARE usefull. Take a look
at www.paulgraham.com for example.

> Besides, you can achieve the same powerful affect using
> blocks.


How can i use blocks to emulate macros ? I cannot understand
that.

George

--
www.navel.gr
www.joy.gr
 
Reply With Quote
 
George Moschovitis
Guest
Posts: n/a
 
      08-02-2004
Joel VanderWerf wrote:
> What you have defined are more like C macros, rather than LISP macros,
> which are hygienic (they operate at the level of syntactic elements,
> rather than characters).


You are of course right. But you can do still usefull things. And I
think it can be improved. Any ideas are welcome. Even better would be
support for macros in Ruby 1.9

George.


--
www.navel.gr
www.joy.gr
 
Reply With Quote
 
James Britt
Guest
Posts: n/a
 
      08-02-2004
Joel VanderWerf wrote:
> George Moschovitis wrote:
>
>> Hello everyone,
>>
>> one of the features of the LISP family of languages that is missing from
>> Ruby are macros. I think they are useful on a lot of occasions so
>> I would like to see Ruby support macros in the future.

>
>
> What you have defined are more like C macros, rather than LISP macros,
> which are hygienic (they operate at the level of syntactic elements,
> rather than characters).


Would Ruby macros, in the Lisp sense, have to manipulate the AST?

And a question (ideally) for Matz, but comments from anyone else are of
course welcome:

Would true macros in Ruby be more prone to abuse than they are in Lisp?

Is there something different about Ruby such that what (supposedly)
works to such acclaim in Lisp would be inappropriate in Ruby?


Thanks,



James



 
Reply With Quote
 
Lennon Day-Reynolds
Guest
Posts: n/a
 
      08-02-2004
On Tue, 3 Aug 2004 02:29:34 +0900, James Britt
<(E-Mail Removed)> wrote:
> Is there something different about Ruby such that what (supposedly)
> works to such acclaim in Lisp would be inappropriate in Ruby?


A: S-expressions, or the lack thereof in Ruby. Lisp code can always be
manipulated as a simple tree of cons cells; Ruby code, even if a
high-level programmatic interface existed to the AST, has a much
richer syntax, and therefore more complex structure.

Also, as has been mentioned, Ruby largely replaces macros with blocks
-- they allow delayed evaluation, specialized iteration and
pre/post-condition checks, etc. Many of the common macros you'll see
in Lisp are things like this:

(with-open-file "foo.dat" my-file (case (read-char my-file) ...))

[my apologies for the weird function names; it's been a couple of
years since I was actively developing any Lisp code]

Obviously, that example is quite handily supported by the more
Ruby-esque example below:

open("foo.dat") {|my_file| case my_file.getc ...}

Personally, I think macros could be an interesting tool for very
special cases, but they're not an essential feature for 90% of
developers. Now, exposing the AST with a high-level API could have
other benefits: code analysis and optimization, JIT compilers,
documentation generators, etc.

Lennon


 
Reply With Quote
 
Phil Tomson
Guest
Posts: n/a
 
      08-02-2004
In article <cel0la$2sto$(E-Mail Removed)>,
George Moschovitis <(E-Mail Removed)> wrote:
>Hello everyone,
>
>one of the features of the LISP family of languages that is missing from
>Ruby are macros. I think they are useful on a lot of occasions so
>I would like to see Ruby support macros in the future.
>

<code snipped>


>Once again i was suprised to find out that Ruby is so powerful!
>
>However I would like to hear your opinion on this code. Is there any
>problem I have overlooked? Is there a better implementation? Could this
>be better designed?
>
>If anyone finds it usefull I could create a small package with
>doc+examples and upload it to RAA.
>
>
>have fun,
>George Moschovitis


I've never used Lisp so I'm not too familiar with Lisp macros and I know
that Matz isn't too fond of macros. However, I can't help feeling that
you've created something quite cool here and I'd certainly like to see
more.



Please do post something on the RAA! Maybe you could put it up on
rubyforge?

Phil
 
Reply With Quote
 
Phil Tomson
Guest
Posts: n/a
 
      08-02-2004
In article <(E-Mail Removed)>, <(E-Mail Removed)> wrote:
>* George Moschovitis <(E-Mail Removed)> [2004-08-02 18:16:35 +0900]:
>
>> Hello everyone,
>>
>> one of the features of the LISP family of languages that is missing from
>> Ruby are macros. I think they are useful on a lot of occasions so
>> I would like to see Ruby support macros in the future.
>>
>> However i think it is quite easy to emulate some form of macro
>> functionality in Ruby. Here is some simple code:

>
>I've heard Matz say that Ruby will not support macros.


True, Matz has said that. But if we can do something Macro-like with a
module (as George has shown) then it's optional.

>They are too easily abused and can mutate the language.
>Besides, you can achieve the same powerful affect using
>blocks.
>


Not quite. Notice that George introduced totally new 'syntax' using his
'macros' - you can't do that with blocks.


Phil
 
Reply With Quote
 
Phil Tomson
Guest
Posts: n/a
 
      08-02-2004
In article <(E-Mail Removed)>,
Lennon Day-Reynolds <(E-Mail Removed)> wrote:
>
>Personally, I think macros could be an interesting tool for very
>special cases, but they're not an essential feature for 90% of
>developers. Now, exposing the AST with a high-level API could have
>other benefits: code analysis and optimization, JIT compilers,
>documentation generators, etc.
>


....and code translators, and lots of other cool things.

Yes, this would be great to have.

Phil
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
macros-loop? calling macros X times? Andrew Arro C Programming 2 07-24-2004 09:52 AM
Explanation of macros; Haskell macros mike420@ziplip.com Python 80 11-07-2003 02:22 AM
Re: Explanation of macros; Haskell macros Michael T. Babcock Python 0 11-03-2003 01:54 PM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 5 11-01-2003 01:09 AM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 1 10-07-2003 04:07 PM



Advertisments