Download Ruby: https://www.ruby-lang.org/en/downloads/
e.g. on Windows: https://rubyinstaller.org/downloads/
Or use TryRuby playground online
- TryRuby online interactive tutorial
- Learn Ruby in One Hour, YouTube video by TechmakerTV
-
General
- Parentheses:
puts "Hello"is equivalent toputs("Hello") - Blocks:
do ... end(is equivalent to{ ... })begin ... end
- Parentheses:
-
Flow Control
if condition ... else ... ende.g.if my_string.empty? print "It's empty" end- Ternary:
condition ? ... : ... case variable when value; ... else ... end
e.g.case value when 1; puts "value is 1" when 2; puts "value is 2" else puts "value is neither 1 nor 2" end- Loops:
loop do ... break ... endfor ... in ... ende.g.for i in 1..3; puts i endwhile/until condition ... ende.g.x = 0; while x <= 10; puts x; x += 1 endbegin ... end while/until condition
- More on Flow Control: Introduction to Programming with Ruby: Flow Control online tutorial by Launch School
-
Objects
methodsarray of methods for an object e.g.1.methodsclassclass of an object e.g.1.class
-
Output
"Hello World"puts(adds a new line) e.g.puts "outputs a string",puts 1 + 2,puts [12, 47, 35]print(no new line)<<output to a file, append to an array e.g.file << "some text"
-
Numberclass:1.class,3.14.class- Operators:
+*-/**
Combine with=to assign to variable e.g.x += 3 to_sto string e.g.40.to_stimes: Enumerator from 0 to number e.g.5.times { |i| print i }
- Operators:
-
Booleanclass:true.class- Operators:
&&|| - Quick guide: RubyGuides: Understanding Boolean Values in Ruby
- Operators:
-
Stringclass:"my string".classlengthreverse*to repeat e.g."Hi " * 5to_ito integer e.g."23 eggs".to_igsubglobal substitute e.g."I love coding".gsub("coding", "eating")linesto split into an array of lines e.g."Hi\nHow are you?\n".linesjoine.g."Hi\nHow are you?\n".lines.reverse.join"#{variable}"interpolate variable inside string e.g."Welcome #{username}!"
-
Symbols, similar to enums, e.g.
:blue,:red,:id,:name(treated as strings) -
Arrayclass:[].class- Example:
arr = [12, 47, 35] min,maxe.g.[12, 47, 35].minsortandsort!(see Functions/Methods > Conventions section below)[n]access the nth element e.g.arr[0]eache.g.arr.each { |value| puts value }selecte.g.arr.select { |value| value > 30 }
- Example:
-
Hashclass:{}.class- Example:
book_dates = {"Title 1": 1983, "Title 2": 2004} newe.g.my_hash = Hash.new {0}would use0as the default value for uninitialized keys[key]e.g.books["Awesome Book Title"]keysarray of keysvaluesarray of valueseache.g.book_dates.each { |key, value| puts "#{key} written in #{value}" }selecte.g.book_dates.select { |key, value| value == 2004 }
- Example:
-
Functions/Methods:
def ... ende.g.def plus_one(value) value + 1 end- Conventions:
my_function!(excalmation mark) indicates the function modifies the variablemy_function?(question mark) indicates the function returns a boolean
return my_result
(if noreturnspecified, function returns the result of the last statement)- Parameters
- Default value e.g.
def plus_one(value= 0) value + 1 end *variadic function, arguments are in an array e.g.def sum(*values) values.reduce(:+) end
Callingsum 2, 3, 4returns9
- Default value e.g.
-
Classes:
class Book attr_accessor :content, :title, :author def initialize(title, author, content="") @title = title @author = author @content = content[0..39] end def to_s "#{title} by #{author}" end end my_book = Book.new "Awesome Title", "William S.", "Lorem ipsum..." puts my_book.content puts my_book.to_s