The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
The Siphoning Splat

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
The Siphoning Splat Posted: Jan 2, 2007 8:42 AM
Reply to this message Reply

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
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

I’m serious about this. The splat is such a wildly devious little asterisk.

>> instruments = {'trombones' => 1, 'clarinets' => 2} 
>> p instruments.to_a
[["trombones", 1], ["clarinets", 2]]
>> p *instruments
["trombones", 1]
["clarinets", 2]

A 2-in-1 combo move! Look at that splat. It’s not just to_a. It called to_a and 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!

Read: The Siphoning Splat

Topic: Online Programming Contest Previous Topic   Next Topic Topic: Quick and Dirty packaging of Camping apps for local use on win32

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use