# Problem with a lambda not executing immediately

**URL:** <https://www.ruby-forum.com/t/problem-with-a-lambda-not-executing-immediately/192415>\
**Category:** Ruby\
**Created:** [July 29, 2010, 10:38pm UTC](https://www.ruby-forum.com/t/problem-with-a-lambda-not-executing-immediately/192415 "2010-07-29T22:38:32Z")\
**Posts on this page:** 7\
**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, 10:38pm UTC](https://www.ruby-forum.com/t/problem-with-a-lambda-not-executing-immediately/192415/1 "2010-07-29T22:38:32Z")

</div>

Hi,

I’m using the paperclip plugin for one of my rails app, but my problem  
is pure Ruby. I’m not good enough with Ruby and lambdas so I need your  
advice.

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:

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

style.processors has received the lambda defined earlier.

And I get the following error message:

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

So how can I fix it, 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, 1:07am UTC](https://www.ruby-forum.com/t/problem-with-a-lambda-not-executing-immediately/192415/2 "2010-07-30T01:07:08Z")

</div>

On Thu, Jul 29, 2010 at 3:38 PM, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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

You have an extra } before ]. I’m assuming that wasn’t part of your  
actual script, because then you’d get a syntax error.

> And I get the following error message:
> 
> NoMethodError (undefined method `inject’ for #Proc:0x455d864)

It looks like style.processors is itself a proc instead of an  
enumerable. Try to find where it was assigned and make sure you pass  
the proc inside of an array.

---

<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, 8:21am UTC](https://www.ruby-forum.com/t/problem-with-a-lambda-not-executing-immediately/192415/3 "2010-07-30T08:21:26Z")

</div>

On Thu, Jul 29, 2010 at 10:38 PM, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> has\_attached\_file :data, :styles =\> {  
> @var = style.processors.inject(something) do |file, processor|
> 
> So how can I fix it, 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.

I have no idea about paperclip, but looking at the above snippets  
style.processor should be an Enumerable (which is where inject is  
defined), for example an array. Given that the key in the hash is  
:processors (in plural) I would assume that the value should be an  
array of processors. So, even if you only have one lambda, can you try  
this:

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

Jesus.

---

<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:40am UTC](https://www.ruby-forum.com/t/problem-with-a-lambda-not-executing-immediately/192415/4 "2010-07-30T11:40:54Z")

</div>

> Given that the key in the hash is  
> :processors (in plural) I would assume that the value should be an  
> array of processors.

Yes style.processors is expected to be an array

> So, even if you only have one lambda, can you try this:
> 
> class Asset \< ActiveRecord::Base  
> has\_attached\_file :data, :styles =\> {  
> :theora =\> [:format =\> :ogv, :processors =\> [lambda {|a| a.video?  
> ? [:video\_converter] : []}]}]  
> }

Unfortunately it doesn’t work, as when the lambda gets executed, the “a”  
object it receives is not the not it expects: “wrong constant name  
passed”.

---

<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:50am UTC](https://www.ruby-forum.com/t/problem-with-a-lambda-not-executing-immediately/192415/5 "2010-07-30T11:50:50Z")

</div>

Here is the correct error message with the latter code:

wrong constant name #Proc:0x0445b600@:14::App::Models::Asset.rb

---

<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:** [August 1, 2010, 11:26am UTC](https://www.ruby-forum.com/t/problem-with-a-lambda-not-executing-immediately/192415/6 "2010-08-01T11:26:26Z")

</div>

Hi Rick,

> the :processors option isn’t supposed to be part of the :style

Yes it can. It’s undocumented, I have tried and it works, but only if I  
hardcode the processors array. Using the lambda doesn’t work for  
processors defined for specific styles.

I’ll read the pickaxe to improve my ruby skills.

---

<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 31, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/problem-with-a-lambda-not-executing-immediately/192415/7 "2010-07-31T17:00:00Z")

</div>

On Fri, Jul 30, 2010 at 5:40 AM, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > ```
> > :theora => [:format => :ogv, :processors => [lambda {|a| a.video?
> > 
> > ```
> > 
> > ? [:video\_converter] : []}]}]  
> > }
> 
> Unfortunately it doesn’t work, as when the lambda gets executed, the “a”  
> object it receives is not the not it expects: “wrong constant name  
> passed”.

I suspect that you would get better answers on the rails forum since  
paperclip is better known there than in the general Ruby community.  
but…

First, the :processors option isn’t supposed to be part of the :style  
hash

has\_attached\_file :date,  
:styles =\> {:theora =\> {:format =\> :ogv},  
:processors =\> [  
# something goes here  
]

Now it appears you are trying to use a feature of paperclip which  
doesn’t seem to be documented, normally the value for the :processors  
option is just an array of symbols which are converted to an instance  
of a subclass of Paperclip::Processor. But from reading the code it  
appears that you can also do:

class Asset \< ActiveRecord::Base

has\_attached\_file :date,  
:styles =\> {:theora =\> {:format =\> :ogv},  
:processors =\> lambda { | asset | # the  
instance of the Asset model

asset.video? ? [:video\_converter] : []  
}  
end

I’d probably refactor this a bit:

class Asset \< ActiveRecord::Base

has\_attached\_file :date,  
:styles =\> {:theora =\> {:format =\> :ogv},  
:processors =\> lambda { | asset |  
asset.processors }

def processors  
self.video? ? [:video\_converter] : []  
end  
end

I’ve tested none of this so take it with the appropriate grain of salt.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
