Ruby for Java Developers
Move from Java 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.
Java and Ruby side by side
The same program in Java and in Ruby, from the lessons of this course.
Output and Comments
Java starts execution in a main method and uses braces to delimit bodies. Ruby executes top-level statements in the entry file and closes many constructs with end. Ruby require_relative loads neighboring source files at runtime; Java resolves compiled classes…
Java1import java.util.*;2public class Main {3 4 public static void main(String[] args) {5 System.out.print("oak!");6 // A comment7 System.out.println("#seed");8 }9}Ruby1print "oak!"2# A real comment3puts "#seed"Conditional Values
Java if is a statement, while a ternary expression can select a value. Ruby if itself yields the selected branch's final expression. Java enforces type rules on the conditional result, whereas Ruby determines behavior dynamically when the value is used. Only…
Java1import java.util.*;2public class Main {3 4 public static void main(String[] args) {5 boolean wet = false;6 String icon = wet ? "umbrella" : "sun";7 System.out.println(icon);8 }9}Ruby1wet = false2icon = if wet3 "umbrella"4else5 "sun"6end7puts iconArrays and Indexing
Java arrays have fixed length and checked zero-based indices; ArrayList provides dynamic size with checked access. Ruby arrays are growable and ordinary out-of-bounds reads return nil. Ruby negative indices count from the end, unlike Java indices. Use Ruby…
Java1import java.util.*;2public class Main {3 4 public static void main(String[] args) {5 int[] values = {4, 8};6 System.out.println(values[values.length - 1]);7 System.out.println(values.length > 9 ? Integer.toString(values[9]) : "missing");8 }9}Ruby1values = [4, 8]2puts values[-1]3puts(values[9].nil? ? "missing" : values[9])Mutation and Aliases
Java reference assignment and Ruby object assignment share referenced mutable objects. Java final prevents reference rebinding, not mutation of the object. Collections.reverse mutates a list; Ruby reverse! mutates whereas reverse creates another array.…
Java1import java.util.*;2public class Main {3 4 public static void main(String[] args) {5 List<Integer> values = new ArrayList<>(List.of(1, 2, 3));6 List<Integer> alias = values;7 Collections.reverse(values);8 System.out.println(alias.stream().map(Object::toString).collect(java.util.stream.Collectors.joining(" ")));9 }10}Ruby1values = [1, 2, 3]2alias_values = values3values.reverse!4puts alias_values.join(" ")Modules as Namespaces
Java packages, classes and static methods organize names at compile time. Ruby modules organize constants and methods at runtime, and require_relative loads their files explicitly. A Ruby file does not automatically create a namespace matching its name. Ruby…
Java1import java.util.*;2public class Main {3 static class TextTools { static String shout(String text) { return text.toUpperCase(Locale.ROOT); } }4 5 public static void main(String[] args) {6 System.out.println(TextTools.shout("elm"));7 }8}Ruby1module TextTools2 def self.shout(text)3 text.upcase4 end5end6puts TextTools.shout("elm")Composing Objects
Java and Ruby both compose objects by retaining references to collaborators. Storing a child does not automatically clone it or transfer exclusive ownership. Delegation can provide flexible behavior without inheritance. A constructor can accept an abstraction…
Java1import java.util.*;2public class Main {3 static class Lens { String label() { return "wide"; } }4 static class Camera {5 private final Lens lens;6 Camera(Lens lens) { this.lens = lens; }7 String label() { return lens.label(); }8 }9 10 public static void main(String[] args) {11 System.out.println(new Camera(new Lens()).label());12 }13}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 Java Programs
2 lessonsFrequently asked questions
Who is Ruby for Java Developers for?
For people who already write Java and want to learn Ruby. Each lesson starts from Java 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 Java basics: variables, conditions, loops, functions and collections.
How many lessons does Ruby for Java Developers have?
Ruby for Java Developers contains 48 interactive lessons you can complete at your own pace.
What does Ruby for Java 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 Java Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.