This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: The Siphoning Splat
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
A 2-in-1 combo move! Look at that splat. It’s not just to_a. It called to_aand it siphoned the arguments.
So, Camping has an r method which is for making HTTP responses from scratch. The signature of this method is: r(status, body, headers = {}).
r(404, 'Not Found', 'X-Powered-By' => 'Camping')
And it has controller objects (descended from Camping::Base) which you run into sometimes. These objects have @status, @headers and @body ivars inside them.
class Camping::Base
def to_a
[@status, @body, @headers]
end
end
With the above to_a method, I can now do:
r(*controller)
Which can be used as a syntax for forwarding HTTP requests.
See, Camping has a technique for making requests inside itself. For example, let’s say we’ve got a Search controller that takes a q variable with search terms in it. And, let’s say we want the front page (the Index controller) to show the results page for a search containing the word “clarinet.”
class Index < R '/'
def get
r *AppName.post(:Search, :env => @env, :input => {:q => "clarinet"})
end
end
The splat is Ruby’s pipe for arrays. Put that in your pipe and splat it!