Interested in metaprogramming?


#1

Hi, I have started writing a guide to metaprogramming called ‘the way
of meta’. I have written only a draft intro and a simple example up
to now [1].

I wonder if there is much interest on the topic out there…
I have lots of material on metaprogramming and DSLs that I haven’t
posted yet (still in a pretty raw format) and if I get a positive
response I’ll start working on it and I will add it to the guide.

[1] http://liquiddevelopment.blogspot.com/2006/03/way-of-meta.html


#2

chiaro scuro wrote:

Hi, I have started writing a guide to metaprogramming called ‘the way
of meta’. I have written only a draft intro and a simple example up
to now [1].

I wonder if there is much interest on the topic out there…
IMHO there definitely is.
I have lots of material on metaprogramming and DSLs that I haven’t
posted yet (still in a pretty raw format) and if I get a positive
response I’ll start working on it and I will add it to the guide.
OK, i begin: +1

Cheers,
Peter


#3

On Wednesday 19 April 2006 09:37, chiaro scuro wrote:

Hi, I have started writing a guide to metaprogramming called ‘the way
of meta’. I have written only a draft intro and a simple example up
to now [1].

I wonder if there is much interest on the topic out there…

I’m definitely interested in learning more about metaprogramming.

Mark


#4

On Wed, Apr 19, 2006 at 06:31:47PM +0900, chiaro scuro wrote:
} two people is already a crowd to me :slight_smile:
}
} I’ll let you know when I post more about metaprogramming and DSLs.

I only skimmed what you’ve written so far, but I noticed that you’ve
used
class_eval. It is preferable to avoid any form of eval whenever
possible.
In your post you can replace…

Array.class_eval do
synonym :size => [ :count, :n_elements ]
end

…with…

Array.send(:synonym, :size => [ :count, :n_elements ])

…to avoid the class_eval.

} thanks for your interest.
[…]
} Chiaroscuro
–Greg


#5

two people is already a crowd to me :slight_smile:

I’ll let you know when I post more about metaprogramming and DSLs.

thanks for your interest.


#6

It’s harder to get information on buggy eval-ed code than just plain
code.


#7

It is preferable to avoid any form of eval whenever possible.
Out of curiosity, why is this better?

–Meador


#8

I would definitely like to add my +1

Metaprogramming looks to be extremely useful and interesting, but not
always the easiest thing to wrap your brain around.

Regards,
Brett


#9

Hi –

On Wed, 19 Apr 2006, Gregory S. wrote:

synonym :size => [ :count, :n_elements ]
end

…with…

Array.send(:synonym, :size => [ :count, :n_elements ])

…to avoid the class_eval.

I don’t think instance_eval and class_eval, in their block forms, have
the same issues as plain eval(string). class_eval is really just a
closure cousin of the class keyword.

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! http://www.manning.com/black
Paper version coming in early May!


#10

On 4/19/06, Gregory S. removed_email_address@domain.invalid wrote:

On Wed, Apr 19, 2006 at 06:31:47PM +0900, chiaro scuro wrote:
} two people is already a crowd to me :slight_smile:
}
} I’ll let you know when I post more about metaprogramming and DSLs.

I only skimmed what you’ve written so far, but I noticed that you’ve used
class_eval. It is preferable to avoid any form of eval whenever possible.
In your post you can replace…

Could not agree more but I am desperately looking for a way to replace
this
which I use very often

class Mine
eval <<-EOS
def #{name}( value, &block )
do_something( &block ) if block_given?
end
EOS
end
I’d love to do it with define_method, but I do not get around the
“&block”
parameter.

Anybody help?

Thx
Robert


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

#11

what about using define_method from ruby1.8.4 ? it also accepts a
block as argument…
alternatively it will look nicer to use the eval %{ … } notation
than the HEREDOC.

if you use heredoc you coud do it this way, to convey the meaning that
the sting represents code:

eval <<-CODE
sdjifklsdnkdfksd
dlòsdklòlòfsdòs CODE


#12

the synonym is just an example, but it still serve a purpose, though.
alias clones a method, whereas synonym wraps it.
if the original method gets changed, synonym reflects this change,
whereas alias would ignore it.


#13

On 4/19/06, chiaro scuro removed_email_address@domain.invalid wrote:

[1] http://liquiddevelopment.blogspot.com/2006/03/way-of-meta.html


Chiaroscuro

Liquid Development: http://liquiddevelopment.blogspot.com/

Nice start
I fail to understand why tou define a method for each synonym though.
Would
using method_alias instead not be a better choice?

Cheers
Robert


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

#14

On 4/19/06, chiaro scuro removed_email_address@domain.invalid wrote:

what about using define_method from ruby1.8.4 ? it also accepts a
block as argument…

which is…
not the problem.
The problem is that the block -used to define the method- does not
accept a
&block parameter for the method I want to define.
BTW it seems to me there was a post on this, but I failed to find it,
sorry
if I am wasting bandwith.

Sorry if I was not clear on this.
Are we now chiari or still scuri?

Ciao


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

#15

“R” == Robert D. removed_email_address@domain.invalid writes:

R> Could not agree more but I am desperately looking for a way to
replace this
R> which I use very often

wait for 2.0

R> class Mine
R> eval <<-EOS
R> def #{name}( value, &block )
R> do_something( &block ) if block_given?
R> end
R> EOS
R> end

moulon% cat b.rb
#!/usr/bin/ruby
class Mine
define_method(:a) {|a, &b|
do_something(a, &b)
}
def do_something(a)
if block_given?
yield a
else
puts “no block #{a}”
end
end
end

Mine.new.a(12) {|x| puts “block #{x}” }
Mine.new.a(24)
moulon%

moulon% ruby -v b.rb
ruby 1.9.0 (2006-04-08) [i686-linux]
block 12
no block 24
moulon%

Guy Decoux


#16

On 4/19/06, ts removed_email_address@domain.invalid wrote:

“R” == Robert D. removed_email_address@domain.invalid writes:

R> Could not agree more but I am desperately looking for a way to replace
this
R> which I use very often

wait for 2.0

Great, I just started

moulon% cat b.rb
#!/usr/bin/ruby
class Mine
define_method(:a) {|a, &b|

                                    ^
                                     |

Here we go -----------------------+

  do_something(a, &b)

}

Guy Decoux

Thank you, at lease I will stop trying in 1.8


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

#17

“R” == Robert D. removed_email_address@domain.invalid writes:

define_method(:a) {|a, &b|

R> ^
R> |
R> Here we go -----------------------+

R> do_something(a, &b)

Well you have also this

moulon% cat b.rb
#!/usr/bin/ruby
class Mine
c = 24
define_method(:a) {|a, &b; c|
p a, b ,c
}
end

Mine.new.a(12) {|x| puts “block #{x}” }

moulon%

moulon% ruby -v b.rb
ruby 1.9.0 (2006-04-08) [i686-linux]
b.rb:4: warning: shadowing outer local variable - c
12
#Proc:removed_email_address@domain.invalid:9
nil
moulon%

Guy Decoux


#18

quite chiari, but the answer is still scura :slight_smile:

I’d like to look into that but I’ve got to run to catch an airplane in
a few hours.

I’ll look into it when I’m back if nobody has come up with an answer
by that time.


#19

On 4/19/06, ts removed_email_address@domain.invalid wrote:

R> do_something(a, &b)
end
nil
moulon%

Guy Decoux

Ok let’s all stop talking about ruby 1.8 it is too boring, just kidding.
Ty for the enlightment :wink:

Cheers
Robert


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein