Skip to content

Instantly share code, notes, and snippets.

@bjhaid
Last active December 11, 2015 23:08
Show Gist options
  • Save bjhaid/4674525 to your computer and use it in GitHub Desktop.
Save bjhaid/4674525 to your computer and use it in GitHub Desktop.
Ruby Assignment for Kenny
#1. Write a script that would request for a person's name, age, home address, and print it to console in the format:
My name is Ayodele Abejide, I am 29 years old, I live in Gbagada, Lagos, Nigeria.
print "What is your name? "
name = gets.chomp
print "How old are you? "
age = gets.chomp
print "Where do you live? "
home_address = gets.chomp
puts "My name is #{name}, I am #{age} years old, I live in #{home_address}"
#2. In 3 different ways, write a program to sum the squares between 1 and 100 (e.g 1*1 + ... + 100*100)
#way number 1
y = 0
(1..100).each do |x|
y+=(x*x)
end
puts y
#way number 2
y = 0
x = 1
100.times do
y+=(x*x)
x+=1
end
puts y
#way number 3
y = 0
x = 1
while x <= 100 do
y+=(x*x)
x+=1
end
puts y
#3. Write a simple class with name Hello and method world such that Hello.new.world would print out "Hello World!"
class Hello
def world
puts "Hello World!"
end
end
#1. Write a script that would request for a person's name, age, home address, and print it to console in the format:
My name is Ayodele Abejide, I am 29 years old, I live in Gbagada, Lagos, Nigeria.
print "What is your name? "
name = gets.chomp
print "How old are you? "
age = gets.chomp
print "Where do you live? "
home_address = gets.chomp
puts "My name is #{name}, I am #{age} years old, I live in #{home_address}"
#2. In 3 different ways, write a program to sum the squares between 1 and 100 (e.g 1*1 + ... + 100*100)
#way number 1
y = 0
(1..100).each do |x|
y+=(x*x)
end
puts y
#way number 2
y = 0
x = 1
100.times do
y+=(x*x)
x+=1
end
puts y
#way number 3
y = 0
x = 1
while x <= 100 do
y+=(x*x)
x+=1
end
puts y
#3. Write a simple class with name Hello and method world such that Hello.new.world would print out "Hello World!"
class Hello
def world
puts "Hello World!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment