Created
January 15, 2019 10:43
-
-
Save rikas/e6578dc6775e68812472078e915bec52 to your computer and use it in GitHub Desktop.
Programming basics — batch 224
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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