Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Last active January 14, 2020 23:32
Show Gist options
  • Save rodloboz/8322cf988ad15f757217b034b51e2203 to your computer and use it in GitHub Desktop.
Save rodloboz/8322cf988ad15f757217b034b51e2203 to your computer and use it in GitHub Desktop.
Ruby 02 - Flow and Array
# Data Types / Built-in Objects
"Word" # String
'Word' # String
34 # Integer
3.14 # Float
true # Boolean - TrueClass
false # Boolean - FalseClass
nil # Nil
# Array
# puts obj.inspect
p [34, 3.14, true, "Hello"]
# Range
(1..10) # includes the last element
(1...10) # excludes the last element
# Variables (snake_case)
first_name = 'rui'
last_name = 'freitas'
# Concatenation
# (combining strings together)
puts first_name + " " + last_name
# Interpolation
# inject ruby expressions into strings
puts "#{first_name} #{last_name}"
puts '#{first_name} #{last_name}'
# Method
# parameters
def full_name(first_name, last_name)
# Guard clause:
# return first_name.capitalize if first_name.length <= 4
if first_name.length <= 4
first_name.capitalize
else
"#{first_name.capitalize} #{last_name.capitalize}"
end
end
#. var defined in lines 23 and 24
# arguments
puts full_name(first_name, last_name)
puts full_name("john", "doe")
# Pseudocode
# ask user for their age
# capture/store the age
# compare the age with 18
# if age is over 18 can vote
puts "What's your age?"
print "> "
age = gets.chomp.to_i
# If structure
# if age >= 18
# puts "You can vote!"
# end
# Inline if/condition
# puts "You can vote!" if age >= 18
# Inline unless
# puts "You can vote!" unless age < 18
# If/Else Strucutre
if age >= 18
# runs if true
puts "You can vote!"
else
# runs if false
puts "Go home! You're a kiddo!"
end
# Ask user to pick a side and store choice # gets.chomp
# Program should pick a random side Array#sample
# Compare user choice with coin
puts "Heads or tails?"
print "> "
choice = gets.chomp.downcase
# ['heads', 'tails']
coin = %w[heads tails].sample
# if choice == coin
# puts "You win!"
# else
# puts "You loose!"
# end
# Ternary operator
# ? run if true : run if false (else)
# condition ? do something : do something else
puts choice == coin ? "You win!" : "You loose!"
# Good morning: before 12
# Good afternoon: after 13 until 18
# Good evening: after 18 until 20
# Good night: after 20
hour = Time.now.hour
if hour < 12
puts "Good morning"
elsif hour >= 13 && hour <= 18
puts "Good afternoon"
elsif hour > 18 && hour <= 20
puts "Good evening"
elsif hour > 20
puts "Good night"
else
puts "Enjoy your lunch!"
end
puts "Which action? [ read | write | list | dance | exit ]"
print "> "
choice = gets.chomp.downcase.strip
case choice
when "read"
puts "Reading..."
when "write"
puts "Writing..."
when "list"
puts "Listing..."
when "dance"
puts "Dancing..."
when "exit"
puts "Goodbye!"
else
puts "Unknown option!"
end
# puts "Guess the number (1-5)?"
# print "> "
# price_to_find = rand(1..5)
# choice = gets.chomp.to_i
# while choice != price_to_find
# puts "Wrong number! Try again."
# print "> "
# choice = gets.chomp.to_i
# end
# until choice == price_to_find
# puts "Wrong number! Try again."
# print "> "
# choice = gets.chomp.to_i
# end
# puts "You picked #{choice} and the number was #{price_to_find}"
# puts "Correct! You win!"
counter = 0
for num in ["a", "b", "c"]
counter += 1
print "Counter: #{counter}"
puts num
end
# 0 1 2. # indexes
beatles = ["john", "ringo", "seb"]
puts beatles.size
puts beatles.length
puts beatles.count
# Read
puts beatles[1]
puts beatles[beatles.length - 1]
puts beatles.last
# Writing
p beatles
puts beatles[1] = "paul"
p beatles
# Append
beatles << "mike"
p beatles
# Prepending
beatles.unshift("susan")
p beatles
# Delete
# by value
beatles << "seb"
p beatles
# beatles.delete("seb")
# p beatles
# by index
beatles.delete_at(2)
p beatles
beatles.each do |beatle|
puts "Hello #{beatle.capitalize}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment