Blog Single
Ruby Activity 1
Topics : ruby data types, operators ,conditional and looping statements

1. Write a Ruby program that declares a variable var, holding your name as its value. Also display "Hello ..followed by your name".
File : p1.rb 
name=”student” />
print(“Hello.. #{name}\n”)  # same as puts(“Hello..#{name}”)
2. Write a Ruby program :
  • a. Declare a variable fname & initialize your first name as its value.
  • b. Declare another variable lname & initialize your last name as its value
  • c. Using string concatenation operator, join fname with lname and display its length to the monitor.
File: p2.rb
fname=”Karthikeyan”
lname=”Palani”
print("My Full name is:#{fname+lname}\n")
3. Explore the Ruby program:
  • no=56
  • item_cost=509.56
  • description=””
  • Using class() method ,determine the class type of the above declaration
File: p3.rb
	
no=56
item_cost=509.56
description=””

print("value #{no} is belongs to #{no.class()}\n")
print("value #{item_cost} is belongs to #{item_cost.class()}\n")
print("value #{description} is belongs to #{description.class()}\n")
4. Write a Ruby program:
  • a. declare a string variable as os
  • b. initialize your working os name as its value( ex: if your working operating system is aix, os="aix" )
  • c. using print() and puts() display working operating system name to monitor
  • Note: understand the difference between puts() vs print() function
File : p4.rb
	
os=”Linux”

print(“my working operating system is : #{os}\n”)
puts # empty line
puts(“my working operating system is : #{os}”)
5. Write a Ruby program:
  • a. Read a business enquiry number from STDIN
  • b. Test whether your enquiry number ranges between 500 - 600. If matched, read a quotation number
  • c. If your quotation number ranges between 550 -650,display input enquiry number and quotation number to monitor.
  • d. If any of the condition fails script won't take next input. Write suitable invalid message if condition is not matched.
File: p5.rb

print("Enter a  business enquiry number:")
eno=gets.to_i # reading value from  and type cast to integer
if(eno >500 and eno <600) then
	print("Enter a quotation number:")
	qno=gets.to_i
	if(qno >550 and qno<650) then
		puts("="*55)  # string repetition operator  
		puts("Business Enquiry Number:#{eno}\tQuotation number:#{qno}")
		puts("="*55)  # string repetition operator  
	else
		puts("Sorry your input quotation number is not valid range")
	end
else
	puts("Sorry your input business enquiry number is not valid range")
end
6. Write a Ruby program that emulates the high-street bank mechanism for checking a PIN. Keep taking an input from the keyboard (STDIN) until it is identical to a PIN declared. Restrict the number of attempts to 3 and output a suitable message for success and failure.
File: p6.rb

pin=1234 
max=3
count=0
while(count<3) do
	print("Enter a pin number:")
	p=gets.to_i  # reading pin number(Integer type) from  and type cast to integer 
	count+=1
	if(p == pin) then
		break # exit from loop
	end
end
if(pin != p) then
	puts ("Sorry your pin is blocked")
else
	puts("Success pin is matched at #{count} ")
end
7. Write a Ruby program to find the sum of all numbers stored in an Array.
  • # numbers = [16, 50, 300,5, 40, 110]
File: p7.rb
	
numbers = [16, 50, 300,5, 40, 110]
total=0

for v in numbers do
	total=total+v
end
puts("Sum of numbers:#{total}")
8. Write a ruby program :
  • contents=[“Data1\n”,”Data2\n”,”Data3\n”,”Data4\n”,”Data5\n”]
  • From the above array display list of all the data in below format.
  • Expected result
  • 1. Data1
  • 2. Data2
  • 3. Data3
  • 4. Data4
  • 5. Data5
  • Total no.of lines: 5
File : p8.rb
	
contents=["Data1\n","Data2\n","Data3\n","Data4\n","Data5\n"]

count=0
for v in contents do
	count+=1
	puts("#{count}\t#{v}")
end
puts("Total no.of lines:#{count}")

# using length function.
puts("Total no.of lines:#{contents.length}")

9. Write a Ruby script:
  • a. Create an empty Array named as “users”
  • b. Read any 5 user name from STDIN , insert each name into existing users Array,
  • c. Display all the user names in sorted order (use : sort function, array_name.sort() )
File : p9.rb
	
users=[]
c=0
while(c<5) do
	print("Enter a user name:")
	var=gets.chomp # remove \n char
	users.push(var) # append operation
	c+=1

end
puts("List of users:-")
for v in users.sort do
	puts("#{v}")
end
      
puts "=>"*5+"\t"+"Thank you\t"+"<="*5
10. Write a Ruby program
  • a. Create an empty Hash
  • b. Display the size of Hash
  • c. Read the following details from STDIN - employee name,ID,dept,place and Initialize them to hash.
  • d. Using keys() function, display Hash details(key,value) .
File : p10.rb
	
emp={} # empty hash
print("Size of hash:#{emp.length}\n")

print("Enter a emp name:")
ename=gets.chomp

print("Enter a emp ID:")
eid=gets.chomp

print("Enter #{ename} working department:")
edept=gets.chomp

print("Enter #{ename} working city:")
eplace=gets.chomp

# adding data to hash

emp['NAME']=ename  
emp['EID']=eid
emp['DEPT']=edept
emp['PLACE']=eplace
puts("------------------   
Emp details:-
------------------")
for v in emp.keys   # list of keys
	puts("#{v}\t#{emp[v]}")
end
puts("------------------")
puts   # empty line
puts("======= Thank you !!! ======")
Tags:
Share:
Comments
  • May 12,2020

    Learning new Programming language helps us to enhance our skill. These Ruby Questions are Usefull

Write a comment
Your email address will not be published. Required fields are marked *
Review*
Name*
Email*
Notify me of new posts by email