Skip to content

Instantly share code, notes, and snippets.

@rikas
Created January 15, 2019 10:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rikas/e6578dc6775e68812472078e915bec52 to your computer and use it in GitHub Desktop.
Save rikas/e6578dc6775e68812472078e915bec52 to your computer and use it in GitHub Desktop.
Programming basics — batch 224
# methods.rb
# method names should be snake_cased
# no arguments
def random_method
return 10
end
# puts random_method
# Method with two parameters
def full_name(first_name, last_name)
first = first_name.capitalize
last = last_name.capitalize
# NEVER USE PUTS INSIDE YOUR METHODS!
# puts "#{first} #{last}"
return "#{first} #{last}"
end
# We are calling full_name with 2 arguments
his_name = full_name('joão', 'costa')
my_name = full_name('ricardo', 'otero')
puts "His name is #{his_name}"
puts "My name is #{my_name}"
# strings.rb
# 2019 - 1999
# String concatenation
puts "My age is" + " " + (2019 - 1999).to_s + " " + "years old"
# String interpolation [preferred]
# It will call .to_s implicitly
puts "My age is #{2019 - 1999} years old"
# puts 'My age is #{2019 - 1999} years old' # this won't work!
# variables.rb
# Assigning a new value to the var. age
age = 2019 - 1999
puts "My age is #{age} years old"
# Reassignment
# snake_case!
new_age = age + 1
puts "My age was #{age} but now is #{new_age} years old"
first_name = 'ricardo'
last_name = 'otero'
first_name.capitalize # won't change the variable
# Destructive method
first_name.capitalize! # will change the variable
puts "My name is #{first_name} #{last_name.capitalize}"
puts first_name # it will be capitalized!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment