Ruby on Rails Thursday, July 29, 2010

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
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment