macros

T

T. Onoma

has anyone given any thought to having macros in ruby?

perhaps there's another way to do what i want, but i'm not sure. i keep reusing the same snippit of code over and over in this one class that includes a few variables, a dumbed-down example:

"/Path/To/#{variable}/Directory"

with such a simple example one would think i only needed to set an @variable to it and resuse that, but this class is usually marshalled via yaml and hence loaded using YAML::load, so initialize will not be run. the upshot of this is that i have no single place in the class that's definitely going to be excecuted where i can set the variable. so what do you do without repeating the same snippet over and over?

oh, of course i could write a method to do it and call that each time. but this snippet really should be in a configuration file of some sort. from the example above, b/c the exact path could change.

hmmm...guess there's always eval.

thoughts?

-t0
 
D

Dan Doel

T. Onoma said:
has anyone given any thought to having macros in ruby?

As for this question, people have talked about macros in the past.

As I recall, Matz is against adding macros to Ruby because they
essentially allow people to make their
own syntax, and he'd like all Ruby to be readable by anyone who knows Ruby.

I also recall an issue with really powerful macros being a lot more
complex in Algol style languages than
they are in Lisp style languages.

In other words, you're probably out of luck as far as macros go in Ruby,
unless there's some non-standard
extention I don't know about (quite possible). You might look at
ruby-talk.org to see what's gone on in the
past with the macro discussions.

- Dan
 
J

John W. Long

T. Onoma said:
has anyone given any thought to having macros in ruby?

Ruby isn't a compiled language so you wouldn't gain a whole lot with some
sort of macro language in addition to what ruby already offers. You have
eval and instance_eval in Ruby, both of which accept strings as parameters
so you essentially have the ability to create your own macros:

def add_foo_method(o, name, result)
o.instance_eval %{
def #{name}
#{result}
end
}
end

o = Object.new
add_foo_method(o, :coolness_level, 10)
o.coolness_level #=> 10

def new_class(name, code)
eval %{
class #{name}
#{code}
end
}
end

new_class "A", %{
def hi
puts "hello world!"
end
}

a = A.new
a.hi #=> "hello world!"

You can abuse this technique, but it has it's place.
___________________
John Long
www.wiseheartdesign.com
 
J

Josef 'Jupp' SCHUGT

Hi!

* T. Onoma; 2003-11-11, 13:12 UTC:
perhaps there's another way to do what i want, but i'm not sure. i
keep reusing the same snippit of code over and over in this one
class that includes a few variables, a dumbed-down example:

"/Path/To/#{variable}/Directory"

You can (ab)use the C preprocessor in the following manner:

$ cat trick_rb.c

#define sniplet(a) ("/Path/To/" + a + "/Directory")
puts sniplet('some')

$ gcc -E trick_rb.c > trick.rb
$ cat trick.rb
# 1 "trick_rb.c"

puts ("/Path/To/" + 'some' + "/Directory")

$ ruby trick.rb

/Path/To/some/Directory

Josef 'Jupp' Schugt
 
T

Tim Hunter

You can (ab)use the C preprocessor in the following manner:

$ cat trick_rb.c

#define sniplet(a) ("/Path/To/" + a + "/Directory")
puts sniplet('some')

$ gcc -E trick_rb.c > trick.rb
$ cat trick.rb
# 1 "trick_rb.c"

puts ("/Path/To/" + 'some' + "/Directory")

$ ruby trick.rb

/Path/To/some/Directory

If you're going to go _that_ route, why not use M4?
 
J

Josef 'Jupp' Schugt

* Tim Hunter; Tue, 11 Nov 2003 23:00:39 GMT
On Wed, 12 Nov 2003 03:58:43 +0900, Josef 'Jupp' SCHUGT wrote:
If you're going to go _that_ route [and use the C preprocessor],
why not use M4?

I don't know if M4 is available for all platforms that are supported
by Ruby but I know for sure that a C compiler is available for all
platforms that Ruby runs on.

Josef 'Jupp' Schugt
 
M

Mauricio Fernández

* Tim Hunter; Tue, 11 Nov 2003 23:00:39 GMT
On Wed, 12 Nov 2003 03:58:43 +0900, Josef 'Jupp' SCHUGT wrote:
If you're going to go _that_ route [and use the C preprocessor],
why not use M4?

I don't know if M4 is available for all platforms that are supported
by Ruby but I know for sure that a C compiler is available for all
platforms that Ruby runs on.

it could have been cross-compiled ;-)

--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Microsoft is not the answer.
Microsoft is the question.
NO (or Linux) is the answer.
-- Taken from a .signature from someone from the UK, source unknown
 
J

Josef 'Jupp' SCHUGT

Hi!

* Mauricio Fernández; 2003-11-17, 21:48 UTC:
it could have been cross-compiled ;-)

[Jupp, sitting at his computer]
Jupp: Ruby has been compiled so there must be a C compiler.
[Jupp types above statement]
Jupp: Waitamoment, Ruby could have been cross-compiled...
[Jupp utters humming sounds]
Jupp: No, the statement is correct. Not because there *must* be a
compiler but because there actually *is* a compiler.

Do know of a platform that for which Ruby is available but for that
no C compiler exists (native C compilers for DOS definitely exist, I
use Pacific C - cute program by the way).

Josef 'Jupp' Schugt
 
M

Mauricio Fernández

Hi!

* Mauricio Fernández; 2003-11-17, 21:48 UTC:

Jupp: No, the statement is correct. Not because there *must* be a
compiler but because there actually *is* a compiler.

If there might be no compiler, you cannot know *for sure* ;)
Do know of a platform that for which Ruby is available but for that
no C compiler exists (native C compilers for DOS definitely exist, I
use Pacific C - cute program by the way).

Don't know any --- but I haven't been exposed to many different
platforms... such a beast might exist, somewhere.

--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Microsoft is not the answer.
Microsoft is the question.
NO (or Linux) is the answer.
-- Taken from a .signature from someone from the UK, source unknown
 
M

Michael Neumann

On Tue, Nov 18, 2003 at 09:04:47AM +0900, Josef 'Jupp' SCHUGT wrote:
If there might be no compiler, you cannot know *for sure* ;)

DJGPP is another DOS compiler (to be fair, DOS+DPMI). I've seen Ruby
compiled with it, years ago :)

Regards,

Michael
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top