Skip to content

Instantly share code, notes, and snippets.

@FaisalAl-Tameemi
Last active February 25, 2016 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FaisalAl-Tameemi/11b9b016ba48023b2ad1 to your computer and use it in GitHub Desktop.
Save FaisalAl-Tameemi/11b9b016ba48023b2ad1 to your computer and use it in GitHub Desktop.
Intro To Ruby Notes -- W2D2
# string are processed literally inside computers
puts "Martin and Maggie are awesome co-workers!"
puts "here's another string"
# we can add numbers since they're the same type
puts 4 + 4
# we can add to strings (concat.) together to form a third string
puts 4.to_s + 8.to_s
# floats + integers result in floats
puts 42.9 + 50
# booleans are true or false values
# computers like booleans
puts false
puts true
puts true == false
name = "Sarah"
age = 24
my_var = "Female"
is_over_18 = true
puts name
puts name + " is " + age.to_s + " years old!"
name = "Jane"
puts name
name = "Sarah"
age = 24
my_var = "Female"
is_over_18 = true
# puts name + " is " + age.to_s + " years old!"
puts "#{name} is #{age} years old!"
name = "Sarah"
age = 24
my_var = "Female"
is_over_18 = (age > 19)
# a bouncer's logic
if is_over_18
puts "#{name} is good to go in!"
puts "Please pay entry fee!"
else
puts "#{name} is underage!"
puts "Ask them to leave!"
age = 0
if age > 0
puts "Something isn't quite right."
end
end
def overage_check(age, country)
# ...
# last line will always return
if country == 'canada'
age > 19
else
age > 18
end
end
# a bouncer's logic
def bouncer_check(name, age)
if overage_check(age, 'canada')
puts "#{name} is good to go in!"
puts "Please pay entry fee!"
else
puts "#{name} is underage!"
puts "Ask them to leave!"
end
end
name = "Sarah"
age = 24
name_2 = "Jackson"
age_2 = 18
bouncer_check(name, age)
bouncer_check(name_2, age_2)
# method definition
# takes 2 params
def add(num1, num2)
# returns the addition
num1 + num2
end
# save the value of the response of the add method
# given params 5 and 7 into a variable named "added"
added = add(5, 7)
# a method to check if a person passed as an argument has age of more than 19
def overage_check(person)
if person[:age] > 19
true
else
false
end
end
# ARRAYS: for lists of things
list_of_nums = [1, "two", ["x", "y"], {key: "value"}]
list_of_nums.each do |current_number|
puts "The current number is #{current_number}"
end
# print the first element of our array
# notice that the index starts at 0
puts list_of_nums[0]
# HASHES: for mixing data types
person_1 = {
name: "Sarah",
age: 24
}
person_2 = {
name: "Jackson",
age: 18
}
# AN ARRAY OF HASHES
people = [person_1, person_2]
# same as ...
people_2 = [{
name: "Sarah",
age: 24
}, {
name: "Jackson",
age: 18
}]
# How to access data from arrays and hashes
puts "Is #{person_1[:name]} underage? #{overage_check(person_1)}"
puts "Is #{people[0][:name]} underage? #{overage_check(people[0])}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment