| Ruby | CoffeeScript |
|---|---|
| "foo #{bar}" | "foo #{bar}" |
| x ||= 2 | x ?= 2 |
| x ? "true" : "false" | if x then "true" else "false" |
| def fct(*x) | (x...) -> |
| *[1, 2, 3] | [1, 2, 3]... |
| def foo; return 5; end | foo = -> return 5 |
| def foo; 5; end | foo = -> 5 |
| hash.each{|key, value| } | for key, value of hash |
| array.each{|value| } | for value in array |
| a = array.map{|i| i + 1} | a = (i + 1 for i in array) |
| a = array.select{|i| i > 1} | a = for i in array when i > 1 |
| if array.include?(value) | if value in array |
| array[1..3], array[1..-1] | array[1..3], array[1..-1] |
| string[1..3], string[1..-1] | string[1..3], string[1..-1] |
| array.dup | array[0..] |
| [a,b] = [b,a] | [a,b] = [b,a] |
| a,b,c = 1,2,3 | [a,b,c] = [1,2,3] |
| class Foo | class Foo |
| class Foo < Bar | class Foo extends Bar |
| def child_fct; super; end | child_fct: -> super() |
| def child_fct(a,c,b); super(a,b,c); end | child_fct(a,c,b): -> super(a,b,c) |
| Code | Explanation |
|---|---|
| a = (@x) -> | a = (x) -> @x = x |
| for i in int_list when i > 3 | when condition need to be true to execute the loop |
| for i in int_list by 3 | won't execute the loop for 2 value on 3 |