0% found this document useful (0 votes)
6 views12 pages

Ruby Programs With Output

The document contains a series of Ruby programs demonstrating various programming concepts such as addition, leap year checking, conditional statements, case statements, class creation, user-defined functions, and file handling. Each program includes code snippets along with sample outputs to illustrate their functionality. The programs cover a range of topics from basic input/output to object-oriented programming and multithreading.

Uploaded by

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

Ruby Programs With Output

The document contains a series of Ruby programs demonstrating various programming concepts such as addition, leap year checking, conditional statements, case statements, class creation, user-defined functions, and file handling. Each program includes code snippets along with sample outputs to illustrate their functionality. The programs cover a range of topics from basic input/output to object-oriented programming and multithreading.

Uploaded by

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

Ruby Programs with Code and Output

---

Program 1: Add Two Integers

Code:

print "Enter first number: "

a = gets.chomp.to_i

print "Enter second number: "

b = gets.chomp.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 = gets.chomp.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 = gets.chomp.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 = gets.chomp

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 = gets.chomp.downcase

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:

3.times do

print "Enter a number (1-3): "

choice = gets.chomp.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 = gets.chomp

student = Student.new(name)

student.display

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 = gets.chomp

person = Person.new(name)

person.show

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 = gets.chomp

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 = gets.chomp

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 = gets.chomp

puts "Length of string is #{str.length}"

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 = gets.chomp.to_i

arr = Array.new(n)

for i in 0...n

print "Enter element #{i+1}: "

arr[i] = gets.chomp

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 = gets.chomp

dog = Dog.new(name)

dog.bark

Sample Output:

Enter dog's name: Bruno

Bruno is barking

---

Program 15: Get Current Timezone

Code:
require 'time'

time = Time.now

puts "Current Timezone: #{time.zone}"

Sample Output:

Current Timezone: IST

---

Program 16: Write Text into a File

Code:

print "Enter text to write into the file: "

text = gets.chomp

File.open("output.txt", "w") do |file|

file.write(text)

end

puts "Text written to output.txt successfully!"

Sample Output:

Enter text to write into the file: Ruby is awesome

Text written to output.txt successfully!

---
Program 17: Create Multiple Threads

Code:

threads = []

3.times do |i|

threads << Thread.new do

print "Enter something for thread #{i+1}: "

input = gets.chomp

puts "Thread #{i+1} received input: #{input}"

end

end

threads.each(&: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

You might also like