RubyJS

180293b3d2d1977bd7dee2557f317cfd?s=47 hasclass
November 30, 2012

 RubyJS

Introducing rubyjs.
More info at www.rubyjs.org

There is a bug on slide 75. check http://rubyjs.org/blog/2012/12/when-to-use-typeof/

180293b3d2d1977bd7dee2557f317cfd?s=128

hasclass

November 30, 2012
Tweet

Transcript

  1.     Sebas'an  Burkhard     hasclass.com   github.com/hasclass?  

    twi:er.com/hasclass?  
  2. None
  3. None
  4. RubyJS  alpha   www.rubyjs.org  

  5. A  port  of  Ruby  core-­‐lib  to  JS   •  String,

     Regexp,  MatchData   •  Array,  Enumerable,  Enumerator   •  Numeric  (Integer/Fixnum,  Float)   •  Range   •  Time,  Hash  coming  soon  
  6. JS  Methods   Array jjjjjjjjjjjjj Enumerable Fixnum Float jjjj Integer

    Kernel Matchdata Numeric Range Regexp jj String jjjjjjjjjjjjj
  7. RubyJS  Methods   Array jjjjjjjjjjjjj................................. ............................ Enumerable .............................................. Fixnum ...........................

    Float jjjj.............................. Integer .................... Kernel ... Matchdata ..................... Numeric ....................... Range ...................... Regexp jj...................... String jjjjjjjjjjjjj................................ ...........................
  8. Ruby   arr = %w(looks feels acts) 
 arr.map {|w|

    w.capitalize }
 .join(", ") 
 .concat(" like Ruby")
 .center(35, '-') '---Looks, Feels, Acts like Ruby---’  
  9. RubyJS   arr = R.w('looks feels acts') 
 
  

  10. Ruby   arr = %w(looks feels acts) 
 arr.map {|w|

    w.capitalize }
  
  11. arr = R.w('looks feels acts') 
 
 arr.map((w) -> w.capitalize()

    )
  
  12. RubyJS   arr = R.w('looks feels acts') 
 
 arr.map((w)

    -> w.capitalize() )
 .join(", ") 
 .concat(" like Ruby")
 .center(35, '-')
 '---Looks, Feels, Acts like Ruby---’  
  13. Ruby   arr = %w(looks feels acts) 
 
 arr.map

    {|w| w.capitalize }
 .join(", ") 
 .concat(" like Ruby")
 .center(35, '-’) 
 '---Looks, Feels, Acts like Ruby---’  
  14. arr = R.w('looks feels acts') 
 arr.map(function (w) {
 return

    w.capitalize(); })
 .join(", ") 
 .concat(" like Ruby")
 .center(35, '-') + "!”  
  15. <script src="ruby.js"></script>
 <script>
 arr = R.w('looks feels acts');
 
 str

    = arr.map(function (w) {
 return w.capitalize(); 
 }).join(", ") 
 .concat(" like Ruby")
 .center(35, '-');
 
 alert(str);
 </script>  
  16. Example  #2   How  it  works  

  17. str = "a"
  

  18. str = R("a”) #=> R.String
  

  19. str = R("a")
 str.capitalize_bang() #=> nil or self

  20. str = R("a")
 str.capitalize_bang() 
 str.upto('C’) #=> R.Enumerator

  21. str = R("a")
 str.capitalize_bang() 
 str.upto('C').each_cons(2) #=> R.Enumerator

  22. str = R("a")
 str.capitalize_bang() 
 str.upto('C').each_cons 2, (a,b) -> 


    R.puts("#{a} to the #{b}")
 # A to the B
 # B to the C  
  23. str = R("a")
 str.capitalize_bang() 
 str.upto('C').each_cons(2).to_a() #=> R.Array

  24. str = R("a")
 str.capitalize_bang() 
 str.upto('C').each_cons(2).map(->) #=> R.Enumerator

  25. Internals  

  26. Wrapper   class RubyJS.Fixnum extends R.Object @include RubyJS.Comparable
 
 constructor:

    (@__native__) -> odd: ->
 @__native__ % 2 != 0
 
 next: ->
 new R.Fixnum(@__native__ + 1)  
  27. 100%  JavaScript   Source  code  in  CoffeeScript  

  28. Compliant  to  Ruby   Not  just  inspired  

  29. Plays  well  with  others   Use  alongside  backbone,  jquery,  …

     
  30. Pragma'c   No  rocket-­‐science  

  31. <script src="/ruby.js” />  

  32. Chainable   arr = R.w('looks feels acts') # %w[ ]

    
 arr.map( (w) -> w.capitalize() )
 .join(", ") 
 .concat(" like Ruby")
 .center(35, '-')
 '---Looks, Feels, Acts like Ruby---'  
  33. Enumerators   arr.size().times -> puts “oy!” R( 5 ).upto(10) ->

    R( 5 ).upto(10).to_a()
  34. Aliases   # equals: ==
 arr['==']('foo') arr.equals('foo’)
 
 arr.each_with_index()
 arr.eachWithIndex()

     
  35. Special  Variables   
 $~
 R['$~']  

  36. 97%  Ruby  compliance   •  Rubyspec  (1800  specs  5000  asser'ons)

      •  Ruby  Features   – Block  arguments,  duck-­‐typing   •  Keep  it  lean,  skipped  features:   – Subclassing  RubyJS  classes,  Tain'ng/trus'ng   – Some  Regexp  func'onality,  No  NilClass,  Boolean  
  37. How  does  it  compare  to   •  CoffeeScript   • 

    Underscore   •  Lodash   •  Jsclass   •  Sugarjs   •  PrototypeJS   •  Components  
  38. “Now  I  realise  your  work  is  supposed  to  finish  

    what  PrototypeJS  once  started.  Help  Ruby   developers  in  wri?ng  JS  –  but  in  a  cleaner  way.”     @nomadcoder,  creator  of  netzke.org  
  39. Benefits  for  JS  developers  

  40. One  dependency   <script src="/es5shims.js"/>
 <script src="/underscore-1.3.min.js"/>
 <script src="/stringjs-0.9.9.js"/>
 <script

    src="/momentjs-1.5.1.js"/>
 <script src="/custom_functions.js"/>  
  41. One  dependency   <script src="/es5shims.js"/>
 <script src="/underscore-1.3.min.js"/>
 <script src="/stringjs-0.9.9.js"/>
 <script

    src="/momentjs-1.5.1.js"/>
 <script src="/custom_functions.js"/> <script src="/ruby.js">    
  42. One  API   _.map([], (w) -> ) 
 _.chain(arr).(...).value()
 S("foo").capitalize().s


    moment().format('L')
  
  43. One  API   R( [1] ).map().to_native() 
 R( [2] ).map().reject().to_native()

    
 R("foo").capitalize().to_native() 
 R(new Date(…)).strftime(“%y-%m-%d”)  
  44. One  chain   arr = ['looks', 'feels', 'acts']
 str =

    _.map(arr, (w) -> S(w).capitalize().s ).join(', ’)
 str += " not like Ruby”
 S(str).pad(str, 35, '-').s  
  45. One  chain   arr = R.w('looks feels acts') 
 arr.map((w)

    -> w.capitalize() )
 .join(", ") 
 .concat(" like Ruby")
 .center(35, '-')
  
  46. One  documenta'on  

  47. One  documenta'on  

  48. One  choice   How  much  'me  have  you  spent  

    switching  libraries?       Backbone,  knockout,  emberjs,  angularjs,   underscore,  prototypejs,  lodash,  stringjs,  jstring,   sugarjs,  components.  
  49. One  choice   Dual-­‐license   •  AGPL     • 

    Commercial    
  50. One  choice   190  USD  /  Dev  

  51. 20  kbytes   minified  and  gzipped  

  52. It  scales   As  fast  as  the  rest  

  53. Some  other  features  

  54. Destruc've  Methods   str = R("foo")
 cap = str.capitalize() 


    cap # "Foo"
 str # "foo"
 
 str.capitalize_bang()
 str # "Foo"  
  55. U'lity-­‐belt  or  OO-­‐style   R("foo") # for 98% of cases

    R("foo").center(50).to_native() R("foo", -> @capitalize() ) # natives 
 str = new R.String("foo") # For speed
 String.new("foo") # Ruby behaviour 
 R.$String(1) # Ruby: String(1)
  
  56. U'lity-­‐belt  or  OO-­‐style   str = new R.String("foo")
 titleize =

    (str) ->
 str.capitalize_bang()
 str.center_bang(50) 
 str
 
 titleize(str)
 " Foo "  
  57. Typecasted  arguments   num = R(76.123)
 num.round( 1 )
 num.round(

    new Number(2) )
 num.round( R(1) )
 # duck-typing
 num.round( {to_int: -> R(1)} )  
  58. Enumerator/Enumerable   fb = R([38,29,31,20,18,21,21]) fb.each_cons(2)
 .map (x,y) -> (y

    / x - 1.0) * 100 # [-23.6, 6.8, ... ] 
 R(1).upto 5, (i) -> # ...
 R(3).times (i) -> # ...  
  59. Ruby  block  arguments   points = R( [ [1,-2] ]

    ) points.each (point) -> 
 # point: [1, -2]
 points.each ( x,y ) -> 
 # x: 1, y: -2 Ruby style
 points.each ([x,y]) -> 
 # x: 1, y: -2 CoffeeScript style  
  60. Ranges   R.rng('a', 'az').each (s) -> 
 # a, b,

    ..., aa, ab, ... az
 
 R.rng('a', 'az').to_a()
 # ['a', 'b', ... 'az']  
  61. Parlez-­‐vous  JavaScript?  

  62. Parlez-­‐vous  JavaScript?   I don’t wanna fucking parlez JavaScript

  63. Web  components  are  the  future  

  64. Web  components  are  the  future   If only I could

    use it now!
  65. A  vehicle  of  oppression  

  66. 1  year  later   Fighting opression leads to depression

  67. 2  years  later   I wish I’d cut my hair

    by now.
  68. Lessons  Learned  

  69. just  do  it   it  always  works  out,  somehow.  

  70. typeof   typeof 1 # number
 typeof true # boolean


    typeof 'str' # string
 typeof undefined # undefined
 typeof anything_else # object 
 # fell in disgrace because of: typeof new Number(1) # object
 typeof new String('f') # object  
  71. typeof ===   FAST  

  72. toString = Object.prototype.toString
 numClass = '[object Number]’ 
 isNumber =

    (obj) ->
 toString.call(obj) == numClass 
 isNumberFast = (obj) -> # Return quick if number primitive
 typeof obj == 'number' || 
 toString.call(obj) == numClass  
  73. Benchmark   isNumber(  1  )     isNumberFast(  1  )

        isNumberFast(  “foo”  )     isNumberFaster(  “foo”  )  
  74. Benchmark  

  75. isNumberFaster = (obj) ->
 typeof obj == 'number' || 


    # Return quickly if other primitive
 typeof obj != 'object' || 
 toString.call(obj) == numClass
  
  76. _ = (value) ->
 if value && value._wrapped
 return value


    # ... # Behind the scenes. value: 1 1._wrapped
 # => new Number(1)._wrapped
  
  77. _ = (value) -> # Primitives are no wrapper objects

    if (typeof value == 'object' && value._wrapped) 
 return value
 # ...
  
  78. 30min   Spend  this  on  general  Proj.  Mgmt  

  79. Track  progress   Array ..............xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx Enumerable .........xxxxxxxxxxxxxxxxxxxxxxxxxxxx Fixnum ...........................

    Float ..................................xxx Integer ..........xxxxxxxx Kernel ... Matchdata ..................... Numeric ....................... Range ...................xxx Regexp ..................xxxxxx String ................................ ....xxxxxxxxxxxxxxxxxxxxx
  80. while/for   instead  of  forEach,  map  

  81. Purity   idx = -1
 len = arr.length
 while ++idx

    < len
 callback( arr[i] )
  
  82. each   idx = -1
 while ++idx < arr.length
 callback(

    arr[i] )
  
  83. map   idx = -1
 ary = Array(arr.length) while ++idx

    < arr.length
 ary[idx] = callback( arr[i] )
   return ary  
  84. It’s  launch  'me  

  85. RubyJS.org   twi:er.com/hasclass   hasclass.com