Arc Challenge: HumaneBrick

By kidko on December 20th, 2009

HumaneBrick, my little wrapper for Ruby’s ill-document WEBrick, is readable. But can it take on the Arc Challenge? I decided to find out.

The idea behind the Challenge (found here) is as follows:

Write a program that causes the url said (e.g. http://localhost:port/said) to produce a page with an input field and a submit button. When the submit button is pressed, that should produce a second page with a single link saying “click here.” When that is clicked it should lead to a third page that says “you said: …” where … is whatever the user typed in the original input field. The third page must only show what the user actually typed. I.e. the value entered in the input field must not be passed in the url, or it would be possible to change the behavior of the final page by editing the url.

So how do the Arc people do it?

(defop said req
  (aform [w/link (pr "you said: " (arg _ "foo"))
           (pr "click here")]
    (input "foo") 
    (submit)))

And now in HumaneBrick…

require 'humane'
server = HB::Server.new
server.path "/" do |said|
  if posted? :said then write "You said %s" % said
  else write '<form method="post" action="/mid"><input name="said" type="text" /><input type="submit" /></form>'; end
end
server.path "/mid" do |said|
  write '<form method="post" action="/"><input name="said" type="text" value="'+said[0]+'" style="display:none"/><input type="submit" value="click here"/></form>'
end
server.start!

While it’s not shorter by any means, I like to think that it’s a lot more readable. Note that there’s no way to efficiently curry POST queries from a GET to another GET, which is why I had to set up that little hidden form in /mid.

Posted in problems, programming, web | 0 Comments

A Modest Twitter Proposal

By kidko on November 24th, 2009

Everybody knows Twitter’s a small, instant, sound-bite kind of medium. But what if your little thought of the moment requires 150 characters, and can’t be cut down without reducing meaning?

Twitter users introduced @mentions, #hashtags, and the RT (retweet). I’m going to go ahead and mention the idea of a multi-part tweet:

kidko tweet tweet, here’s the first part .. /p1
kidko..And then comes the rest of it. Note the two periods in both tweets. /p2

That whole charade could go on for pages and pages, but really, I just hope it catches on in the first place.

Posted in Uncategorized | 0 Comments

Easy Ruby “Binaries”

By kidko on October 15th, 2009

A lot of Ruby projects that I’ve run into on Github have some kind of binary under a bin/ directory. rails, monk, jekyll, and so on. Even my small projects sometimes have them just for convenience. But re-writing the basic code is a pain each time. Thus, a framework is born.

Enter Canine. It’s a class for this sort of thing. It’s as easy as loading it and passing a block in a DSL (domain-specific language):

require 'canine'
Canine.new do
  command "help", "commands"
  default_command "help" # :help works too
end

That as a script (assuming, in this case, that it’s in the same directory as the Canine library or Canine is in your libdir) will print its help message (the auto-generated command “commands”), and running without arguments will run the help command:

$ canine      # => help message
$ canine help # => help message

You can pass the commands blocks to run, with or without arguments:

require 'canine'
Canine.new do
  command "delete", "Delete your home directory" do |force|
    if force.downcase == "force" then
      `rm -r ~/*`
    end
  end
  options 'force' => 'Ensure you really want to do this'
end

You’d have to run:

$ canine delete force

to get any kind of reaction (it will delete your home directory, please don’t do this without knowing what that means).

It’s up on Github Gists for those that want it: http://gist.github.com/211445, and is a mere 65 lines of code for all of that fun stuff.

Posted in programming | 0 Comments

Packaging Some Love For Ruby

By kidko on August 19th, 2009

I have finally found a solution to my problem. I was actually spurred onwards by the sad event of the week: The disappearance of _why, a great artist & Ruby coder. His anonymous status has really come into play. What it boils down to is that nobody knows who he really is, so when all of his sites & accounts disappeared, he left no trace whatsoever. This includes Shoes.

So, what options do I have left to distribute Ruby apps? Well, if I ignore Mac OS X as a target platform since I don’t have access to an Apple machine… I could just pin up a copy of bare-bones Ruby with some libraries, and distribute that with my apps. WARP was born (clone URL: git://github.com/h0rs3r4dish/WARP.git)

Warp is an acronym, one that I don’t always capitalize because I think it looks cooler like a real word: WxWidgets And Ruby Packager, which will do exactly what I need. It will, once done, throw together a .zip file with Ruby, WxWidgets, and my app in it. Persistent data: check. GUI: check. Ruby: extra check.

Posted in hacks, programming, real life | 1 Comments

Loving And Hating Ruby

By kidko on August 12th, 2009

Update! Packaging Some Love For Ruby, in which I announce my own packaging system, Warp.

Every couple months I’ll go back to Ruby. I love it. I forget why I left it. I’ll code away, whipping out useful apps and what I like to think of as fairly complex code, and so on. And then comes the Big Issue, the reason I keep putting Ruby on the sidelines.

Now, don’t get me wrong. It’s an awesome way to code. Being able to access anything and change it (a string, the Integer class, and so on) is great. The syntax is a breath of fresh air:

amnt = gets.chomp.to_i
amnt.times { |which| puts "It's #%s" % which }

In fact, a lot of things are easy. attr_accessor makes setters/getters a breeze. File IO is ridiculously simple. There’s no reason to want to leave Ruby. That is, while I’m programming for me.

What if I want to spread my app around? Say, to somebody without Ruby. How would I do that? Well, let’s see… I could&em; no, that wouldn’t work. Wait, what about RubyScript2Exe? Right, that requires me to install Ruby on Windows. Just for development? I don’t think so. I could put Enemy Nations on instead.

Shoes (shoooes.net) had me going for a while… A stand-alone script that could generate Windows, Linux, and Mac executable files. It was awesome. Except… what’s that? Yes, my apps can’t create permanent files. No configurations, no saves, nothing. Shoes is out of the picture.

What does that leave me with? Nothing. And that, gentleman, is what keeps me from Ruby.

Posted in programming | 1 Comments

Next Page »