[1,2,3].send(:collect,Proc.new{|x| x.to_s + "!"})
This fails. Any ideas how I could work around this? How do you use
Object#send (or similar) with a block?
Xavier
--
SD Ruby mailing list
sdr...@googlegroups.com
http://groups.google.com/group/sdruby
I'm building a collection of transformations which can be described as
chained method invocations. How do you extract related entities from
an nokogiri document of an amazon review page? By applying these
transformations:
https://github.com/derdewey/amzn-scraper/blob/master/amazon_review.rb
. Find a common node to describe the entity, then extract each of the
elements and return a handy hash.
Ben,
So that works when it's being passed directly to send but it won't
work when passed in from a splatted array!
[1,2,3].send(*[:collect, &Proc.new{|x| x.to_s + "!"}])
-> SyntaxError: (irb):1: expecting ']'
My workaround, which is kinda lame, is to just extend the nokogiri class.
Xavier
That doesn't work because you can't put a block in an array:
>> [:collect, &Proc.new{|x| x.to_s + '!'}]
SyntaxError: compile error
Instead of storing the call and args in an array, you might want to
consider a hash so you can label and handle blocks special case.
There's some ambiguity in just shoving it in at the end of the args
(is it a user argument or a handler?).
--
Kevin Clark
http://glu.ttono.us
--
--
Kevin Clark
http://glu.ttono.us
[1,2,3].send(:collect, :block => lambda{|x| x.to_s+"!"})
That obviously wouldn't work because it's still ambiguous. Oh well.
I'll just extend the nokogiri class! Thanks for playing everyone!
Xavier
Are you still with powerset? How's it working for the man? Are you still in san jose?
Sent from my iPhone
Not quite. Send can pass the block just fine. But the way you're
storing your information doesn't allow you (as the person calling
send) to split it out.
=> {:method=>:collect, :block=>#<Proc:0x00000001012ee558@(irb):2>}
>> [1,2,3].send(data[:method], &data[:block])
=> ["1!", "2!", "3!"]
I was saying the way you're storing what essentially amount to bound
method calls is ambiguous:
REVIEW_EXTRACTION =
{
:most_common_node => [[:css, "a + br + div > div + div >
span > span > span"], [:collect, &Proc.new{|x|
x.parent_node.parent_node.parent_node.parent_node.parent_node}]],
[:collect, &...] could only express method(arg1, arg2, arg3, &myblock)
if you strip off the first and last item, and set args equal to the
rest. You couldn't just splat everything after collect and expect it
to work (since blocks aren't really a positional argument).
Does that makes sense? You don't need to extend the class, you just
need to tweak your data.
[1,2,3].send(:collect, :block => lambda{|x| x.to_s+"!"})
That obviously wouldn't work because it's still ambiguous. Oh well.
I'll just extend the nokogiri class! Thanks for playing everyone!