0% found this document useful (0 votes)
103 views24 pages

Ruby Student

The document contains 10 Ruby programs and 2 TCL programs on various scripting tasks. The Ruby programs include scripts to: 1) Create a string with n copies of a given string 2) Calculate the area and perimeter of a circle given radius 3) Reverse first and last names with a space between 4) Find the file extension given a filename 5) Find the greatest of three numbers 6) Print odd numbers from n to 1 7) Check if one of two numbers is 20, otherwise return sum 8) Check if one temperature is <0 and one >100 9) Print elements of an array in reverse order 10) Calculate total marks from a hash of subject names and

Uploaded by

Vicky Vignesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views24 pages

Ruby Student

The document contains 10 Ruby programs and 2 TCL programs on various scripting tasks. The Ruby programs include scripts to: 1) Create a string with n copies of a given string 2) Calculate the area and perimeter of a circle given radius 3) Reverse first and last names with a space between 4) Find the file extension given a filename 5) Find the greatest of three numbers 6) Print odd numbers from n to 1 7) Check if one of two numbers is 20, otherwise return sum 8) Check if one temperature is <0 and one >100 9) Print elements of an array in reverse order 10) Calculate total marks from a hash of subject names and

Uploaded by

Vicky Vignesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

RUBY SCRIPTS
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: To Write a Ruby script to create a new string which is n copies of a given
string where n is a non-negative integer.

Program:

def multiple_str(str, n)
return str*n
end
print multiple_str('km', 1),"\n"
print multiple_str('km ', 2),"\n"
print multiple_str('km ', 3),"\n"
print multiple_str('km ', 4),"\n"
print multiple_str('km ', 5),"\n"

Output:

E:\OneDrive\Desktop\RUBY>ruby prog1.rb
km
km km
km km km
km km km km
km km km km km

1
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

2. Write a Ruby script which accept the radius of a circle from the user and
compute the parameter and area.

Aim: To Write a Ruby script which accept the radius of a circle from the user and
compute the parameter and area.

Program:

puts("enter radius:")
radius=gets.to_f
def area_cir(radius)
return 3.141*radius*radius
end
def peri_cir(radius)
return 3.141*2*radius
end
puts("area is:")
puts area_cir(radius)
puts("perimeter is:")
puts peri_cir(radius)

2
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

output:

E:\OneDrive\Desktop\RUBY>ruby prog2.rb
enter radius:
6
area is:
113.076
perimeter is:
37.692

3
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

3. Write a Ruby script which accept the user's first and last name and print them
in reverse order with a space between them.

Aim: To Write a Ruby script which accept the user's first and last name and print
them in reverse order with a space between them.

Program:
puts("enter the first name:")
first=gets.chomp
puts("enter last name:")
last=gets.chomp
def rev(first,last)
name=first+" "+last
newstr= ' '
for i in 1..name.length
newstr+=name[name.length-i]
end
return newstr
end
str=rev(first,last)
puts("the reverse is #{str}")

4
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

Output:
E:\OneDrive\Desktop\RUBY>ruby prog3.rb
enter the first name:
hello
enter last name:
kmit
the reverse is timk olleh

4. Write a Ruby script to accept a filename from the user print the extension of
that file.
5
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

Aim: To Write a Ruby script to accept a filename from the user print the
extension of that file.

Program:
file=gets.chomp
#filename
filename=File.basename file
puts("File name:"+filename)
#file basename
bname=File.basename file,".rb"
puts("Base name:"+bname)
#file extension
ffextn=File.extname file
puts("Extension:"+ffextn)
#path name
path_name=File.dirname file
puts("pathname:"+path_name)

Output:

E:\OneDrive\Desktop\RUBY>ruby prog4.rb
E:\OneDrive\Desktop\RUBY\prog.rb
File name:prog.rb
Base name:prog
Extension:.rb
pathname:E:\OneDrive\Desktop\RUBY

5. Write a Ruby script to find the greatest of three numbers.

6
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

Aim: To Write a Ruby script to find the greatest of three numbers.


Program:
puts("enter three number:")
x=Integer(gets.chomp)
y=Integer(gets.chomp)
z=Integer(gets.chomp)
def greattest(x,y,z)
if(x>y and x>z)
puts ("greatest #{x}")
elsif(y>x and y>z)
puts ("greatest #{y}")
elsif(z>x and z>y)
puts ("greatest #{z}")
else
puts("all are equal x=#{x} y=#{y} z=#{z}")
end
end
greattest(x,y,z)

Output:

7
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

E:\OneDrive\Desktop\RUBY>ruby prog5.rb
enter three number:
6
5
4
greatest 6

E:\OneDrive\Desktop\RUBY>ruby prog5.rb
enter three number:
6
6
6
all are equal x=6 y=6 z=6

6. Write a Ruby script to print odd numbers from n to 1.

8
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

Aim: To Write a Ruby script to print odd numbers from n to 1.

Program:

puts("enter a number:")
a=gets.to_i
def oddn(a)
while(a>0)
if(a%2!=0)
puts("output:#{a}");
end
a=a-1
end
end
oddn(a)

Output:
9
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

E:\OneDrive\Desktop\RUBY>ruby prog6.rb
enter a number:
15
output:15
output:13
output:11
output:9
output:7
output:5
output:3
output:1

7.Write a Ruby script to check two integers and return true if one of them is 20
otherwise return their sum.

10
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

Aim: to Write a Ruby script to check two integers and return true if one of them
is 20 otherwise return their sum.

Program:
puts("enter a:")
a=gets.to_i
puts("enter b:")
b=gets.to_i;
def sum(a,b)
if(a==20 or b==20)
puts("true")
else
puts("sum:#{a+b}")
end
end
sum(a,b)

Output:

11
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

enter a:
5
enter E:\OneDrive\Desktop\RUBY>ruby prog7.rb
b:
6
sum:11

E:\OneDrive\Desktop\RUBY>ruby prog7.rb
enter a:
5
enter b:
20
true

8. Write a Ruby script to check two temperatures and return true if one is less
than 0 and the other is greater than 100.

12
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

Aim: To Write a Ruby script to check two temperatures and return true if one is
less than 0 and the other is greater than 100.

Program:
puts("enter temperatures:")
t1=gets.to_i
t2=gets.to_i
def temp(t1,t2)
if(t1<0 and t2>100)
puts("true")
else
puts("false")
end
end
temp(t1,t2)
Output:

E:\OneDrive\Desktop\RUBY>ruby prog8.rb
enter temperatures:
-1
101
true

E:\OneDrive\Desktop\RUBY>ruby prog8.rb
enter temperatures:
1
99
False

9. Write a Ruby script to print the elements of a given array in reverse order.

13
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

Aim: To Write a Ruby script to print the elements of a given array in reverse
order.

Program:
puts("enter array size:")
n=gets.to_i
arr=[]
for i in 0..n-1
print("enter elements:")
arr[i]=gets.to_i
end
def revarr(arr)
size=arr.length
puts("reverse elements are:")
for i in 1..size
puts("#{arr[size-i]}")
end
end
revarr(arr)

Output:

E:\OneDrive\Desktop\RUBY>ruby prog9.rb
enter array size:
14
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

5
enter elements:1
enter elements:2
enter elements:3
enter elements:4
enter elements:5
reverse elements are:
5
4
3
2
1

10. Write a Ruby program to retrieve the total marks where subject name and
marks of a student stored in a hash.

15
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

Aim: To Write a Ruby program to retrieve the total marks where subject name
and marks of a student stored in a hash.

Program:

def totalmarks(stu_marks)
total_marks=0
stu_marks.each{|key,value|
total_marks+=value}
puts("Total marks:"+total_marks.to_s)
end
student_marks=Hash.new 0
puts("enter three subject marks:")
student_marks['Ruby']=gets.to_i
student_marks['TCL']=gets.to_i
student_marks['PERL']=gets.to_i

totalmarks(student_marks)
Output:

E:\OneDrive\Desktop\RUBY>ruby prog10.rb
enter three subject marks:
10
50
100
Total marks:160
TCL SCRIPTS
11. Write a TCL script to find the factorial of a number using procedure.

Aim: To write a TCL script to find the factorial of a number using procedure.
16
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

Program:
proc factorial {n} {
set i 1; set prod 1
while {$i <= $n} {
set prod [expr $prod*$i]
incr i
}
return $prod
}
puts "factorial is [factorial 4]"

Output:

E:\OneDrive\Desktop\ruby & tcl\TCL>tclsh prog1.tcl


factorial is 24

12. Write a TCL script that multiplies the number from 1 to 10.

Aim: To write a TCL script that multiplies the number from 1 to 10.

17
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

Program:
proc mul {n} {
set i 1
while {$i <= 10} {
set prod [expr $n*$i]
puts "$n x $i=$prod"
incr i
}
}
mul 4
Output:

E:\OneDrive\Desktop\ruby & tcl\TCL>tclsh prog2.tcl


4 x 1=4
4 x 2=8
4 x 3=12
4 x 4=16
4 x 5=20
4 x 6=24
4 x 7=28
4 x 8=32
4 x 9=36
4 x 10=40

13. Write TCL script for sorting a list using comparison function.

Aim: To Write TCL script for sorting a list using comparison function.

Program:
18
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

Output:

14. Write a TCL script to


(i)create a list
(ii)append elements to list
(iii)Traverse the list
(iv)Concatenate the list

19
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

Aim: To write a TCL script to


(i)create a list
(ii)append elements to list
(iii)Traverse the list
(iv)Concatenate the list

Program:
#1. Creatring list

set courseList1 {CSE ECE IT EIE AI ML}


puts $courseList1

#2. Append Elements


set list MECH
append list " " "CHEM"
lappend list "CIVIL"
lappend list "CYBER"
puts $list

#3. Traverse the list


foreach v $list { puts $v}
#foreach and lrange combine traversal of partial list elements.
foreach v [lrange $list 1 3] { puts $v}

#method3

20
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

set x 0
while {$x<[llength $list]} {
puts "$x--[lindex $list $x]"
incr x
}

#4.Concatenate the list

lappend courseList1 {*}$list


puts $courseList1

Output:

E:\OneDrive\Desktop\ruby & tcl\TCL>tclsh prog4.tcl


CSE ECE IT EIE AI ML
MECH CHEM CIVIL CYBER
MECH
CHEM
CIVIL
CYBER
CHEM
CIVIL
21
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

CYBER
0--MECH
1--CHEM
2--CIVIL
3--CYBER
CSE ECE IT EIE AI ML MECH CHEM CIVIL CYBER

15. Write a TCL script to comparing the file modified times.

Aim: To Write a TCL script to comparing the file modified times.

Program:

Output:

22
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

16. Write a TCL script to copy a file and translate to native format.

Aim: To write a TCL script to copy a file and translate to native format.

Program:
Output:

23
SCRIPTING LANGUAGES LAB
KESHAV MEMORIAL INSTITUTE OF TECHNOLOGY

24
SCRIPTING LANGUAGES LAB

You might also like