Learn to Code via Tutorials on Repl.it!

← Back to all posts
Ruby Crash Course
matthewproskils (464)

Pls All i want is to be on monthly repls 🙏, ok. Whatever. Let's get started.
Setup:
Create A Ruby Repl / Edit An Existing Repl
IF EDITING AN EXISTING REPL!!
Change .replit File

language = "ruby"
run = "ruby main.rb"

&& CREATE MAIN.RB FILE
Ok. Now, let's go to our main.rb file.

HELLO WORLD

push "hello world"

Boom! You just did Ruby.

Array

myarray = ["a", "b", "c"]
push myarray.to_s

Explanation: ["a", "b", "c"] is an array, and myarray.to_s adds brackets to the end and , for seperators.

Objects

myobj = {
  "one" => "1"
}
puts myobj["one"]

Explanation: Translation In Most Languages
"one" => "1" translates to "one": "1"
pust myobj["one"]: get the one of myobj

NOW FOR THE FUN

Functions

def myfunction()
  puts("My First Function Has Ran")
end
myfunction()

Explanation: def is like def token in python or function token in javascript.
end flags the end of myfunction()

Loops

while i < 10 do
  puts "loopty loopty"
  puts i
  i = i + 1
end

Explanation: this is a while loop (called a for loop in javascript / c / c# / c++)

While signals a loop, i<10 is the if statement (if i<10 continue) and both puts are part of loop. i = i + 1 adds 1 to i (which signals that 1 run is done)

If Statements

matthewissmart = true
if matthewissmart
  puts "Matthew Is Smart"
else
  pust "Matthew Is Smart ;D"
end

Explanation: if signals the start of an if statement, matthewissmart is an expression (also can be like 1=1, 2=2, you get my point) and puts is what runs if matthewissmart = true; if that is false puts "matthew is smart ;D" runs. end signals the end of the if statement.

and BOOM! there's RUBY Crash Course :D