1.Write a Ruby script to create a new string which is n copies of a given string where n is a non-
negative integer
Aim: Ruby script to create a new string which is n copies of a given string where n is a non-
negative integer
Flowchart:
Ruby Code:
def multiple_string(str, n)
return str*n
end
print multiple_string('a', 1),"\n"
print multiple_string('a', 2),"\n"
print multiple_string('a', 3),"\n"
print multiple_string('a', 4),"\n"
print multiple_string('a', 5),"\n"
Output:
a
aa
aaa
aaaa
aaaaa

2.Write a Ruby script which accept the radius of a circle from the user and compute the parameter
and area.
Aim: Ruby script which accept the radius of a circle from the user and compute the parameter and
area.
Block Structure
Flowchart:
Ruby Code:
radius = 5.0
perimeter = 0.0
area = 0.0
print "Input the radius of the circle: "
radius = gets.to_f
perimeter = 2 * 3.141592653 * radius
area = 3.141592653 * radius * radius
puts "The perimeter is #{perimeter}."
puts "The area is #{area}."
Output:
Input the radius of the circle: 2
The perimeter is 12.566370612.
The area is 12.566370612.

3. Write a Ruby script which accept the user's first and last name and print them in
reverse orderwith a space between them
Aim: Ruby script which accept the user's first and last name and print them in reverse order with
a space between them
Flowchart:
Ruby Code:
puts "Input your first name: "
fname = gets.chomp
puts "Input your last name: "
lname = gets.chomp
puts "Hello #{lname} #{fname}"
Output:
Input your first name:
Input your last name:
Hello Naga babu


4.Write a Ruby script to accept a filename from the user print the extension of that
Aim: Ruby script to accept a filename from the user print the extension of that
Flowchart:
Ruby Code:
file = "/user/system/test.rb"
# file name
fbname = File.basename file
puts "File name: "+fbname
# basename
bname = File.basename file,".rb"
puts "Base name: "+bname
# file extention
ffextn = File.extname file
puts "Extention: "+ffextn
# path name
path_name= File.dirname file
puts "Path name: "+path_name
output:
File name: test.rb
Base name: test
Extention: .rb
Path name: /user/system

5.Write a Ruby script to find the greatest of three numbers
Aim: Ruby script to find the greatest of three numbers
Flowchart:
Ruby Code:
x,y,z = 2,5,4
if x >= y and x >= z
puts "x = #{x} is greatest."
elsif y >= z and y >= x
puts "y = #{y} is greatest."
else
puts "z = #{z} is greatest."
end
output
y = 5 is greatest.

6. Write a Ruby script to print odd numbers from 10 to 1
Aim: Ruby script to print odd numbers from 10 to 1
Flowchart:
Ruby Code:
puts "Odd numbers between 9 to 1: "
9.step 1, -2 do |x|
puts "#{x}"
end
Output
Odd numbers between 9 to 1:
9
7
5
3
1

7. Write a Ruby scirpt to check two integers and return true if one of them is 20
otherwisereturn their sum
Aim: Ruby scirpt to check two integers and return true if one of them is 20 otherwise return their
sum
Flowchart:
Ruby Code:
def makes20(x,y)
return x == 20 || y == 20 || x + y == 20
end
print makes20(10, 10),"\n"
print makes20(40, 10),"\n"
print makes20(15, 20)
Output:
true
false
true


8. Write a Ruby script to check two temperatures and return true if one is less than
0 andthe other is greater than 100
Aim: Ruby script to check two temperatures and return true if one is less than 0 and the other is
greater than 100
Flowchart:
Ruby Code:
def temp(temp1, temp2)
return ( temp1 < 0 && temp2 > 100 ) || ( temp1 > 100 && temp2 < 0 );
end
print temp(110, -1),"\n"
print temp(-1, 110),"\n"
print temp(2, 120)
Output
true
true
false


9. Write a Ruby script to print the elements of a given array
Aim: Ruby script to print the elements of a given array
Block Structure
Flowchart:
Ruby Code:
array1 = ["Ruby", 2.3, Time.now]
for array_element in array1
puts array_element
end
Output
Ruby
2.3
2017-12-28 06:01:53 +0000

10 Write a Ruby program to retrieve the total marks where subject name and marks of astudent stored in a
hash
Aim: Ruby program to retrieve the total marks where subject name and marks of a student storedin a hash
Flowchart
Ruby Code:
student_marks = Hash.new 0 student_marks['Literature'] = 50
student_marks['Science'] = 40
student_marks['Math'] = 90
total_marks = 0 student_marks.each {|key,value|
total_marks +=value
}
puts "Total Marks: "+total_marks.to_s
Output
Total Marks: 180