Decaf is a modification of WebKit that runs Ruby in the browser. You can access the DOM, use the inspector, and do most other things that JavaScript can do. For now, it only builds on the Mac. You can download a binary or you can build it yourself.
<script type='text/ruby'>
window.onload do
introduction = document.create_element('p')
introduction.inner_text = 'Hello, world!'
document.body.append_child(introduction)
end
</script>
There are a few major differences in the DOM API for Ruby.
- Methods and attributes are specified in
underscore_case
instead ofcamelCase
. - The
window
variable is accessible from only the top-most scope. Elsewhere you can use the global$window
. - Ruby accepts
Procs
and blocks as callbacks and listeners. For example:
# Ruby with explicit Procs
$window.set_timeout(Proc.new { console.log('Hello!') }, 1000)
$window.onload = Proc.new do |event|
console.log('The window loaded.')
end
# Ruby with implicit blocks
$window.set_timeout(1000) { console.log('Hello!') }
$window.onload do |event|
console.log('The window loaded.')
end
This is a work in progress. Please report any issues you find.