Skip to content

Instantly share code, notes, and snippets.

@matt297
Forked from MaggieMoss/ruby-examples.rb
Last active November 14, 2021 04:29
Show Gist options
  • Save matt297/055ee0b28c2bd79aa9783359d83f4906 to your computer and use it in GitHub Desktop.
Save matt297/055ee0b28c2bd79aa9783359d83f4906 to your computer and use it in GitHub Desktop.
Lighthouse Labs - Intro to Web Dev - W2D2
# STRINGS
# # we can use double quotes
puts "This is a string."
# # or we can use single quotes
puts 'This is a string with single quotes.'
# # adding two strings together
puts 'Hello, ' + "Maggie."
#INTEGER
#we can add them
puts 1 + 3
# subtraction
puts 3 - 2
# division
puts 6/2
# multiplication
puts 8*3
# exponents
puts 6**2
# gives you back the remainder
puts 7%2
# we can't add strings and integers
puts "Hello, " + 1
# FLOATS
puts 1.0 + 1
# BOOLEAN
# boolean values are either true, or false
# we don't put them into quotes - just the words true or false
puts true
puts false
# comparators return a boolean value - we can compare numbers using greater
# than and less than
puts 1 > 2
puts 1 < 2
# we use the double equals in order to see if two values are the same
# remember the single equals is used for ASSIGNING variables
puts 1 == 1
# Variables are like bookmarks if we want to store a value for later,
# we use a variable
# to assign a variable we use = (Remember == is used to compare two values)
first_name = "Maggie"
time_of_day = "morning"
# we can interpolate variables into strings
# in order to do this, we must use double quotes
puts "Hello, #{first_name}. Good #{time_of_day}!"
# => "Hello, Maggie. Good morning!"
# we can store any datatype into a variable, for example:
number = 87
float = 20.0
# Remember when you're naming variables to use descriptive names
# we also use snake case (lower case letters and underscores to separate words)
is_wearing_a_scarf = true
# IF STATEMENTS
# if statements are useful for control flow
# they let us make our code more dynamic
# to write an if statement we need an if and an end
if 4 > 3
puts "Four is greater than three"
end
#=> "Four is greater than three"
#if the comparison next to the if statement evaluates to true - then
# the code inside the if statement will run
# We can also have an else on our if statements - this will run when
# the comparator evalates to false
if "one" == "one"
puts "This was true"
else
puts "This was false"
end
# METHODS
# Methods are a piece of code or set of instructions that we are going to want to use
# more than once
# we start with the word def, name our method and then close our method with the
# keyword end
def say_hello
puts "Hello!"
end
# we then have to call our method, we can do so like this:
say_hello # => "Hello!"
# We can also pass our method arguments - or pieces of information it will need to
# run the code inside of it like this:
def time_ago(time_in_minutes)
if time_in_minutes < 60
"Less than an hour ago"
elsif time_in_minutes == 60
"One hour ago"
else
"More than an hour ago"
end
end
# we call methods with arguments like this:
time_ago(12)
# methods in ruby return the last line of the method,
# if we puts what this method returns:
puts time_ago(12) # => "Less than an hour ago"
# we can also save the output of our methods to variables like this:
time_string = time_ago(78)
# hashes are useful forA Hash is just a container for data where each piece of data is mapped to a Key.
# The data is called the Value.
# Keys can be either strings or symbols. Values can be anything, just like with arrays
# there are two ways to define a has like this:
person1 = {
:first_name => "Martin",
:last_name => "Laws",
:eye_color => "blue/green"
}
# or like this
# Both are valid, the only thing that changes is the syntax
person = {
first_name: "Maggie",
last_name: "Moss",
eye_color: "Brown"
}
# We access values on a hash using a symbol like so:
puts person[:first_name] # => "Maggie"
# we can also store more than just strings in a hash
person3 = {
first_name: "Margaret Olivia",
last_name: "Winston",
species: "Cat",
# we can also store integers
age: 7,
# or boolean values
can_purr: true
}
# if we want to store something in a list - we use an array
student_names = ["Nikki", "Kate", "Duane", "Ali"]
# we start counting the position of the elements at 0
student_names[0] # => "Nikki"
student_names[1] # => "Kate"
# we can interpolate these values into a string
"Hello, #{student_names[0]}"
# => "Hello, Nikki"
# if we want to do something to each element in an array
# we can loop over it
student_names.each do |first_name|
# each element in the array
# will be assigned to the variable first_name
puts "Hello, #{first_name}"
end
@SavageBell00
Copy link

This, along with the sinatra-dev-cheatsheet is gnarly, thank you :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment