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.
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”
snipped
hmmm…guess there’s always eval.
thoughts?
Is (“/Path/To/%s/Directory” % [variable]) usable to you?
You can store string itself in some place, and then just do
(str % [variable])
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.
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’)
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.
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.
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.
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
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
···
On Mon, Nov 17, 2003 at 10:27:53AM +0900, Josef ‘Jupp’ Schugt wrote:
–
.-------.
message > 100 kB? / | |
sender = spammer? / | R.I.P.|
text = spam? / | |
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.
···
On Tue, Nov 18, 2003 at 09:04:47AM +0900, Josef ‘Jupp’ SCHUGT wrote:
On Mon, Nov 17, 2003 at 10:27:53AM +0900, Josef ‘Jupp’ Schugt wrote:
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
DJGPP is another DOS compiler (to be fair, DOS+DPMI). I’ve seen Ruby
compiled with it, years ago
Regards,
Michael
···
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).