When you first look at some Ruby code, it will likely remind you of
other programming languages you’ve used. This is on purpose. Much of the
syntax is familiar to users of Perl, Python, and Java (among other
languages), so if you’ve used those, learning Ruby will be a piece of
cake.
{: .summary}

This document contains two major sections. The first attempts to be a
rapid-fire summary of what you can expect to see when going from
language *X* to Ruby. The second section tackles the major language
features and how they might compare to what you’re already familiar
with.

## What to Expect: *Language X* to Ruby

* [To Ruby From C and C++](to-ruby-from-c-and-cpp/)
* [To Ruby From Java](to-ruby-from-java/)
* [To Ruby From Perl](to-ruby-from-perl/)
* [To Ruby From PHP](to-ruby-from-php/)
* [To Ruby From Python](to-ruby-from-python/)

## Important Language Features And Some Gotchas

Here are some pointers and hints on major Ruby features you’ll see while
learning Ruby.

### Iteration

Two Ruby features that are a bit unlike what you may have seen before,
and which take some getting used to, are “blocks” and iterators. Instead
of looping over an index (like with C, C++, or pre-1.5 Java), or looping
over a list (like Perl’s `for (@a) {...}`, or Python’s
`for i in aList: ...`), with Ruby you’ll very often instead see

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">some_list</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">this_item</span><span class="o">|</span>
  <span class="c1"># We're inside the block.</span>
  <span class="c1"># deal with this_item.</span>
<span class="k">end</span></code></pre></figure>

For more info on `each` (and its friends `collect`, `find`, `inject`,
`sort`, etc.), see `ri Enumerable` (and then `ri Enumerable#some_method`).

### Everything has a value

There’s no difference between an expression and a statement. Everything
has a value, even if that value is `nil`. This is possible:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="n">x</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">y</span> <span class="o">=</span> <span class="mi">11</span>
<span class="n">z</span> <span class="o">=</span> <span class="k">if</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="n">y</span>
      <span class="kp">true</span>
    <span class="k">else</span>
      <span class="kp">false</span>
    <span class="k">end</span>
<span class="n">z</span> <span class="c1"># =&gt; true</span></code></pre></figure>

### Symbols are not lightweight Strings

Many Ruby newbies struggle with understanding what Symbols are, and what
they can be used for.

Symbols can best be described as identities. A symbol is all about
**who** it is, not **what** it is. Fire up `irb` and see the difference:

<figure class="highlight"><pre><code class="language-irb" data-lang="irb"><span class="gp">irb(main):001:0&gt;</span><span class="w"> </span><span class="ss">:george</span><span class="p">.</span><span class="nf">object_id</span> <span class="o">==</span> <span class="ss">:george</span><span class="p">.</span><span class="nf">object_id</span>
<span class="p">=&gt;</span> <span class="kp">true</span>
<span class="gp">irb(main):002:0&gt;</span><span class="w"> </span><span class="s2">"george"</span><span class="p">.</span><span class="nf">object_id</span> <span class="o">==</span> <span class="s2">"george"</span><span class="p">.</span><span class="nf">object_id</span>
<span class="p">=&gt;</span> <span class="kp">false</span>
<span class="gp">irb(main):003:0&gt;</span></code></pre></figure>

The `object_id` methods returns the identity of an Object. If two
objects have the same `object_id`, they are the same (point to the same
Object in memory).

As you can see, once you have used a Symbol once, any Symbol with the
same characters references the same Object in memory. For any given two
Symbols that represent the same characters, the `object_id`s match.

Now take a look at the String (“george”). The `object_id`s don’t match.
That means they’re referencing two different objects in memory. Whenever
you use a new String, Ruby allocates memory for it.

If you’re in doubt whether to use a Symbol or a String, consider what’s
more important: the identity of an object (i.e. a Hash key), or the
contents (in the example above, “george”).

### Everything is an Object

“Everything is an object” isn’t just hyperbole. Even classes and
integers are objects, and you can do the same things with them as with
any other object:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># This is the same as</span>
<span class="c1"># class MyClass</span>
<span class="c1">#   attr_accessor :instance_var</span>
<span class="c1"># end</span>
<span class="no">MyClass</span> <span class="o">=</span> <span class="no">Class</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span>
  <span class="nb">attr_accessor</span> <span class="ss">:instance_var</span>
<span class="k">end</span></code></pre></figure>

### Variable Constants

Constants are not really constant. If you modify an already initialized
constant, it will trigger a warning, but not halt your program. That
isn’t to say you **should** redefine constants, though.

### Naming conventions

Ruby enforces some naming conventions. If an identifier starts with a
capital letter, it is a constant. If it starts with a dollar sign (`$`),
it is a global variable. If it starts with `@`, it is an instance
variable. If it starts with `@@`, it is a class variable.

Method names, however, are allowed to start with capital letters. This
can lead to confusion, as the example below shows:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="no">Constant</span> <span class="o">=</span> <span class="mi">10</span>
<span class="k">def</span> <span class="nf">Constant</span>
  <span class="mi">11</span>
<span class="k">end</span></code></pre></figure>

Now `Constant` is 10, but `Constant()` is 11.

### Keyword arguments

Like in Python, since Ruby 2.0 methods can be defined
using keyword arguments:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="k">def</span> <span class="nf">deliver</span><span class="p">(</span><span class="ss">from: </span><span class="s2">"A"</span><span class="p">,</span> <span class="ss">to: </span><span class="kp">nil</span><span class="p">,</span> <span class="ss">via: </span><span class="s2">"mail"</span><span class="p">)</span>
  <span class="s2">"Sending from </span><span class="si">#{</span><span class="n">from</span><span class="si">}</span><span class="s2"> to </span><span class="si">#{</span><span class="n">to</span><span class="si">}</span><span class="s2"> via </span><span class="si">#{</span><span class="n">via</span><span class="si">}</span><span class="s2">."</span>
<span class="k">end</span>

<span class="n">deliver</span><span class="p">(</span><span class="ss">to: </span><span class="s2">"B"</span><span class="p">)</span>
<span class="c1"># =&gt; "Sending from A to B via mail."</span>
<span class="n">deliver</span><span class="p">(</span><span class="ss">via: </span><span class="s2">"Pony Express"</span><span class="p">,</span> <span class="ss">from: </span><span class="s2">"B"</span><span class="p">,</span> <span class="ss">to: </span><span class="s2">"A"</span><span class="p">)</span>
<span class="c1"># =&gt; "Sending from B to A via Pony Express."</span></code></pre></figure>

### The universal truth

In Ruby, everything except `nil` and `false` is considered true. In
C, Python and many other languages, 0 and possibly other values, such as
empty lists, are considered false. Take a look at the following Python
code (the example applies to other languages, too):

<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="c1"># in Python
</span><span class="k">if</span> <span class="mi">0</span><span class="p">:</span>
  <span class="nf">print</span><span class="p">(</span><span class="sh">"</span><span class="s">0 is true</span><span class="sh">"</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
  <span class="nf">print</span><span class="p">(</span><span class="sh">"</span><span class="s">0 is false</span><span class="sh">"</span><span class="p">)</span></code></pre></figure>

This will print “0 is false”. The equivalent Ruby:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># in Ruby</span>
<span class="k">if</span> <span class="mi">0</span>
  <span class="nb">puts</span> <span class="s2">"0 is true"</span>
<span class="k">else</span>
  <span class="nb">puts</span> <span class="s2">"0 is false"</span>
<span class="k">end</span></code></pre></figure>

Prints “0 is true”.

### Access modifiers apply until the end of scope

In the following Ruby code,

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="k">class</span> <span class="nc">MyClass</span>
  <span class="kp">private</span>
  <span class="k">def</span> <span class="nf">a_method</span><span class="p">;</span> <span class="kp">true</span><span class="p">;</span> <span class="k">end</span>
  <span class="k">def</span> <span class="nf">another_method</span><span class="p">;</span> <span class="kp">false</span><span class="p">;</span> <span class="k">end</span>
<span class="k">end</span></code></pre></figure>

You might expect `another_method` to be public. Not so. The `private`
access modifier continues until the end of the scope, or until another
access modifier pops up, whichever comes first. By default, methods are
public:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="k">class</span> <span class="nc">MyClass</span>
  <span class="c1"># Now a_method is public</span>
  <span class="k">def</span> <span class="nf">a_method</span><span class="p">;</span> <span class="kp">true</span><span class="p">;</span> <span class="k">end</span>

  <span class="kp">private</span>

  <span class="c1"># another_method is private</span>
  <span class="k">def</span> <span class="nf">another_method</span><span class="p">;</span> <span class="kp">false</span><span class="p">;</span> <span class="k">end</span>
<span class="k">end</span></code></pre></figure>

`public`, `private` and `protected` are really methods, so they can take
parameters. If you pass a Symbol to one of them, that method’s visibility is
altered.

### Method access

In Java, `public` means a method is accessible by anyone. `protected`
means the class’s instances, instances of descendant classes, and
instances of classes in the same package can access it, but not anyone
else, and `private` means nobody besides the class’s instances can
access the method.

Ruby differs slightly. `public` is, naturally, public. `private` means
the method(s) are accessible only when they can be called without an
explicit receiver. Only `self` is allowed to be the receiver of a
private method call.

`protected` is the one to be on the lookout for. A protected method can be
called from a class or descendant class instances, but also with another
instance as its receiver.
Here is an example (adapted from [The Ruby Language FAQ][faq]):

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="k">class</span> <span class="nc">Test</span>
  <span class="c1"># public by default</span>
  <span class="k">def</span> <span class="nf">identifier</span>
    <span class="mi">99</span>
  <span class="k">end</span>

  <span class="k">def</span> <span class="nf">==</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
    <span class="n">identifier</span> <span class="o">==</span> <span class="n">other</span><span class="p">.</span><span class="nf">identifier</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="n">t1</span> <span class="o">=</span> <span class="no">Test</span><span class="p">.</span><span class="nf">new</span>  <span class="c1"># =&gt; #&lt;Test:0x34ab50&gt;</span>
<span class="n">t2</span> <span class="o">=</span> <span class="no">Test</span><span class="p">.</span><span class="nf">new</span>  <span class="c1"># =&gt; #&lt;Test:0x342784&gt;</span>
<span class="n">t1</span> <span class="o">==</span> <span class="n">t2</span>       <span class="c1"># =&gt; true</span>

<span class="c1"># now make `identifier' protected; it still works</span>
<span class="c1"># because protected allows `other' as receiver</span>

<span class="k">class</span> <span class="nc">Test</span>
  <span class="kp">protected</span> <span class="ss">:identifier</span>
<span class="k">end</span>

<span class="n">t1</span> <span class="o">==</span> <span class="n">t2</span>  <span class="c1"># =&gt; true</span>

<span class="c1"># now make `identifier' private</span>

<span class="k">class</span> <span class="nc">Test</span>
  <span class="kp">private</span> <span class="ss">:identifier</span>
<span class="k">end</span>

<span class="n">t1</span> <span class="o">==</span> <span class="n">t2</span>
<span class="c1"># NoMethodError: private method `identifier' called for #&lt;Test:0x342784&gt;</span></code></pre></figure>

### Classes are open

Ruby classes are open. You can open them up, add to them, and change them at
any time. Even core classes, like `Integer` or even `Object`, the parent of all
objects. Ruby on Rails defines a bunch of methods for dealing with time on
`Integer`. Watch:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="k">class</span> <span class="nc">Integer</span>
  <span class="k">def</span> <span class="nf">hours</span>
    <span class="nb">self</span> <span class="o">*</span> <span class="mi">3600</span> <span class="c1"># number of seconds in an hour</span>
  <span class="k">end</span>
  <span class="k">alias</span> <span class="n">hour</span> <span class="n">hours</span>
<span class="k">end</span>

<span class="c1"># 14 hours from 00:00 January 1st</span>
<span class="c1"># (aka when you finally wake up ;)</span>
<span class="no">Time</span><span class="p">.</span><span class="nf">mktime</span><span class="p">(</span><span class="mi">2006</span><span class="p">,</span> <span class="mo">01</span><span class="p">,</span> <span class="mo">01</span><span class="p">)</span> <span class="o">+</span> <span class="mi">14</span><span class="p">.</span><span class="nf">hours</span> <span class="c1"># =&gt; Sun Jan 01 14:00:00</span></code></pre></figure>

### Funny method names

In Ruby, methods are allowed to end with question marks or exclamation marks.
By convention, methods that answer questions end in question marks
(e.g. `Array#empty?`, which returns `true` if the receiver is empty).
Potentially “dangerous” methods by convention end with exclamation marks
(e.g. methods that modify `self` or the arguments, `exit!`, etc.).
Not all methods that change their arguments end with exclamation marks, though.
`Array#replace` replaces the contents of an array with the contents
of another array. It doesn’t make much sense to have a method like that
that **doesn’t** modify self.

### Singleton methods

Singleton methods are per-object methods. They are only available on the
Object you defined it on.

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="k">class</span> <span class="nc">Car</span>
  <span class="k">def</span> <span class="nf">inspect</span>
    <span class="s2">"Cheap car"</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="n">porsche</span> <span class="o">=</span> <span class="no">Car</span><span class="p">.</span><span class="nf">new</span>
<span class="n">porsche</span><span class="p">.</span><span class="nf">inspect</span> <span class="c1"># =&gt; Cheap car</span>
<span class="k">def</span> <span class="nc">porsche</span><span class="o">.</span><span class="nf">inspect</span>
  <span class="s2">"Expensive car"</span>
<span class="k">end</span>

<span class="n">porsche</span><span class="p">.</span><span class="nf">inspect</span> <span class="c1"># =&gt; Expensive car</span>

<span class="c1"># Other objects are not affected</span>
<span class="n">other_car</span> <span class="o">=</span> <span class="no">Car</span><span class="p">.</span><span class="nf">new</span>
<span class="n">other_car</span><span class="p">.</span><span class="nf">inspect</span> <span class="c1"># =&gt; Cheap car</span></code></pre></figure>

### Missing methods

Ruby doesn’t give up if it can’t find a method that responds to a
particular message. It calls the `method_missing` method with the name
of the method it couldn’t find and the arguments. By default,
`method_missing` raises a NameError exception, but you can redefine it to
better fit your application, and many libraries do. Here is an example:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># id is the name of the method called, the * syntax collects</span>
<span class="c1"># all the arguments in an array named 'arguments'</span>
<span class="k">def</span> <span class="nf">method_missing</span><span class="p">(</span><span class="nb">id</span><span class="p">,</span> <span class="o">*</span><span class="n">arguments</span><span class="p">)</span>
  <span class="nb">puts</span> <span class="s2">"Method </span><span class="si">#{</span><span class="nb">id</span><span class="si">}</span><span class="s2"> was called, but not found. It has "</span> <span class="o">+</span>
       <span class="s2">"these arguments: </span><span class="si">#{</span><span class="n">arguments</span><span class="p">.</span><span class="nf">join</span><span class="p">(</span><span class="s2">", "</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>

<span class="n">__</span> <span class="ss">:a</span><span class="p">,</span> <span class="ss">:b</span><span class="p">,</span> <span class="mi">10</span>
<span class="c1"># =&gt; Method __ was called, but not found. It has these</span>
<span class="c1"># arguments: a, b, 10</span></code></pre></figure>

The code above just prints the details of the call, but you are free to
handle the message in any way that is appropriate.

### Message passing, not function calls

A method call is really a **message** to another object:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># This</span>
<span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span>
<span class="c1"># Is the same as this ...</span>
<span class="mi">1</span><span class="p">.</span><span class="nf">+</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="c1"># Which is the same as this:</span>
<span class="mi">1</span><span class="p">.</span><span class="nf">send</span> <span class="s2">"+"</span><span class="p">,</span> <span class="mi">2</span></code></pre></figure>

### Blocks are Objects, they just don’t know it yet

Blocks (closures, really) are heavily used by the standard library. To
call a block, you can either use `yield`, or make it a `Proc` by
appending a special argument to the argument list, like so:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="k">def</span> <span class="nf">block</span><span class="p">(</span><span class="o">&amp;</span><span class="n">the_block</span><span class="p">)</span>
  <span class="c1"># Inside here, the_block is the block passed to the method</span>
  <span class="n">the_block</span> <span class="c1"># return the block</span>
<span class="k">end</span>
<span class="n">adder</span> <span class="o">=</span> <span class="n">block</span> <span class="p">{</span> <span class="o">|</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">|</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="p">}</span>
<span class="c1"># adder is now a Proc object</span>
<span class="n">adder</span><span class="p">.</span><span class="nf">class</span> <span class="c1"># =&gt; Proc</span></code></pre></figure>

You can create blocks outside of method calls, too, by calling `Proc.new`
with a block or calling the `lambda` method.

Similarly, methods are also Objects in the making:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="nb">method</span><span class="p">(</span><span class="ss">:puts</span><span class="p">).</span><span class="nf">call</span> <span class="s2">"puts is an object!"</span>
<span class="c1"># =&gt; puts is an object!</span></code></pre></figure>

### Operators are syntactic sugar

Most operators in Ruby are just syntactic sugar (with some precedence
rules) for method calls. You can, for example, override Integer’s `+`
method:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="k">class</span> <span class="nc">Integer</span>
  <span class="c1"># You can, but please don't do this</span>
  <span class="k">def</span> <span class="nf">+</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
    <span class="nb">self</span> <span class="o">-</span> <span class="n">other</span>
  <span class="k">end</span>
<span class="k">end</span></code></pre></figure>

You don’t need C++’s `operator+`, etc.

You can even have array-style access if you define the `[]` and `[]=` methods.
To define the unary + and - (think +1 and -2), you must define the `+@` and
`-@` methods, respectively. The operators below are **not** syntactic sugar,
though. They are not methods, and cannot be redefined:

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="o">=</span><span class="p">,</span> <span class="o">..</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="ow">not</span><span class="p">,</span> <span class="o">&amp;&amp;</span><span class="p">,</span> <span class="ow">and</span><span class="p">,</span> <span class="o">||</span><span class="p">,</span> <span class="ow">or</span><span class="p">,</span> <span class="o">::</span></code></pre></figure>

In addition, `+=`, `*=` etc. are just abbreviations for `var = var + other_var`,
`var = var * other_var`, etc. and therefore cannot be redefined.

## Finding Out More

When you are ready for more Ruby knowledge, see our
[Documentation](/en/documentation/) section.



[faq]: http://ruby-doc.org/docs/ruby-doc-bundle/FAQ/FAQ.html
