Skip to content

Instantly share code, notes, and snippets.

@rikas
Created January 12, 2021 11:38
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 rikas/18096408f40276caf668f03e70df2a03 to your computer and use it in GitHub Desktop.
Save rikas/18096408f40276caf668f03e70df2a03 to your computer and use it in GitHub Desktop.
Programming basics
"Ricardo" #=> String
4 #=> Integer
4.6 #=> Float
[1,2,3] #=> Array
(1..10) #=> Range
false #=> FalseClass (Boolean)
true #=> TrueClass (Boolean)
nil #=> NilClass
name = "Ricardo"
name.upcase
# Print the result of 5 + 6 to the terminal
puts 5 + 6
puts name # "Ricardo" "RICARDO"
# DRY (Don't Repeat Yourself)!!!!!
# puts "Hello Ricardo, how are you?"
# puts "Hello Fabi, how are you?"
# puts "Hello Hanna, how are you?"
# puts "Hello José, how are you?"
# method definition (the code inside WON'T RUN until
# the method is called!)
# def greet_person
# puts "HI!"
# end
# DRY
# method with one parameter (name)
def greet_student(name)
puts "Hello #{name}, how are you today? :)"
end
# we ommit the () characters if we don't have arguments!
# greet_person
# greet_person
# we're calling the method greet_student with the ARGUMENT 'Ricardo'
greet_student('Ricardo')
greet_student('Fabi')
greet_student('Hanna')
greet_student('José')
def full_name(first_name, last_name)
first_name = first_name.capitalize
last_name = last_name.capitalize
return "#{first_name} #{last_name}"
end
# full_name is returning nil!
name = full_name('riCarDO', 'oTEro')
puts name
def max(number_1, number_2)
return 0 if number_1 == number_2
if number_1 > number_2
return number_1
else
return number_2
end
end
maximum = max(5, 6) # => 6
puts maximum # => 6
def upcased_name(name)
5 + 6
new_name = name.downcase
# return new_name
name.upcase
end
upcased = upcased_name('ricardo')
puts upcased
def stupid_method
end
result = puts('Ricardo')
puts result
require 'date'
def tomorrow
tomorrow_date = Date.today + 1
return tomorrow_date.strftime("%B %d")
end
puts tomorrow # => "October 4"
puts "Hello world"
puts 'Hello world'
name = 'Ricardo'
age = 37
# Concatenation of strings
# puts 'Hello' + ' ' + name + ' ' + 'how are you?'
puts 'You are' + ' ' + 37.to_s + ' ' + 'years old'
# Interpolation of strings (needs double quotes!)
# puts "Hello #{name} how are you?"
puts "You are #{36 + 1} years old"
# variable names should be lower snake case
user_city = 'Lisbon'
age = 40 # assigning a variable value
age_in_four_years = age + 4
age = age + 4 # re-assignment
age += 4 # sugar syntax to age = age + 4
puts age #=> 44
age = 50 # re-assignment
puts age #=> 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment