Archive for the 'ruby' Category

18th Jul 2007

How-to: Pop-up Menu Selection AJAX in Rails

The problem: In Rails, how do you create a popup menu that calls a given action (to replace a div or whatever else) and passes the selected option as an argument when a new selection is made?

This took me way longer than I thought it would to figure out how to do, so I’m posting the answer here as well for anyone trying to do the same thing.


< %= select_tag :person_choice, options_for_select(@persons),
:onchange => remote_function(:update => “divToBeUpdated”, :with => ‘Form.Element.serialize(this)’, :url => { :action => ‘update_person’}) %>

The key here is the :with => ‘Form.Element.serialize(this)’ option. It’s a bit of magic javascript that passes the selected object along with AJAX call, but I actually don’t really understand it much beyond that. If anyone wants to give me a more thorough explanation, it would be highly appreciated.

Huzzah!
Update: Props to OnRails.org.

Posted by Posted by patrick under Filed under ajax, code, how-to, rails, ruby Comments 1 Comment »

28th Nov 2006

A Fun Project: Perchance, an HTTP Server in Ruby

I’ve started a new project (ha! as if I needed another): I’m trying to write an HTTP server in ruby using just the HTTP/1.1 RFC (RFC 2616) and the ruby docs. It’s called Perchance, just because I’m feeling rather serendipitous. It’s been mildly successful so far. I’ve gotten it to connect to respond to a basic GET request, which means that the socket stuff is working good. Now I just have to worry about the proper formatting of the protocol. It’s really just a matter of coming up with cleaver ways to read and respond to the requests, because right now it’s pretty crude.

I’m doing this because I think it’ll be useful to be able to create an application from only an RFC. These things are pretty dry, and to be able to read through them and extract the requirements and create a program that adheres to them is probably a pretty useful skill, especially since all the documentation on cool stuff like SIP and NAT-Transversal tend to be hidden away inside RFCs.

Also, learning the ruby socket stuff is fun. It feels useful already.

I don’t know if it’s something I’ll ever release (something tells me the world could go without another httpd) but it’s definitely an interesting project for the time being. I’ll update the status on it around the new year. Hopefully I’ll have most of the spec done by then.

fortune spat out something funny and relevant today:

“Documentation is like sex: when it is good, it is very, very good; and when it is bad, it is better than nothing.”
— Dick Brandon

Posted by Posted by patrick under Filed under http, ruby Comments 1 Comment »

05th Oct 2006

N yvggyr Ehol…

I’ve been asked to lead a Ruby session at my school for a small group of students interested in learning the language. As such, I’ve been brushing up on it lately, and I came up with a fun little script I thought I’d share.

Our topic is cryptography, and in the spirit of fun I thought I would show how easy it is to write a ROT13 cipher in Ruby. Here it is:

def uppercase_swap(byte)
  if /[A-M]/ === byte.chr then byte += 13
  else byte -= 13 end
end

def lowercase_swap(byte)
  if /[a-m]/ === byte.chr then byte += 13
  else byte -= 13 end
end

ARGV.shift.each_byte do |byte|
  byte = uppercase_swap byte if /[A-Z]/ =~ byte.chr
  byte = lowercase_swap byte if /[a-z]/ =~ byte.chr
  print byte.chr.to_s
end
print "\n"

As you can see, it’s not very big. Ignoring the 2 function definitions at the top momentarily, all we are doing is taking the first argument (ARGV.shift), and iterating through each byte –each character of the string. With each byte, we are checking to see if it is an upper or lowercase letter and calling the appropriate function to swap it with a different letter if it is.

Simple. Nice.

If anyone has any input on how to improve this code, please let me know. UPDATE: I made it better, here’s how:

def swapLetter(byte)
  if /[A-M]|[a-m]/ === byte.chr then byte += 13
  else byte -= 13 end
end

ARGV.shift.each_byte do |byte|
  byte = swapLetter(byte) if /[A-Z]|[a-z]/ =~ byte.chr
  print byte.chr.to_s
end
print "\n"

Merging functions is goood.

Posted by Posted by patrick under Filed under crypto, ruby Comments 2 Comments »