Wednesday, June 4, 2008

Write your javascript in ruby with rubyjs

I was inspired by Nathaniel Talbott's awesome railsconf talk to do some hacking. I had a lot of fun, and I think I came up with something worth sharing, so here it is. My willing victim was rubyjs, Michael Neumann's excellent little project I mentioned in an earlier post. If you want to play along at home, the first thing you'll need to do is fetch my code like so:
git clone git://github.com/superchris/rubyjs.git

There are some things in my repo that haven't made it into the gem version of rubyjs yet. My hack was a little "port" of the hangman example from Tapestry. It's there in examples/hangman. You can try it here. Or if you're so inclined, you can build it your dang self by running:

rake "examples/hangman/hangman.js"

This command uses a rake rule to run the rubyjs compiler which compiles hangman.rb to hangman.js.

So let's see some code how about, hmm?

class DOMElement
def initialize(element)
@dom_element = element
end

def observe(event, &block)
element = @dom_element
`
if (#<element>.addEventListener) {
#<element>.addEventListener(#<event>, #<block>, false);
} else {
#<element>.attachEvent("on" + #<event>, #<block>);
}
`
nil
end

def [](attribute)
element = @dom_element
`return #<element>[#<attribute>]`
end

def []=(attr, value)
element = @dom_element
`#<element>[#<attr>] = #<value>;`
nil
end

def self.find_js_element(element)
`return document.getElementById(#<element>);`
end


def self.find(element)
dom_element = self.find_js_element(element)
DOMElement.new(dom_element)
end

#
# Gets an HTML representation (as String) of an element's children.
#
# elem:: the element whose HTML is to be retrieved
# return:: the HTML representation of the element's children
#
def inner_html
elem = @dom_element
`
var ret = #<elem>.innerHTML;
return (ret == null) ? #<nil> : ret;`
end

#
# Sets the HTML contained within an element.
#
# elem:: the element whose inner HTML is to be set
# html:: the new html
#
def inner_html=(html)
elem = @dom_element
`
#<elem>.innerHTML = #<html>;
return #<nil>;`
end

end

The first class you see here is DOMElement. This gives some basic dom manipulation abilities in a ruby friendly way. I got this working by looking at the work Michael had done on porting GWT to ruby and extracting the bit I wanted and "rubifying" it a little. Basically, you can find elements by id, observe events with ruby blocks, and get/set attributes and inner html. Pretty simple, but has what I need. I should probably extract this into a separate file in rubyjs in case other people want it. You can also see lots of examples of how rubyjs and javascript talk to each other here.

Then it's on to the hangman code. First it's worth looking at the html so you can see what dom elements the code refers to:

And finally here's the hangman class:

class Hangman

attr_accessor :word, :letters, :misses

MAX_MISSES = 6

def initialize
@word = "snail"
@letters = @word.split ""
@guessed_letters = {}
@letters.each { |letter| @guessed_letters[letter] = false }
@misses = 0
@scaffold_div = DOMElement.find("scaffold_div")
@letters_div = DOMElement.find("letters")
@guess_input = DOMElement.find("letter")
@guess_button = DOMElement.find("guess")
@guess_button.observe("click") do
guess(@guess_input["value"])
end
@letters_div.inner_html = display_word
end

def display_word
letters.collect do |letter|
@guessed_letters[letter] ? letter : "_"
end.join
end

def guess(letter)
if letters.include?(letter)
@guessed_letters[letter] = true
@letters_div.inner_html = display_word
puts "You win!" if won?
else
@misses += 1
@scaffold_div.inner_html = "<img src='scaffold-#{@misses}.png' />"
if lost?
puts "You lost!"
@guess_button["disabled"] = true
end
end
@guess_input["value"] = ""
end

def lost?
@misses >= 6
end

def won?
@guessed_letters.values.each do |guessed|
return false unless guessed
end
return true
end

def self.main
hangman = Hangman.new
rescue StandardError => ex
puts ex
end
end

First you see the initialize method. Here is where we lookup our DOM elements, setup a Hash of which letters are guessed yet, and bind a block to the click event of our guess_button. Next comes the display_word method, which displays each letter or a blank if it's not been guessed.

The meat of the matter is in guess_letter, which is pretty simple. If the letter guessed is in the word we mark it, redisplay the word with the guessed letter and check if the user has won. If not, we update our miss count, display the right image, and check to see if the user lost. Won? and lost? are both trivial and not worth talking about.

Well, I had a lot of fun hacking on this. I think rubyjs has a good chance to be really useful as well as fun. It needs some love, certainly, but one of the things I did was start of woefully inadequate port of miniunit I'm calling microunit. It's in rubyjs/lib. This should make it easier and, I think, funner, to flesh out the core library for rubyjs.

For next steps I may expand on this example some. I'm thinking it would be a blast to tie to a Rails backend that gives me random words (right now it is always the same word :( ) or stores scores or some such foolishness. I could use this as an excuse to build an ActiveResource client for rubyjs and maybe a rubyjs on rails plugin.

If anyone cares about this, drop me a comment and let me know. Or if you think it's utterly stupid, tell me why. I promise to read all your comments and do whatever I feel like doing anyways :)

10 comments:

brainopia said...

This is absolutely cool!

I would definitely like to use something like rubyjs on rails =)

And it will be even more amazing if it will have a support for seamless integration with jquery!

May be something like

J('.some-class').each {|it| it.children().any? &:hidden? }.each do |element|
element.toggle().effect('highlight', {}, 2000)
end

tea42 said...

jQuery interface would be even more useful I think.

I'm still trying to understand the difference between red and rubyjs.

Anonymous said...

WoW shares many wow gold of its features with previously launched games. Essentially, you battle with wow gold cheap monsters and traverse the countryside, by yourself or as a buy cheap wow gold team, find challenging tasks, and go on to higher aoc gold levels as you gain skill and experience. In the course of your journey, you will be gaining new powers that are increased as your skill rating goes up. All the same, in terms of its features and quality, that is a ture stroy for this.WoW is far ahead of all other games of the genre the wow power leveling game undoubtedly is in a league of its own and cheapest wow gold playing it is another experience altogether.

Even though WoW is a Cheap Wow Gold rather complicated game, the controls and interface are done in warhammer gold such a way that you don't feel the complexity. A good feature of the game is that it buy wow items does not put off people with lengthy manuals. The instructions cannot be simpler and the pop up tips can help you start playing the game World Of Warcraft Gold immediately. If on the other hand, you need a detailed manual, the instructions are there for you to access. Buy wow gold in this site,good for you, BUY WOW GOLD.

Anonymous said...

i can get silkroad gold cheaply,
Yesterday i bought sro gold for my brother.
i hope him like it. i will give silkroad online gold to him
as birthday present. i like the cheap silkroad gold very much.
I usually buy the silk road gold and keep it in my store.
I can getSword of the New World Vischeaply,
Yesterday i boughtSword of the New World Gold for my brother.
i hope him like it. i will give Sword of the New World money to him
as birthday present. i like the cheap snw vis very much.
I usually buy vis and keep it in my store.

Anonymous said...

As a new player , you may need some game guides or information to enhance yourself.
wow gold is one of the hardest theme for every class at the beginning . You must have a good way to manage your World of Warcraft Gold.If yor are a lucky guy ,you can earn so many warcraft gold by yourself . But if you are a not , I just find a nice way to buy wow gold. If you need , you can buy cheap wow gold at our website . Go to the related page and check the detailed information .

Making aoc gold is the old question : Honestly there is no fast way to make lots of conan gold . Sadly enough a lot of the people that all of a sudden come to with millions of age of conan gold almost overnight probably duped . Although there are a lot of ways to make lots of cheap aoc gold here I will tell you all of the ways that I know and what I do to buy aoc money.

Anonymous said...

池袋 風俗
渋谷 風俗
新宿 風俗
コンドーム 激安
アダルトDVD
av 写真
アダルトグッズ
アダルトグッズ
アダルトDVD
アダルトショップ
ペニス増大
電マ
TENGA
SM 通販
セクシー下着
男性下着
Tバック下着
大規模修繕
決済代行
SEO
SEO
fether felt flat
決済代行
ブライダルエステ
FX 比較
クレジットカード 申込
子猫
子犬
仔猫
アダルトショップ
アダルトグッツ
ゴールドカード ランキング
調査料金 探偵
行動調査 探偵

Anonymous said...

夫の浮気調査 探偵
身元調査 探偵
所在調査 探偵
浮気調査
ストーカー調査
素行調査
盗聴調査
身元調査
不倫調査
不倫調査
尾行調査
企業調査
身元調査
身辺調査
夫の浮気 相談
浮気 相談
身上調査 相談
所在調査 相談
不倫調査 相談
盗聴調査 相談
結婚詐欺 相談
ダッチワイフ
ビジネス英会話
ビジネススクール 英語

Albert H. said...

What the hell?

liuyunxin said...

I really enjoy your posts as I learn a lot from them. I also broaden my thinking as far as what I can use and do with thingsHere useful link:
diablo 3 gold
guild wars 2 gold
guild wars 2 gold
diablo 3 gold
diablo 3 gold
guild wars 2 gold
tera gold
diablo 3 gold
guild wars 2 gold
guild wars 2 gold
diablo 3 gold
diablo 3 gold
guild wars 2 gold
tera gold

Unknown said...

As well asBuy rs gold the earlier hen price makes it the top seminar offer That i've ever been aware of. Daylights, including the publish first bird pace causes it to become the very best conference bargain That i've ever heard of. Therefore enroll currently. Cheapest Diablo 3 goldYou already know you actually want to.