# Stupid question about blocks in before\_validation for eg

**URL:** <https://www.ruby-forum.com/t/stupid-question-about-blocks-in-before-validation-for-eg/192419>\
**Category:** Rails\
**Created:** [July 29, 2010, 11:07pm UTC](https://www.ruby-forum.com/t/stupid-question-about-blocks-in-before-validation-for-eg/192419 "2010-07-29T23:07:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 29, 2010, 11:07pm UTC](https://www.ruby-forum.com/t/stupid-question-about-blocks-in-before-validation-for-eg/192419/1 "2010-07-29T23:07:42Z")

</div>

Hi,

I’m hitting a wall in my ruby knowledge. When I have:

before\_validation { |object| object.permalink =  
object.title.parameterize }

What’s that { |object| …} thing? It’s a block, ok, but when does it  
get executed? When and where does object come from?

I tried digging in the Rails source code, but there is a lot going on so  
I’m getting confused.

I’m asking this question because my real problem is the following:

I’m using the paperclip plugin for one of my rails app, and here is the  
code I am trying to make work:

class Asset \< ActiveRecord::Base

has\_attached\_file :data, :styles =\> {  
:theora =\> [:format =\> :ogv, :processors =\> lambda {|a| a.video? ?  
[:video\_converter] : []}}]  
}

end

What fails is the lambda, because in Paperclip behind the scenes this is  
what happens (edited for brevity):

@some\_var = style.processors.inject(something) do |file, processor|  
…  
end

style.processors has received the lambda defined earlier. But doesn’t it  
get the asset object and get executed when the interpreter hits the line  
like the before\_validation does?

So I get the following error message:

NoMethodError (undefined method `inject’ for #Proc:0x455d864)

How can I fix this problem?

Surprisingly if I define the following, the lambda does not cause to  
crash:

has\_attached\_file :data, :styles =\> {  
:theora =\> [:format =\> :ogv],  
:processors =\> lambda {|a| a.video? ? [:video\_converter] : []}  
}

Can someone shed some light on this lambda thing? And after that which  
book or resources can I read to get proficient in such dynamic coding  
practice? Currently in use blocks all the time ([array].each { …})  
without really mastering them.

Thanks for your support

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 30, 2010, 10:31am UTC](https://www.ruby-forum.com/t/stupid-question-about-blocks-in-before-validation-for-eg/192419/2 "2010-07-30T10:31:24Z")

</div>

On Jul 29, 10:07 pm, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I’m hitting a wall in my ruby knowledge. When I have:
> 
> before\_validation { |object| object.permalink =  
> object.title.parameterize }
> 
> What’s that { |object| …} thing? It’s a block, ok, but when does it  
> get executed? When and where does object come from?

That depends entirely on what the before\_validation does. In general  
such a method could call it straight away or it could stash away the  
block for use at some later point. According to the active record docs  
that block will be called before an object is validated and the  
parameter passed to the block is the object in question.

> style.processors has received the lambda defined earlier. But doesn’t it  
> get the asset object and get executed when the interpreter hits the line  
> like the before\_validation does?
> 
> So I get the following error message:
> 
> NoMethodError (undefined method `inject’ for #Proc:0x455d864)
> 
> How can I fix this problem?

That’s just saying that it was expect an array of lambdas, rather than  
a single one - i suppose the clue there (other than the paperclip  
source code & docs) is that the option is called processors (ie a  
plural)

> Surprisingly if I define the following, the lambda does not cause to  
> crash:
> 
> has\_attached\_file :data, :styles =\> {  
> :theora =\> [:format =\> :ogv],  
> :processors =\> lambda {|a| a.video? ? [:video\_converter] : []}
> 
> }

paperclip probably isn’t expecting the :processors option there and is  
just ignoring it.

> Can someone shed some light on this lambda thing? And after that which  
> book or resources can I read to get proficient in such dynamic coding  
> practice? Currently in use blocks all the time ([array].each { …})  
> without really mastering them.

In a nutshell lambda turns a bloc into a Proc object that you can pass  
around and manipulate like any other object.  
Have you read the pickaxe?

Fred

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [July 30, 2010, 11:56am UTC](https://www.ruby-forum.com/t/stupid-question-about-blocks-in-before-validation-for-eg/192419/3 "2010-07-30T11:56:22Z")

</div>

> paperclip probably isn’t expecting the :processors option there and is  
> just ignoring it.

No it is a valid option here and gets executed correctly.

> Have you read the pickaxe?

I’ll read it again, I probably went too quickly over a few chapters the  
first times 🙂
