Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

RubyJS

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/

hasclass

November 30, 2012
Tweet

More Decks by hasclass

Other Decks in Programming

Transcript

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

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

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

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

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

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

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

    w.capitalize(); })
 .join(", ") 
 .concat(" like Ruby")
 .center(35, '-') + "!”  
  8. <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>  
  9. 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  
  10. Wrapper   class RubyJS.Fixnum extends R.Object @include RubyJS.Comparable
 
 constructor:

    (@__native__) -> odd: ->
 @__native__ % 2 != 0
 
 next: ->
 new R.Fixnum(@__native__ + 1)  
  11. Chainable   arr = R.w('looks feels acts') # %w[ ]

    
 arr.map( (w) -> w.capitalize() )
 .join(", ") 
 .concat(" like Ruby")
 .center(35, '-')
 '---Looks, Feels, Acts like Ruby---'  
  12. 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  
  13. How  does  it  compare  to   •  CoffeeScript   • 

    Underscore   •  Lodash   •  Jsclass   •  Sugarjs   •  PrototypeJS   •  Components  
  14. “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  
  15. 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">    
  16. 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”)  
  17. 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  
  18. One  chain   arr = R.w('looks feels acts') 
 arr.map((w)

    -> w.capitalize() )
 .join(", ") 
 .concat(" like Ruby")
 .center(35, '-')
  
  19. One  choice   How  much  'me  have  you  spent  

    switching  libraries?       Backbone,  knockout,  emberjs,  angularjs,   underscore,  prototypejs,  lodash,  stringjs,  jstring,   sugarjs,  components.  
  20. Destruc've  Methods   str = R("foo")
 cap = str.capitalize() 


    cap # "Foo"
 str # "foo"
 
 str.capitalize_bang()
 str # "Foo"  
  21. 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)
  
  22. U'lity-­‐belt  or  OO-­‐style   str = new R.String("foo")
 titleize =

    (str) ->
 str.capitalize_bang()
 str.center_bang(50) 
 str
 
 titleize(str)
 " Foo "  
  23. 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)} )  
  24. 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) -> # ...  
  25. 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  
  26. Ranges   R.rng('a', 'az').each (s) -> 
 # a, b,

    ..., aa, ab, ... az
 
 R.rng('a', 'az').to_a()
 # ['a', 'b', ... 'az']  
  27. 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  
  28. 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  
  29. Benchmark   isNumber(  1  )     isNumberFast(  1  )

        isNumberFast(  “foo”  )     isNumberFaster(  “foo”  )  
  30. isNumberFaster = (obj) ->
 typeof obj == 'number' || 


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


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

    if (typeof value == 'object' && value._wrapped) 
 return value
 # ...
  
  33. Track  progress   Array ..............xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx Enumerable .........xxxxxxxxxxxxxxxxxxxxxxxxxxxx Fixnum ...........................

    Float ..................................xxx Integer ..........xxxxxxxx Kernel ... Matchdata ..................... Numeric ....................... Range ...................xxx Regexp ..................xxxxxx String ................................ ....xxxxxxxxxxxxxxxxxxxxx
  34. Purity   idx = -1
 len = arr.length
 while ++idx

    < len
 callback( arr[i] )
  
  35. map   idx = -1
 ary = Array(arr.length) while ++idx

    < arr.length
 ary[idx] = callback( arr[i] )
   return ary