Created
April 16, 2012 20:52
-
-
Save mkoby/2401434 to your computer and use it in GitHub Desktop.
Intro to Ruby - 12 - Input, Output
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 “Enter your name:” | |
name = gets.chomp | |
puts “Hello, #{name}!” | |
## Output | |
# PROMPT> ruby hello.rb | |
# Enter your name: | |
# Michael | |
# Hello, Michael! |
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
name = ARGV[0] | |
puts “Please pass a name” unless name | |
puts “Hello, #{name}!” if name | |
## Output | |
# PROMPT> ruby hello2.rb Michael | |
# Hello, Michael! |
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 get_higher_limit | |
limit = ARGV[0] | |
limit = 100 unless limit | |
return limit.to_i | |
end | |
def get_result(number) | |
if(number % 3 == 0 && number % 5 == 0) | |
return "FizzBuzz" | |
elsif(number % 3 == 0) | |
return "Fizz" | |
elsif(number % 5 == 0) | |
return "Buzz" | |
else | |
return number.to_s | |
end | |
end | |
limit = get_higher_limit | |
1.upto(limit) do |n| | |
puts get_result(n) | |
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
def get_higher_limit | |
puts "Enter max number: " | |
limit = gets.chomp.to_i | |
return limit | |
end | |
def get_result(number) | |
if(number % 3 == 0 && number % 5 == 0) | |
return "FizzBuzz" | |
elsif(number % 3 == 0) | |
return "Fizz" | |
elsif(number % 5 == 0) | |
return "Buzz" | |
else | |
return number.to_s | |
end | |
end | |
limit = get_higher_limit | |
1.upto(limit) do |n| | |
puts get_result(n) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment