An Introduction To Opal
Ruby code is compiled and run live, and the JavaScript is verbose!
As such, they are best viewed in a browser
So if you are reading a static slide, go here:
Ruby to JavaScript Transpiler
AKA a "source to source" compiler
puts "Hello, World"
JavaScript bridge
Browser APIs, NodeJS Support
puts %x{document.title}
%x{
console.log(#{ RUBY_ENGINE_VERSION })
}
JavaScript bridge
require 'native'
win = Native(`window`)
puts win.location.href
# win.alert('+1 for native bridging!')
"a gemstone consisting of hydrated silica, typically semitransparent and showing varying colors against a pale or dark background"
New Oxford American Dictionary
National Gemstone of Australia
def fib(n)
n <= 1 ? 1 : fib(n - 1) + fib(n - 2)
end
puts (1..10).
map {|n| fib n }.
map(&:to_s).
join(", ")
def hamming(a,b)
a.split("").
zip(b.split("")).
select {|(a,b)| a != b }.
length
end
puts hamming("rubyist", "opalist")
puts hamming("happy", "yappy")
puts hamming("goose", "geese")
class Greeter
def initialize(name = "Opal")
@name = name
end
def say_hello
puts greeting
end
def greeting
"Hello, #{@name}!"
end
end
class LoudGreeter < Greeter
def greeting
super.upcase
end
end
greeter = Greeter.new
greeter.say_hello
class Array
def all?
each {|n| return false unless n }
end
end
module Validatable
def self.included(base)
base.extend(ClassMethods)
end
def valid?
self.class.validations.
map {|attribute, block|
block.call(self.send(attribute))
}.all?
end
module ClassMethods
def validate(attribute, &block)
validations[attribute] = block
end
def validations
@validations ||= {}
end
end
end
class Person
include Validatable
attr_accessor :name
validate(:name) {|value|
!value.to_s.empty?
}
end
brad = Person.new
puts brad.valid?
brad.name = "Bradley"
puts brad.valid?
Version | Date | Examples |
---|---|---|
0.5.5 | 2013-11 | 2,715* |
0.6.3 | 2014-11 | 3,070 |
0.7.0.beta1 | 2014-10 | 3,445 |
0.7.0.beta2 | 2014-11 | 3,601 |
0.7.0.beta3 | 2014-11 | 3,603 |
* not all passing (for me!)
For context, ~20k rubyspecs total
#<<
or #gsub!
RSpec via opal-rspec
Adds async support:
async 'HTTP requests should work' do
HTTP.get('/users/1.json') do |res|
run_async {
expect(res).to be_ok
}
end
end
Browser API wrapper:
DOM, CSS, AJAX, WebSockets, SSE, History, Storage, SQL
$document.ready do
DOM {
div.info {
span.red "I'm all cooked up."
}
}.append_to($document.body)
end
"toll-free" bridge to JQuery
foos = Element.find('.foo')
# => [<div class="foo">, ...]
foos.class
# => JQuery
foos.on(:click) do
alert "element was clicked"
end
support for erb and haml
require 'template'
template = Template['user']
context = User.new('Ford Prefect')
puts template.render(context)
# => "<div>...</div>"
Template.paths
# => [#<Template: 'views/user'>, #<Template: 'login'>]
require 'promise'
first = get_json '/users/1.json'
second = get_json '/users/2.json'
Promise.when(first, second).then do |user1, user2|
puts "got users: #{user1}, #{user2}"
end.fail do
alert "Something bad happened"
end
github/bspaulding
twitter/bradspaulding