Ruby Programs with Code and Output
---
Program 1: Add Two Integers
Code:
print "Enter first number: "
a = [Link].to_i
print "Enter second number: "
b = [Link].to_i
sum = a + b
puts "Sum = #{sum}"
Sample Output:
Enter first number: 5
Enter second number: 10
Sum = 15
---
Program 2: Check Leap Year
Code:
print "Enter a year: "
year = [Link].to_i
if (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
puts "#{year} is a Leap Year"
else
puts "#{year} is not a Leap Year"
end
Sample Output:
Enter a year: 2024
2024 is a Leap Year
---
Program 3: If-Else Statement
Code:
print "Enter a number: "
num = [Link].to_i
if num > 0
puts "Positive number"
else
puts "Negative number"
end
Sample Output:
Enter a number: -7
Negative number
---
Program 4: Case Statement with Multiple Values
Code:
print "Enter a day: "
day = [Link]
case day
when "Saturday", "Sunday"
puts "Weekend"
else
puts "Weekday"
end
Sample Output:
Enter a day: Sunday
Weekend
---
Program 5: Case Statement with String
Code:
print "Enter a color: "
color = [Link]
case color
when "red"
puts "Color is Red"
when "blue"
puts "Color is Blue"
else
puts "Unknown Color"
end
Sample Output:
Enter a color: Red
Color is Red
---
Program 6: Case Statement Using Loop
Code:
[Link] do
print "Enter a number (1-3): "
choice = [Link].to_i
case choice
when 1
puts "You entered One"
when 2
puts "You entered Two"
when 3
puts "You entered Three"
else
puts "Invalid number"
end
end
Sample Output:
Enter a number (1-3): 1
You entered One
Enter a number (1-3): 2
You entered Two
Enter a number (1-3): 5
Invalid number
---
Program 7: Print Numbers 1 to 5
Code:
for i in 1..5
puts i
end
Sample Output:
---
Program 8: Create Object of a Class
Code:
class Student
def initialize(name)
@name = name
end
def display
puts "Student Name: #{@name}"
end
end
print "Enter student name: "
name = [Link]
student = [Link](name)
[Link]
Sample Output:
Enter student name: John
Student Name: John
---
Program 9: Class with Data Member Initialization
Code:
class Person
def initialize(name)
@name = name
end
def show
puts "Name: #{@name}"
end
end
print "Enter a name: "
name = [Link]
person = [Link](name)
[Link]
Sample Output:
Enter a name: Alice
Name: Alice
---
Program 10: User-Defined Function (No Arg, No Return)
Code:
def greet
print "Enter your name: "
name = [Link]
puts "Hello, #{name}!"
end
greet
Sample Output:
Enter your name: Sam
Hello, Sam!
---
Program 11: User-Defined Function (No Arg, With Return)
Code:
def get_language
print "Enter your favorite programming language: "
language = [Link]
return language
end
favorite_language = get_language
puts "Your favorite language is #{favorite_language}"
Sample Output:
Enter your favorite programming language: Ruby
Your favorite language is Ruby
---
Program 12: Find Length of a String
Code:
print "Enter a string: "
str = [Link]
puts "Length of string is #{[Link]}"
Sample Output:
Enter a string: Hello World
Length of string is 11
---
Program 13: Create an Array Using new
Code:
print "How many elements you want to enter? "
n = [Link].to_i
arr = [Link](n)
for i in 0...n
print "Enter element #{i+1}: "
arr[i] = [Link]
end
puts "Array elements are: #{arr}"
Sample Output:
How many elements you want to enter? 3
Enter element 1: apple
Enter element 2: banana
Enter element 3: cherry
Array elements are: ["apple", "banana", "cherry"]
---
Program 14: Single Inheritance
Code:
class Animal
def initialize(name)
@name = name
end
end
class Dog < Animal
def bark
puts "#{@name} is barking"
end
end
print "Enter dog's name: "
name = [Link]
dog = [Link](name)
[Link]
Sample Output:
Enter dog's name: Bruno
Bruno is barking
---
Program 15: Get Current Timezone
Code:
require 'time'
time = [Link]
puts "Current Timezone: #{[Link]}"
Sample Output:
Current Timezone: IST
---
Program 16: Write Text into a File
Code:
print "Enter text to write into the file: "
text = [Link]
[Link]("[Link]", "w") do |file|
[Link](text)
end
puts "Text written to [Link] successfully!"
Sample Output:
Enter text to write into the file: Ruby is awesome
Text written to [Link] successfully!
---
Program 17: Create Multiple Threads
Code:
threads = []
[Link] do |i|
threads << [Link] do
print "Enter something for thread #{i+1}: "
input = [Link]
puts "Thread #{i+1} received input: #{input}"
end
end
[Link](&:join)
Sample Output:
Enter something for thread 1: Hello
Thread 1 received input: Hello
Enter something for thread 2: World
Thread 2 received input: World
Enter something for thread 3: Ruby
Thread 3 received input: Ruby