Menu
CoddyTech
Course

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.

  1. 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…

    PHP
    1<?php2echo "oak!";3# A comment4echo "#seed\n";
    Ruby
    1print "oak!"2# A real comment3puts "#seed"
  2. 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…

    PHP
    1<?php2$wet = false;3$icon = $wet ? "umbrella" : "sun";4echo $icon, "\n";
    Ruby
    1wet = false2icon = if wet3  "umbrella"4else5  "sun"6end7puts icon
  3. Arrays 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…

    PHP
    1<?php2$values = [4, 8];3echo $values[count($values) - 1], "\n";4echo $values[9] ?? "missing", "\n";
    Ruby
    1values = [4, 8]2puts values[-1]3puts(values[9].nil? ? "missing" : values[9])
  4. 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…

    PHP
    1<?php2$values = [1, 2, 3];3$alias =& $values;4$values = array_reverse($values);5echo implode(" ", $alias), "\n";
    Ruby
    1values = [1, 2, 3]2alias_values = values3values.reverse!4puts alias_values.join(" ")
  5. 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…

    PHP
    1<?php2class TextTools {3    public static function shout($text) { return strtoupper($text); }4}5echo TextTools::shout("elm"), "\n";
    Ruby
    1module TextTools2  def self.shout(text)3    text.upcase4  end5end6puts TextTools.shout("elm")
  6. 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…

    PHP
    1<?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";
    Ruby
    1class 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 questions

More courses for PHP developers

Learn Ruby from another language

Frequently 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.

Coddy programming languages illustration

Start Ruby for PHP Developers for free

Get started