Ruby for R Developers
Move from R 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.
R and Ruby side by side
The same program in R and in Ruby, from the lessons of this course.
Output and Comments
R cat writes values without an automatic final newline, while print can add indexes and quotes. Ruby puts adds a newline to ordinary text and print does not. Both languages use a hash sign for comments outside strings. R source evaluates a file in a selected…
R1cat(paste0("Hi",", Ada"),"\n",sep="")Ruby1print "oak!"2# A real comment3puts "#seed"Conditional Values
R if and Ruby if can both supply a selected branch value. R needs a scalar nonmissing condition, while Ruby uses truthiness with only false and nil falsey. Ruby uses elsif for another branch and closes the construct with end. An unselected branch is not…
R1wet <- FALSE2label <- if(wet) "rain" else "sun"3cat(label,"\n",sep="")Ruby1wet = false2icon = if wet3 "umbrella"4else5 "sun"6end7puts iconArrays and Indexing
R positive indexes start at one and negative indexes exclude positions, while Ruby indexes start at zero and negative indexes count from the end. Out-of-range R atomic selection can yield NA; Ruby ordinary missing array reads yield nil. Lists have additional…
R1values <- c(4,8)2cat(tail(values,1),"\n",sep="")3cat(if(is.na(values[10])) "missing" else values[10],"\n",sep="")Ruby1values = [4, 8]2puts values[-1]3puts(values[9].nil? ? "missing" : values[9])Mutation and Aliases
R vector and list replacement has value semantics, commonly implemented by copy-on-modify, while environments remain shared reference objects. Ruby assignment shares a mutable object, so reverse! is visible through every alias. Ruby reverse creates another…
R1a <- c(1); b <- a; b[1] <- 9; cat(a[1],"\n",sep="")Ruby1values = [1, 2, 3]2alias_values = values3values.reverse!4puts alias_values.join(" ")Modules as Namespaces
R source evaluates expressions in a chosen environment, and functions or environments can provide a namespace-like boundary. Ruby require_relative loads a neighboring file, while a declared module organizes constants and methods. A filename does not create a…
R1bracket <- function(text) paste0("[",text,"]"); cat(bracket("ready"),"\n",sep="")Ruby1module TextTools2 def self.shout(text)3 text.upcase4 end5end6puts TextTools.shout("elm")Composing Objects
R lists can retain function collaborators, and environments can provide shared mutable collaborators. Ruby instance variables retain object references without cloning them. Delegation supplies behavior without a superclass relationship. R promises can delay…
R1policy <- function(n) n*2; order <- list(policy=policy); total <- function(x) x$policy(3);cat(total(order),"\n",sep="")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 R Programs
2 lessonsFrequently asked questions
Who is Ruby for R Developers for?
For people who already write R and want to learn Ruby. Each lesson starts from R 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 R basics: variables, conditions, loops, functions and collections.
How many lessons does Ruby for R Developers have?
Ruby for R Developers contains 48 interactive lessons you can complete at your own pace.
What does Ruby for R 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 R Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.