# DRYing code through meta programming

**URL:** <https://www.ruby-forum.com/t/drying-code-through-meta-programming/83741>\
**Category:** Rails\
**Created:** [January 29, 2007, 12:25pm UTC](https://www.ruby-forum.com/t/drying-code-through-meta-programming/83741 "2007-01-29T12:25:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![svenax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@svenax](https://www.ruby-forum.com/u/svenax)\
**Post date:** [January 29, 2007, 12:25pm UTC](https://www.ruby-forum.com/t/drying-code-through-meta-programming/83741/1 "2007-01-29T12:25:32Z")

</div>

I am using the following idiom to validate form data that is not  
stored in a table:

```
class FormData < ActiveRecord::BaseWithoutTable
  column :date_column, :date
  column :int_column, :integer
  validates_date :date_column
  validates_numericality_of :int_column
end

unless request.get?
  @report = FormData.new(params[:report])
  @report.save
else
  @report = FormData.new
end

```

This works great (BaseWithoutTable and ValidatesDate are non-standard  
packages), but it contains lots of boilerplate code that I’d like to  
factor out. I would like to do something like this instead:

```
form_validate do
  column :date_column, :date
  column :int_column, :integer
  validates_date :date_column
  validates_numericality_of :int_column
end

```

but I’m unsure how I could do this. Something like:

def form\_validate(init = {}, &block)  
instance\_eval \<\<-CODE  
class Eval \< ActiveRecord::BaseWithoutTable  
#{block}  
end  
unless request.get?  
@report = Eval.new(params[:report])  
@report.save  
else  
@report = Eval.new(#{init})  
end  
CODE  
end

doesn’t work. Do I need to use method\_missing to parse the lines from  
the block or is there a better way?

---

<div class="post-metadata">

**Author:** ![svenax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@svenax](https://www.ruby-forum.com/u/svenax)\
**Post date:** [January 29, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/drying-code-through-meta-programming/83741/2 "2007-01-29T13:41:53Z")

</div>

Hi –

On Mon, 29 Jan 2007, svenax wrote:

> form\_validate do  
> column :date\_column, :date  
> column :int\_column, :integer  
> validates\_date :date\_column  
> validates\_numericality\_of :int\_column  
> end

I probably just haven’t had enough coffee, but I’m not seeing how this  
differs significantly from what you’ve got above, in terms of amount  
of code. Or is the savings inside the two add-on packages?

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![svenax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@svenax](https://www.ruby-forum.com/u/svenax)\
**Post date:** [January 29, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/drying-code-through-meta-programming/83741/3 "2007-01-29T14:36:11Z")

</div>

Try some more coffee 😉

Well, it would eliminate

```
unless request.get?
  @report = FormData.new(params[:report])
  @report.save
else
  @report = FormData.new
end

```

from each time I use this idiom.

Maybe I really want to ask the general question: How do I treat a code  
block like a string to be used in some XXX\_eval function? I can’t do  
block.to\_s can I? No, that would be too simple …

---

<div class="post-metadata">

**Author:** ![svenax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@svenax](https://www.ruby-forum.com/u/svenax)\
**Post date:** [January 29, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/drying-code-through-meta-programming/83741/4 "2007-01-29T18:22:38Z")

</div>

svenax wrote:

> Maybe I really want to ask the general question: How do I treat a code  
> block like a string to be used in some XXX\_eval function? I can’t do  
> block.to\_s can I?

I thought most evals could take blocks as arguments, or as do-end  
blocks. Do you mean you need to do string surgery to the block first?  
I would try the opposite of “meta” programming, first. 🙂

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![svenax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@svenax](https://www.ruby-forum.com/u/svenax)\
**Post date:** [January 29, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/drying-code-through-meta-programming/83741/5 "2007-01-29T21:57:40Z")

</div>

On Jan 29, 6:20 pm, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> svenax wrote:
> 
> > Maybe I really want to ask the general question: How do I treat a code  
> > block like a string to be used in some XXX\_eval function? I can’t do  
> > block.to\_s can I?I thought most evals could take blocks as arguments, or as do-end  
> > blocks. Do you mean you need to do string surgery to the block first?  
> > I would try the opposite of “meta” programming, first. 🙂

That is exactly what I mean. Opposite? You mean hyperprogramming?

/sven axelsson

---

<div class="post-metadata">

**Author:** ![svenax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@svenax](https://www.ruby-forum.com/u/svenax)\
**Post date:** [January 30, 2007, 12:02am UTC](https://www.ruby-forum.com/t/drying-code-through-meta-programming/83741/6 "2007-01-30T00:02:07Z")

</div>

svenax wrote:

> That is exactly what I mean. Opposite? You mean hyperprogramming?

No, uh, like… programmging. if statements. +. You know…

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![svenax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@svenax](https://www.ruby-forum.com/u/svenax)\
**Post date:** [January 30, 2007, 2:27am UTC](https://www.ruby-forum.com/t/drying-code-through-meta-programming/83741/7 "2007-01-30T02:27:26Z")

</div>

svenax wrote:

> So the question still stands: Is it possible to  
> stringify a code block in order to perform  
> some string surgery on it before handing it off to  
> an eval function?

Absolutely. What have you tried? What problem(s) did you have?

---

<div class="post-metadata">

**Author:** ![svenax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@svenax](https://www.ruby-forum.com/u/svenax)\
**Post date:** [January 30, 2007, 1:28am UTC](https://www.ruby-forum.com/t/drying-code-through-meta-programming/83741/8 "2007-01-30T01:28:20Z")

</div>

On Jan 30, 12:00 am, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> svenax wrote:
> 
> > That is exactly what I mean. Opposite? You mean hyperprogramming?  
> > No, uh, like… programmging. if statements. +. You know…

Yeah, so? I’ve alrady done that, so I thought it was time to try  
something new.

So the question still stands: Is it possible to stringify a code block  
in order to perform some string surgery on it before handing it off to  
an eval function?
