Lars Tesmer's Blog

From Journeyman to Master - A Software Craftsman's Blog

Learning Ruby: Gotchas and Pitfalls for PHP Programmers

I’m currently learning Ruby. In this post I’ll list some pitfalls for programmers coming from PHP that would probably cause some confusion if you aren’t aware of them.
This list is by no means complete, while I learn Ruby I’ll very probably encounter more gotchas, which I will blog about, too.

Anyways, here we go, the incomplete list of things Ruby does differently than PHP:

1. Arrays are continuous

As a PHP programmer, you’re used to, for example, put User-objects into an array with the index being the user id:

1
2
3
<?php
$users = array();
$users[$user_id] = $user;

Now you have an array with one element containing an User. Let’s do the same in Ruby:

Don’t do this!
1
2
users = []
users[user_id] = user

Do not do it like that in Ruby!
If user_id is 9999, the above code will create an array of 10.000 elements, the last of them containing the User, all other elements containing nil!
Imagine having user ids in the range of millions…
Instead you must use hashes, like so:

Do this instead
1
2
users = {} # Note the curly braces
users[user_id] = user

This will create a hash containing only one element, the User.

2. Zero is not falsy

In Ruby only nil and false are false, everything else evaluates to true!

This will not output anything
1
2
users = {}
print "No users!" if !users.length // This will never be printed

This is of course a good thing but in PHP this is something you’re probably used to do frequently - but it won’t work in Ruby!

3. The keywords private and protected

In Ruby any methods which are defined as private are not visible from outside the class but they are still visible to subclasses of the class containing the private method!
However, a private method is not visible to instances of the same class.

Methods that are protected have the same visibility as private methods - except that a protected method is visible to instances of the same class.

So by using private and protected you do not influence the visibility of methods for subclasses but their visibility to instances of the same class. In PHP “private” means “private to this class” while in Ruby it means “private to this instance”.

Update
Here’s an example illustrating showing how private and protected change the visibility of methods from within instances of the same class:

Example: protected vs. private
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class SomeClass
def call_private_method(instance_of_some_class)
instance_of_some_class.a_private_method
end
def call_protected_method(instance_of_some_class)
instance_of_some_class.a_protected_method
end
protected
def a_protected_method()
print "I'm a protected method!\n"
end
private
def a_private_method()
print "I'm a private method!"
end
end
some_class_1 = SomeClass.new
some_class_2 = SomeClass.new # New instance of same class!
# The following call will work
some_class_1.call_protected_method(some_class_2)
# This will throw an error!
some_class_1.call_private_method(some_class_2)
# And the following two calls will throw an error, too.
# Neither private nor public methods are visible from outside.
some_class_2.a_protected_method
some_class_2.a_private_method

Oh, and in PHP both private and protected methods can always be accessed from within instances of the same class, there’s no way to prevent that.

4. There’s no static keyword

If you’re looking for static in Ruby, well, stop looking.
This keyword doesn’t exist.

However, what exists instead, is the concept of class methods. Class methods are similar to static methods, in that they can be called without having to instantiate the class.

Explaining what class methods are, is out of the scope of this blog post but here are two blog posts about them from other authors:

Class and Instance Methods in Ruby
Advantages of class methods over static methods

Conclusion

I hope this list of differences between Ruby and PHP will help in your mission to learn Ruby and prevent you from shooting yourself in the foot.
I will almost certainly find more gotchas, thus I’d suggest you subscribe to my blog so you won’t miss them.

Anyways, have fun learning Ruby - I sure have! ;)

Related posts:
Review: Eloquent Ruby

Comments