|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: "To Ruby From Javascript" |
| 4 | +lang: en |
| 5 | +--- |
| 6 | + |
| 7 | +JavaScript is a ubiquitous programming language, primarily known for web |
| 8 | +development but also used for server-side development with Node.js. Going |
| 9 | +from JavaScript to Ruby, you'll find Ruby has more structured syntax and |
| 10 | +strong object-oriented principles, but you'll also discover Ruby's focus |
| 11 | +on developer happiness and expressiveness. |
| 12 | + |
| 13 | +### Similarities |
| 14 | + |
| 15 | +As with JavaScript, in Ruby,... |
| 16 | + |
| 17 | +* There's an interactive prompt (called `irb`). |
| 18 | +* Objects are dynamically typed. |
| 19 | +* Functions are first-class objects. |
| 20 | +* There are no special line terminators (except the usual newline). |
| 21 | +* You can define functions inside other functions. |
| 22 | +* Arrays and objects (hashes in Ruby) are core data structures. |
| 23 | +* There is excellent support for functional programming with blocks, |
| 24 | + iterators, and higher-order functions. |
| 25 | +* Variables are dynamically typed—you don't declare their types. |
| 26 | +* Both support closures and can capture variables from their |
| 27 | + surrounding scope. |
| 28 | +* Regular expressions are built into the language. |
| 29 | +* Both languages are interpreted, not compiled. |
| 30 | + |
| 31 | + |
| 32 | +### Differences |
| 33 | + |
| 34 | +Unlike JavaScript, in Ruby,... |
| 35 | + |
| 36 | +* You don't need to worry about browser compatibility—Ruby runs |
| 37 | + consistently across platforms. |
| 38 | +* Everything is an object, including numbers and basic types. |
| 39 | + `5.times { puts "Hello" }` is valid Ruby. |
| 40 | +* There's no concept of `undefined`. Ruby uses `nil` instead of both |
| 41 | + `null` and `undefined`. |
| 42 | +* Functions are called methods, and you typically call them on objects. |
| 43 | +* There's `public`, `private`, and `protected` for method visibility, |
| 44 | + rather than relying on conventions or closures for privacy. |
| 45 | +* Ruby has class-based inheritance with single inheritance plus mixins, |
| 46 | + rather than JavaScript's prototype-based inheritance. |
| 47 | +* Variables have different scopes indicated by their prefix (`@instance`, |
| 48 | + `@@class`, `$global`) rather than using `var`, `let`, or `const`. |
| 49 | +* String interpolation uses `#{}` syntax: `"Hello #{name}"` instead of |
| 50 | + template literals or concatenation. |
| 51 | +* Ruby blocks with `do...end` or `{...}` are more powerful than |
| 52 | + JavaScript arrow functions and are used extensively for iteration. |
| 53 | +* Minimal punctuation: semicolons are optional and rarely used; blocks are delimited with `end` (or `do...end`) rather than `{}`. |
| 54 | +* It's `elsif` instead of `else if`. |
| 55 | +* Ruby has symbols (`:symbol`) which are immutable strings often used |
| 56 | + as identifiers. |
| 57 | +* No type coercion surprises—Ruby is more predictable about type |
| 58 | + conversions. |
| 59 | +* Ruby methods can end with `?` (for predicates) or `!` (for |
| 60 | + destructive operations). |
| 61 | +* Parentheses for method calls are usually optional. |
| 62 | +* You use `require` or `require_relative` instead of `import` or |
| 63 | + `require()`. |
| 64 | +* Classes are defined with `class...end` blocks rather than constructor |
| 65 | + functions or class expressions. |
| 66 | +* Ruby has built-in support for operator overloading. |
| 67 | +* When tested for truth, only `false` and `nil` are falsy. Everything |
| 68 | + else is truthy (including `0`, `""`, and `[]`). |
| 69 | +* Ruby has extensive metaprogramming capabilities—you can easily |
| 70 | + modify classes and objects at runtime. |
0 commit comments