[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

T. Onoma

11/11/2003 2:17:00 AM

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


10 Answers

Dan Doel

11/11/2003 2:49:00 AM

0

T. Onoma wrote:

>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


John W. Long

11/11/2003 3:25:00 AM

0

T. Onoma wrote:
>
>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



Josef 'Jupp' Schugt

11/11/2003 6:59:00 PM

0

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
--
.-------.
message > 100 kB? / | |
sender = spammer? / | R.I.P.|
text = spam? / ___| |___

Tim Hunter

11/11/2003 11:01:00 PM

0

On Wed, 12 Nov 2003 03:58:43 +0900, Josef 'Jupp' SCHUGT wrote:
>
> 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?

Josef 'Jupp' Schugt

11/12/2003 11:54:00 PM

0

* 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

Mauricio Fernández

11/17/2003 9:21:00 PM

0

On Mon, Nov 17, 2003 at 10:27:53AM +0900, Josef 'Jupp' Schugt wrote:
> * 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


Josef 'Jupp' Schugt

11/18/2003 12:05:00 AM

0

Hi!

* Mauricio Fernández; 2003-11-17, 21:48 UTC:
> On Mon, Nov 17, 2003 at 10:27:53AM +0900, Josef 'Jupp' Schugt wrote:
> > * Tim Hunter; Tue, 11 Nov 2003 23:00:39 GMT
> > 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 ;-)

[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
--
.-------.
message > 100 kB? / | |
sender = spammer? / | R.I.P.|
text = spam? / ___| |___

Mauricio Fernández

11/18/2003 6:33:00 PM

0

On Tue, Nov 18, 2003 at 09:04:47AM +0900, Josef 'Jupp' SCHUGT wrote:
> Hi!
>
> * Mauricio Fernández; 2003-11-17, 21:48 UTC:
> > On Mon, Nov 17, 2003 at 10:27:53AM +0900, Josef 'Jupp' Schugt wrote:
> > > * Tim Hunter; Tue, 11 Nov 2003 23:00:39 GMT
> > > 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 ;-)
>
> 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


Michael Neumann

11/18/2003 8:16:00 PM

0

On Wed, Nov 19, 2003 at 03:32:45AM +0900, Mauricio Fernández wrote:
> 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* ;)
>
> > 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).

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

Regards,

Michael



WATANABE Hirofumi

11/19/2003 7:28:00 AM

0

Hi,

Michael Neumann <mneumann@ntecs.de> writes:

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

Even now, the djgpp version exists:
http://ftp.ruby-lang.org/pub/ruby/binaries/...

--
eban