Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Help with some ruby metaprogramming
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Xavier Lange  
View profile  
 More options Jan 31 2011, 7:50 pm
From: Xavier Lange <xrla...@gmail.com>
Date: Mon, 31 Jan 2011 18:50:56 -0600
Local: Mon, Jan 31 2011 7:50 pm
Subject: Help with some ruby metaprogramming
I'm building a HTML extractor on top of nokogiri which applies a
collection of CSS search strings and more to build a logical
extraction of data and I wanted to use something like this:

[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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Hughes  
View profile  
 More options Jan 31 2011, 7:54 pm
From: Ben Hughes <m...@benhughes.name>
Date: Mon, 31 Jan 2011 16:54:21 -0800
Local: Mon, Jan 31 2011 7:54 pm
Subject: Re: [SDRuby] Help with some ruby metaprogramming

You're just missing one character:

[1,2,3].send(:collect, &Proc.new{|x| x.to_s + "!"})

& will pass that proc object in as a block to the method.

--
Ben Hughes
http://benhugh.es/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Grant  
View profile  
 More options Jan 31 2011, 8:06 pm
From: Adam Grant <adam.jgr...@gmail.com>
Date: Mon, 31 Jan 2011 17:06:09 -0800
Local: Mon, Jan 31 2011 8:06 pm
Subject: Re: [SDRuby] Help with some ruby metaprogramming

Any reason why you aren't calling "collect" directly from the Array?
_
Adam


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Xavier Lange  
View profile  
 More options Jan 31 2011, 10:20 pm
From: Xavier Lange <xrla...@gmail.com>
Date: Mon, 31 Jan 2011 21:20:12 -0600
Local: Mon, Jan 31 2011 10:20 pm
Subject: Re: [SDRuby] Help with some ruby metaprogramming
Adam,

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Clark  
View profile  
 More options Jan 31 2011, 10:34 pm
From: Kevin Clark <kevin.cl...@gmail.com>
Date: Mon, 31 Jan 2011 19:34:55 -0800
Local: Mon, Jan 31 2011 10:34 pm
Subject: Re: [SDRuby] Help with some ruby metaprogramming

> 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 ']'

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jordan Fowler  
View profile  
 More options Jan 31 2011, 10:41 pm
From: Jordan Fowler <m...@jordanfowler.com>
Date: Mon, 31 Jan 2011 19:41:28 -0800
Local: Mon, Jan 31 2011 10:41 pm
Subject: Re: [SDRuby] Help with some ruby metaprogramming

You can also use a lambda:

ruby-1.9.2-p136 :004 > [:collect, lambda { |x| x.to_s + "!" }]
 => [:collect, #<Proc:0x000001009a29b8@(irb):4 (lambda)>]

--
Jordan A. Fowler
E-mail: m...@jordanfowler.com
Website: http://www.jordanfowler.com
Phone: (619) 339-6752

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jordan Fowler  
View profile  
 More options Jan 31 2011, 10:43 pm
From: Jordan Fowler <m...@jordanfowler.com>
Date: Mon, 31 Jan 2011 19:43:24 -0800
Local: Mon, Jan 31 2011 10:43 pm
Subject: Re: [SDRuby] Help with some ruby metaprogramming

Oh wait, I missed the part about needing an anonymous block. Hmm...

--
Jordan A. Fowler
E-mail: m...@jordanfowler.com
Website: http://www.jordanfowler.com
Phone: (619) 339-6752

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Clark  
View profile  
 More options Jan 31 2011, 10:49 pm
From: Kevin Clark <kevin.cl...@gmail.com>
Date: Mon, 31 Jan 2011 19:49:43 -0800
Local: Mon, Jan 31 2011 10:49 pm
Subject: Re: [SDRuby] Help with some ruby metaprogramming
Yeah, doesn't matter if you could curry it or not - it needs to be
passed as a block. If you don't separate them, there's no way to
detect it *should* be separated. *args can't work. That's why method
missing takes them separate - blocks are considered their own part of
the call, and you only get one.

--
Kevin Clark
http://glu.ttono.us

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Xavier Lange  
View profile  
 More options Feb 1 2011, 12:11 am
From: Xavier Lange <xrla...@gmail.com>
Date: Mon, 31 Jan 2011 23:11:42 -0600
Local: Tues, Feb 1 2011 12:11 am
Subject: Re: [SDRuby] Help with some ruby metaprogramming
As my knowledge of Object#send stands now: it can't recreate the full
breadth of method invocations in ruby. Object#send should accept an
optional parameter:

[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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mike O'Brien  
View profile  
 More options Feb 1 2011, 12:19 am
From: Mike O'Brien <mcob...@yahoo.com>
Date: Mon, 31 Jan 2011 21:19:08 -0800
Local: Tues, Feb 1 2011 12:19 am
Subject: Re: [SDRuby] Help with some ruby metaprogramming
Hey man, what are you up to these days???

Are you still with powerset?  How's it working for the man?  Are you still in san jose?

Sent from my iPhone

On Jan 31, 2011, at 7:49 PM, Kevin Clark <kevin.cl...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin Clark  
View profile  
 More options Feb 1 2011, 12:33 am
From: Kevin Clark <kevin.cl...@gmail.com>
Date: Mon, 31 Jan 2011 21:33:53 -0800
Local: Tues, Feb 1 2011 12:33 am
Subject: Re: [SDRuby] Help with some ruby metaprogramming

On Mon, Jan 31, 2011 at 9:11 PM, Xavier Lange <xrla...@gmail.com> wrote:
> As my knowledge of Object#send stands now: it can't recreate the full
> breadth of method invocations in ruby. Object#send should accept an
> optional parameter:

> [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!

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.

--
Kevin Clark
http://glu.ttono.us


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Aimonetti  
View profile  
 More options Feb 1 2011, 12:41 am
From: Matt Aimonetti <mattaimone...@gmail.com>
Date: Mon, 31 Jan 2011 21:41:25 -0800
Local: Tues, Feb 1 2011 12:41 am
Subject: Re: [SDRuby] Help with some ruby metaprogramming

Xavier,

> [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!

I didn't read the entire thread but that would work:
[1,2,3].map &lambda{|x| x.to_s+"!"}
=> ["1!", "2!", "3!"]

>> [1,2,3].send(:map, &lambda{|x| x.to_s+"!"})

=> ["1!", "2!", "3!"]

Probably not what you're after tho.

Mike...., you might want to contact Kevin directly and not via the ML ;)
(and super scoop, no he doesn't work for Powerset/Microsoft anymore ;))

- Matt


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dan Simpson  
View profile  
 More options Feb 1 2011, 11:18 am
From: Dan Simpson <dan.simp...@gmail.com>
Date: Tue, 1 Feb 2011 08:18:09 -0800
Local: Tues, Feb 1 2011 11:18 am
Subject: Re: [SDRuby] Help with some ruby metaprogramming

I'll give one more option:

ruby-1.9.2-p0 > [1,2,3].send(:collect) { |x| x * 2 }
 => [2, 4, 6]

Not sure if that suits your use case.

--Dan

On Mon, Jan 31, 2011 at 9:41 PM, Matt Aimonetti <mattaimone...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »