my-preamble
OPAL - CHAPTER 4: A
NEW HOPE
FORREST CHANG
FKCHANG2000@YAHOO.COM
WHO CODES RUBY?
WHO WORKS WITH THE WEB?
WHO LIKES JAVASCRIPT
WHO LIKES RUBY BETTER?
from
http://dark--romance.deviantart.com/art/Screamy-and-
Bee-love-Ruby-146852940
YOU ARE MY AUDIENCE
THE PITCH
OPAL WILL MAKE YOU HAPPY
STEP BACK
(from:
)
http://www.celiajohnsononline.com/knee-exercises-
osteoarthritis-joint-pain/
WHAT IS OPAL?
Ruby to JS Source to Source, similar to Coffescript
Runtime (supports method missing, operator overloading,
etc)
TOTALLY AWESOME
AS CODE
class Opal < Ruby
include Browser::Access
include Javascript::Interaction
runs_in :browser
runs_in :node
end
WHAT'S CURRENTLY UNDER
GITHUB.COM/OPAL
PAGE 2
THE RUBY WAY
Matz wrote in an introductory article to Ruby
"For me the purpose of life is partly to have joy. Programmers
often feel joy when they can concentrate on the creative side
of programming, So Ruby is designed to make programmers
happy."
OPAL IS THE RUBY WAY
(from
)
http://www.get-covers.com/do-more-of-what-makes-you-
happy/
OPAL IS JDD FOR ME
Joy Driven Development
no need for it, but I can't stop coding
Forrest just loves to write opal (Test Infected parody)
WHAT IF YOU CODE RUBY IN THE BROWSER?
TOP 7 REASONS WHY OPAL WILL MAKE YOU
HAPPY
1. It is a viable Ruby
2. Greenspun's 10th Rule, Opal Edition
3. Solves things the Ruby way
4. Tools you already know and love
5. Easy integration with Ruby
6. Better tools (In Browser)
7. A new future
#1 VIABLE - NOT A TOY
In production - 5 + apps, 2 more coming soon
Real Ruby1.9.3 - compliant (mostly)
passes 1377 rubyspec examples
runs asciidoc gem unmodified
runs mspec gem unmodified
can compile itself
Capable of compiling complex code
Is written in Ruby, easy to contribute
RUBY LANGUAGE FEATURES IN OPAL
classes
modules & mixins
singleton methods
method_missing
arity checks
lambda
blocks
yield
CONSTANTS
global variables
MORE FEATURES
class variables
instance variable
Enumerable
BasicObject
Kernel
NilClass
Range
Hash
String Interpolation
Heredocs
%w splits
DON'T UNDERESTIMATE THE POWER OF A
BETTER LANGUAGE
opal-irb, opal-inspector, easy to write
leads us to Greenspun's 10th rule
GREENSPUN'S 10TH RULE
Any sufficiently complicated C or Fortran program contains
an ad hoc, informally-specified, bug-ridden, slow
implementation of half of Common Lisp.
#2 GREENSPUN'S 10TH RULE, OPAL EDITION
Any sufficiently complicated Javascript program contains an
ad hoc, informally-specified, bug-ridden, slow implementation
of half of Ruby
JAVASCRIPT EFFORTS THAT IMITATE RUBY OR
RAILS
PROTOTYPE
UNDERSCORE
HTTP://RUBYJS.ORG
STRUKT
STRINGJS
HTTP://JSCLASS.JCOGLAN.COM
HTTP://CODE.GOOGLE.COM/P/JOOSE-JS/
HTTP://BLOG.JAVASCRIPTMVC.COM/?P=61
HTTPS://GITHUB.COM/TOBEYTAILOR/DEF.JS
HTTP://YEHUDAKATZ.COM/2012/01/10/JAVASC
NEEDS-BLOCKS/
HTTP://WWW.CODEPROJECT.COM/ARTICLES/314
IN-HUMAX-V0-3-EXTENDING-YOUR-JAVASCRIP
HTTP://WWW.SCATTERPOLL.COM/#TITLE-
SLIDE
ACTIVEJS
REDSCRIPT
FORML
OTHER ATTEMPTS TO DO RUBY ON JS
(from:
)
https://github.com/jashkenas/coffee-script/wiki/List-of-
languages-that-compile-to-JS
WHY NOT THE REAL THING?
OPAL IS RUBY
#3 SOLVING THINGS THE RUBY WAY
packaging/modules
javascript integration
testing
PACKAGING AND MODULE
JAVASCRIPT MODULES
long article
AMD
CommonJS
Harmony
http://addyosmani.com/writing-modular-js/
MODULES
Do This
define(
module_id /*optional*/,
[dependencies] /*optional*/,
definition function /*function for instantiating the module or objec
t*/
);
// Consider 'foo' and 'bar' are two external modules
// In this example, the 'exports' from the two modules loaded are passed
as
// function arguments to the callback (foo and bar)
// so that they can similarly be accessed
And This
require(['foo', 'bar'], function ( foo, bar ) {
// rest of your code here
foo.doSomething();
});
RUBY
require
uses gems
require 'file_or_gem_you_want'
OPAL
require
uses gems
opal-sprockets handles requires
also include templates
require 'file_gem_template_you_want'
FOR NON RAILS STANDALONE - RAKEFILE
require 'bundler/setup'
# require 'opal/rake_task'
require 'opal'
require 'opal-sprockets'
desc "build jqconsole based irb"
task :build_jqconsole do
File.open("js/app-jqconsole.js", "w+") do |out|
env = Opal::Environment.new
env.append_path "examples"
env.append_path "opal"
out << env["app-jqconsole"].to_s
end
# system "terminal-notifier -title 'opal-irb build' -message 'js file
built'"
system "open -a 'Google Chrome' index-jq.html"
end
FOR NON RAILS STANDALONE APP-JQCONSOLE.RB
require 'opal'
require 'opal-jquery'
require 'opal-parser'
require 'opal_irb_jqconsole'
Document.ready? do
OpalIrbJqconsole.create("#console")
end
JAVASCRIPT INTEGRATION
x strings (backticks %x)
method missing
window = `window`
window.location
TESTING
opal-spec
rake/phantomjs
in browser
OPAL-SPEC
#4 THE TOOLS YOU KNOW AND LOVE
language - Ruby
editor - emacs, vim, sublime, Rubymine, etc.
Rake
rspec (sorry DHH)
guard
<fill in the ruby tool> - remember it's just Ruby
#5 EASY INTEGRATION WITH RUBY
opal-rails
opal-sprockets (already shown in modules)
it's Ruby
OPAL RAILS
https://github.com/opal/opal-rails
#6 NEW IN BROWSER TOOLS
opal-irb
opal-inspector
OPAL-IRB
irb for opal
history
readline controls
multiline edit (over all of history)
send a link - live gist
OPAL-INSPECTOR
inspired by amber.js in browser Smalltalk like code browser
change the paradigm of development
develop in browser w/better tools
persistent live Opal objects is similar to Smalltalk image
DEMOS
opal-irb
opal-inspector
Source Maps (head has broken it somewhat though)
QUICK LOOK AT CODE
only 440 lines for opal-inspector current state
very short, small classes
didn't need a framework
#7 A NEW FUTURE (HOPE)
in browser dev, goodness from Smalltalk, Ruby and Lisp
Machines
meteor.js like same code on both sides
blurring front and back
1 language…
THERE CAN ONLY BE ONE
May that language be Ruby/Opal
CALL TO ARMS
things are good
they can be better
need your help
HELP
try it out
talk, google group, irc
document
contribute, it's just Ruby
have fun
KISS JAVASCRIPT GOODBYE
Created by fkchang.

Opal chapter 4_a_new_hope