Forum: Ruby interested in metaprogramming?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chiaro S. (Guest)
on 2006-04-19 12:40
(Received via mailing list)
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-...
Peter S. (Guest)
on 2006-04-19 12:47
(Received via mailing list)
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
Mark S. (Guest)
on 2006-04-19 13:21
(Received via mailing list)
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
Chiaro S. (Guest)
on 2006-04-19 13:33
(Received via mailing list)
two people is already a crowd to me :-)

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

thanks for your interest.
Gregory S. (Guest)
on 2006-04-19 16:08
(Received via mailing list)
On Wed, Apr 19, 2006 at 06:31:47PM +0900, chiaro scuro wrote:
} two people is already a crowd to me :-)
}
} 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
Meador I. (Guest)
on 2006-04-19 17:10
(Received via mailing list)
>It is preferable to avoid any form of eval whenever possible.
Out of curiosity, why is this better?

--Meador
Chiaro S. (Guest)
on 2006-04-19 17:13
(Received via mailing list)
It's harder to get information on buggy eval-ed code than just plain
code.
unknown (Guest)
on 2006-04-19 17:23
(Received via mailing list)
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!
Brett R. (Guest)
on 2006-04-19 17:48
(Received via mailing list)
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
Robert D. (Guest)
on 2006-04-19 17:54
(Received via mailing list)
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 :-)
> }
> } 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
Robert D. (Guest)
on 2006-04-19 18:00
(Received via mailing list)
On 4/19/06, chiaro scuro <removed_email_address@domain.invalid> wrote:
> [1] http://liquiddevelopment.blogspot.com/2006/03/way-...
>
> --
> 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
Chiaro S. (Guest)
on 2006-04-19 18:04
(Received via mailing list)
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
Chiaro S. (Guest)
on 2006-04-19 18:04
(Received via mailing list)
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.
ts (Guest)
on 2006-04-19 18:07
(Received via mailing list)
>>>>> "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
Robert D. (Guest)
on 2006-04-19 18:10
(Received via mailing list)
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
Chiaro S. (Guest)
on 2006-04-19 18:13
(Received via mailing list)
quite chiari, but the answer is still scura :-)

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.
Robert D. (Guest)
on 2006-04-19 18:19
(Received via mailing list)
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
ts (Guest)
on 2006-04-19 18:25
(Received via mailing list)
>>>>> "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
Robert D. (Guest)
on 2006-04-19 18:37
(Received via mailing list)
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 ;)

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
This topic is locked and can not be replied to.