Created
November 30, 2017 00:19
-
-
Save mdepolli/1e1504924c8d7dc4d90c1e5c8ad78f9e to your computer and use it in GitHub Desktop.
Ruby para iniciantes - 29/nov/2017
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
john = "John Lennon" | |
ringo = "Ringo Starr" | |
paul = "Paul McCartney" | |
george = "George Harrison" | |
beatles = [john, ringo, paul, george] | |
sorted_beatles = beatles.sort | |
puts sorted_beatles |
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
puts "Qual o limite superior?" | |
print ">" | |
upper_limit = gets.chomp.to_i | |
(1..upper_limit).each do |n| | |
if n % 3 == 0 && n % 5 == 0 | |
puts "FizzBuzz" | |
elsif n % 3 == 0 | |
puts "Fizz" | |
elsif n % 5 == 0 | |
puts "Buzz" | |
else | |
puts n | |
end | |
end |
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
number = (1..10).to_a.sample | |
puts "Qual o seu chute?" | |
print ">" | |
guess = gets.chomp.to_i | |
if guess == number | |
print "Você acertou!" | |
else | |
print "Você errou!" | |
end | |
puts " O número é #{number}." |
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
puts "Que horas são?" | |
print ">" | |
time = gets.chomp.to_i | |
puts time | |
if time < 12 | |
puts "Bom dia!" | |
elsif time < 18 | |
puts "Boa tarde!" | |
elsif time < 24 | |
puts "Boa noite!" | |
end |
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
age = 17 | |
puts "Você tem #{age} anos de idade." |
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
def print_names(names) | |
names.each do |n| | |
puts n.upcase | |
end | |
end | |
print_names(["marcelo", "mathieu", "flávio"]) |
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
# Resolvi fazer esse método porque tenho preguiça de escrever nomes inteiros | |
def print_name(first_name, last_name) | |
if first_name == "Flávio" | |
puts first_name + " " + last_name | |
else | |
puts "Não é o Flávio." | |
end | |
end | |
print_name("Flávio", "Fernandes") | |
print_name("Marcelo", "De Polli") | |
print_name("Mathieu", "") |
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
puts "Qual a sua idade?" | |
print ">" | |
age = gets.chomp.to_i | |
if age >= 16 | |
puts "Você pode votar!" | |
else | |
puts "Você precisa esperar um pouco." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment