Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1085127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:38:55+00:00 2026-05-16T22:38:55+00:00

I noticed (and verified in the sunspot code) the following behavior class Foo <

  • 0

I noticed (and verified in the sunspot code) the following behavior

class Foo < ActiveRecord::Base
  def  bar
    search_str = "foo"
    Boo.search do
      keywords(search_str)
      p self.id
      p self
   end
 end
end

In the code above, the DSL block can access the variables defined in
the context. But the self inside block, points to an instance of
Sunspot::DSL::Search class (instead of an instance of Foo class.)
When I try to access self.id, instead of getting the id of a Foo
object; I get the id of a Sunspot::DSL::Search object.

I think Sunpot is doing some binding swapping/delegation magic in Util.instance_eval_or_call method.

I am curious why Sunspot does this and why there is no warning about
this behavior in documentation.

Edit:

The Sunspot search method can be found at this link

The code below will illustrate my point. In the method foo I have a block that behaves as expected. In the method bar, the block doesn’t behave.

class Order < ActiveRecord::Base  

  def foo
    p self.class.name # prints Order

    # The `self` inside the block passed to the each method
    # points to an object of type Order (as expected)
    # This is the normal block behavior.
    [1,2,3].each do |val|
      p self.class.name # prints Order
    end
  end


  def bar

    p self.class.name # prints Order

    # the `self` inside the block passed to the search method
    # points to an object of type Sunspot::DSL::Search.
    # This is NOT the normal block behavior.

    Order.search do
      keywords("hello")
      p self.class.name # prints Sunspot::DSL::Search
    end
end

Note2

I have located the code in Sunspot source tree that modifies the normal block behavior. My question is about the reason for rigging the binding like this.

Note3

Specifically, I found an issue while invoking the id method in side the block. The search method delegates the method invocation inside the block to the DSL object and if it doesn’t find the method then call is re-delegated to the calling context. Search method strips all but the essential methods from the DSL object before registering delegation code. The id method is not stripped out. This is causing the problem. For all the other methods delegation works fine.

This behavior is not documented in the Sunspot method documentation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:38:56+00:00Added an answer on May 16, 2026 at 10:38 pm

    Ok, I know how it works:

    The magic is found in ContextBoundDelegate in util.rb.

    • It creates a blank slate delegator object.
    • The delegator forwards all method calls to the ‘receiver’. In your example the ‘receiver’ is probably the object which contains the methods keywords and with and any_of and so on.
    • If a given method is not found in ‘receiver’ then it forwards the method call onto the ‘context’ object
    • The context object is the object that holds the bindings for the block.
    • You find the context object for a given block by doing this: eval('self', block.binding)

    Rationale:

    So the effect of all this is that the block not only gets access to the methods in the search object (a la instance_eval) but it also gets access to local methods in the calling scope of the block.

    The block also, of course, gets access to local variables in the calling scope of the block, but this is just normal closure behaviour.

    The block does not, however, get access to instance variables in the calling scope of the block.

    The following code might be useful as it follows roughly the same idea but is much simpler and less sophisticated: Using methods from two different scopes?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed the following behavior in the following code (using threading.Timer class): import threading
Noticed this today when a patch was submitted with the following line: lblCompletionTime.Text =
I noticed that the <td align=center> code works differently on the MediaWiki.org page and
I have recently noticed that my test code is too close to the production
While debugging some C# code during a sort of peer-review, I noticed an odd
I'm crossfading between two views using UIView animation. I've noticed the following surprising fact:
I noticed that there are two sets of Hadoop configuration parameters: one with mapred.*
I noticed that Clojure (1.4) seems to be happy to consider vectors equal to
I noticed that IE 9 does not support application cache. How can I use
I noticed that the first creation of an image from an uri takes pretty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.