Ruby for PHP Developers
Move from PHP to Ruby through 48 multi-file tasks covering flow, blocks, collections, errors, classes, modules and inheritance. Build a lending desk and port two complete source programs.
PHP and Ruby side by side
The same program in PHP and in Ruby, from the lessons of this course.
Output and Comments
PHP scripts begin with an opening tag, and echo writes without an automatic newline. Ruby source needs no opening tag; puts adds a newline and print does not. Both languages permit hash-style comments outside quoted strings. Ruby require_relative loads a…
PHP1<?php2echo "oak!";3# A comment4echo "#seed\n";Ruby1print "oak!"2# A real comment3puts "#seed"Conditional Values
PHP ternary selects a value and if is a statement. Ruby if can itself yield its selected branch value. PHP braces or alternative end-style syntax differ from Ruby end. Use elsif or else if carefully according to the language rather than transferring…
PHP1<?php2$wet = false;3$icon = $wet ? "umbrella" : "sun";4echo $icon, "\n";Ruby1wet = false2icon = if wet3 "umbrella"4else5 "sun"6end7puts iconArrays and Indexing
PHP arrays are ordered maps, not a separate dense-vector type. Negative keys are literal keys rather than offsets from the end. Ruby Arrays use integer positions and negative indices count backward. Reading a missing PHP key warns; the null-coalescing…
PHP1<?php2$values = [4, 8];3echo $values[count($values) - 1], "\n";4echo $values[9] ?? "missing", "\n";Ruby1values = [4, 8]2puts values[-1]3puts(values[9].nil? ? "missing" : values[9])Mutation and Aliases
PHP array assignment has value semantics, unlike Ruby array reference sharing. PHP explicit references using an ampersand alias a variable, and object assignment shares object identity without needing that syntax. Ruby destructive methods operate on a shared…
PHP1<?php2$values = [1, 2, 3];3$alias =& $values;4$values = array_reverse($values);5echo implode(" ", $alias), "\n";Ruby1values = [1, 2, 3]2alias_values = values3values.reverse!4puts alias_values.join(" ")Modules as Namespaces
PHP namespaces qualify declarations, and static methods can group operations on a class. Ruby modules can group constants and singleton methods, but file loading does not automatically create a namespace. Use require_relative explicitly for neighboring Ruby…
PHP1<?php2class TextTools {3 public static function shout($text) { return strtoupper($text); }4}5echo TextTools::shout("elm"), "\n";Ruby1module TextTools2 def self.shout(text)3 text.upcase4 end5end6puts TextTools.shout("elm")Composing Objects
PHP and Ruby objects can hold references to collaborators. Neither automatically deep-clones a child object when it is stored. Delegation lets a contained object provide behavior without making the outer object a subtype of it. Array value semantics in PHP…
PHP1<?php2class Lens { public function label() { return "wide"; } }3class Camera {4 private $lens;5 public function __construct($lens) { $this->lens = $lens; }6 public function label() { return $this->lens->label(); }7}8echo (new Camera(new Lens()))->label(), "\n";Ruby1class Lens2 def label; "wide"; end3end4class Camera5 def initialize(lens); @lens = lens; end6 def label; @lens.label; end7end8puts Camera.new(Lens.new).label
Syllabus
9 chapters48 lessons1 project48 challenges99 quiz questionsStart with Ruby
8 lessonsFlow and Methods
6 lessonsCollections and Blocks
6 lessonsMutation and Errors
5 lessonsObjects and State
5 lessonsComposition and Dispatch
6 lessonsObjects in Practice
6 lessonsLending Desk Project
4 lessonsPort PHP Programs
2 lessonsFrequently asked questions
Who is Ruby for PHP Developers for?
For people who already write PHP and want to learn Ruby. Each lesson starts from PHP you know and shows how Ruby does the same thing, so you skip the beginner basics.
Do I need to know Ruby already?
No. The course starts with your first Ruby program. You need PHP basics: variables, conditions, loops, functions and collections.
How many lessons does Ruby for PHP Developers have?
Ruby for PHP Developers contains 48 interactive lessons you can complete at your own pace.
What does Ruby for PHP Developers cover?
The course is organized into chapters covering: Start with Ruby, Flow and Methods, Collections and Blocks, Mutation and Errors, and more.
Is Ruby for PHP Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.