Last active
July 31, 2020 23:28
-
-
Save Sheg5/f5adb09da0c7a6961d564acd5f07c7ce to your computer and use it in GitHub Desktop.
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
https://anyonecanlearntocode.com/tutorial/lessons/episode-1-intro-to-ruby |
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
#1 | |
puts 3 - 6 #=> -3 | |
#2 | |
PUTS 3 + 3 #=> No Method Error | |
#3 | |
p 7 + 3 #=> 10 | |
puts 7 + 3 #=> 10 | |
print 7 + 3 #=> 10 |
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
#1 | |
puts 8 #=> 8 | |
puts 6 #=> 6 | |
puts 7 #=> 7 | |
puts 5 #=> 5 | |
puts 3 #=> 3 | |
puts 0 #=> 0 | |
puts 9 #=> 9 | |
#2 | |
puts 3 #=> 3 | |
puts 1 #=> 1 | |
put 4 #=> No Method Error | |
puts 1 #=> 1 | |
puts 5 #=> 5 | |
puts 9 #=> 9 |
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
#BODMAS | |
#1 | |
puts 21 - 6 / 2 * 3 #=> 12 | |
#2 | |
puts 100 * 5 - 2 / 2 #=> 499 | |
puts 100 * (5 - 2) / 2 #=> 150 | |
#3 | |
puts 1/0 #=> ZeroDivisionError: divided by 0 |
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
#1 | |
puts "Shegs " . "Olu" |
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
#1 | |
puts 12 * "4" # should produce => 48 | |
puts 12 * 4 #=> 48 | |
puts 3 + " times a day" # should produce => 3 times a day | |
puts "3" + " times a day" #=> 3 times a day |
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
#1 | |
puts "YUrI TOMoko".downcase | |
#2 | |
puts "spaceship".reverse #=> pihsecaps | |
puts "racecar".reverse #=> racecar | |
puts 1776.reverse #=> 6771 | |
#3 | |
puts 12 * "4" # should produce => 48 | |
puts 12 * "4".to_i #=> 48 | |
puts 3 + " times a day" # should produce => 3 times a day | |
puts 3.to_s + " times a day" #=> 3 times a day |
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
#1 | |
a = 1 | |
b = 2 | |
c = a + b | |
d = c + a + b | |
puts d #=> 6 | |
#2 | |
hello = "goodnight" | |
world = " moon" | |
puts hello + world #=> goodnight moon | |
hello = cat | |
world = "fish" | |
puts hello + world #=> undefined local variable | |
x = 1 | |
y = 2 | |
puts x + y #=> 3 | |
#3 | |
a = 1 | |
b = a | |
puts b #=> 1 | |
#4 | |
a = 1 | |
b = a | |
a = 2 | |
puts b #=> 1 |
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
#1 | |
class Animal | |
attr_accessor :name, :num_of_legs, :family | |
def initialize(options) | |
@name = options[:name] | |
@num_of_legs = options[:num_of_legs] | |
@family = options[:family] | |
end | |
def has_more_legs?(animal) | |
num_of_legs > animal.num_of_legs | |
end | |
def creepy? | |
num_of_legs > 4 | |
end | |
def creepy_message | |
creepy? ? "much creepy" : "much not creepy" | |
end | |
def description | |
"This animal's name is #{name}. And the animal is very #{creepy_message}." \ | |
" It has #{num_of_legs} legs and is part of the #{family} family." | |
end | |
end | |
xoloitzcuintli = Animal.new({name: "Xoloitzcuintli", num_of_legs: 4, family: "Canidae"}) | |
ptarmigan = Animal.new({name: "Ptarmigan", num_of_legs: 2, family: "Phasianidae"}) | |
tsetse_fly = Animal.new({name: "Tsetse Fly", num_of_legs: 6, family: "Glossinidae"}) | |
platypus = Animal.new({name: "Platypus", num_of_legs: 4, family: "Ornithorhynchidae"}) | |
assassin_bug = Animal.new({name: "Assassin Bug", num_of_legs: 6, family: "Assassin Bug"}) | |
puts "Does the #{tsetse_fly.name} have more legs than #{ptarmigan.name}? : " + tsetse_fly.has_more_legs?(ptarmigan).to_s | |
puts "Is the #{xoloitzcuintli.name} creepy? : " + xoloitzcuintli.creepy?.to_s | |
puts platypus.description | |
puts assassin_bug.description | |
puts "Does the #{xoloitzcuintli.name} have more legs than #{tsetse_fly.name}? : " + xoloitzcuintli.has_more_legs?(tsetse_fly).to_s | |
puts tsetse_fly.description | |
puts xoloitzcuintli.description | |
puts "Is the #{platypus.name} creepy? : " + platypus.creepy?.to_s | |
puts ptarmigan.description |
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
#1 | |
x = 3 #=> 3 | |
x = x + 4 #=> 3 + 4 = 7 | |
x = x - 5 #=> 7 - 5 = 2 | |
puts x #=> 2 | |
x = x + x #=> 2 + 2 = 4 | |
x = x + x #=> 4 + 4 = 8 | |
x = x + x #=> 8 + 8 = 16 | |
x = x * x #=> 16 * 16 = 256 | |
x = "goodbye" #=> goodbye | |
#2 | |
first_number = 3 | |
puts first_number + second_number #=> undefined local variable | |
#3 | |
my_favorite_number = 9 | |
your_favorite_number = 1 | |
both = my_favorite_number + your_favorite_number | |
puts both #=> 10 |
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
#1 | |
puts "Please, Enter first name: " | |
FIRST_NAME = gets.chomp | |
puts "Please, Enter middle name: " | |
MIDDLE_NAME = gets.chomp | |
puts "Please, Enter last name: " | |
LAST_NAME = gets.chomp | |
puts "Your full name is #{FIRST_NAME} #{MIDDLE_NAME} #{LAST_NAME}" | |
#2 | |
SECONDS_IN_YEAR = 31556952 | |
puts "How old are you: " | |
USR_AGE = gets.to_i | |
puts "You are #{SECONDS_IN_YEAR * USR_AGE} seconds old" | |
#3 | |
print "Enter start year: " | |
START_YEAR = gets.to_i | |
print "Enter end year: " | |
END_YEAR = gets.to_i | |
print "#{END_YEAR - START_YEAR} years\n" |
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
#1 | |
print "Please, Enter your name: " | |
USR_NAME = gets.chomp | |
print "Please, Enter your age: " | |
USR_AGE = gets.to_i | |
puts "Welcome #{USR_NAME}! In 10 years, you will be #{USR_AGE + 10}!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment