This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vehicle | |
def getdata | |
p "Enter the name of the vehicle" | |
@name = gets.chomp | |
p "Enter the price of the vehicle" | |
@price = gets.chomp | |
end | |
def showdata | |
p "Name of vehicle: #{@name}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def factorial(num) | |
p (1..num).inject(:*) || 1 | |
end | |
p "Factorial Using Ranges" | |
print "Enter the number: " | |
num = gets.to_i | |
print "Factorial is: " | |
factorial(num) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reverse(string) | |
arr = string.split | |
arr.reverse! | |
p arr.join(" ") | |
end | |
p "Enter the string" | |
string = gets.chomp | |
reverse(string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p "Enter the string" | |
line = gets.chomp | |
arr = line.split(//) | |
lower = 0 | |
upper = 0 | |
num = 0 | |
other = -1 | |
for i in 0..arr.size | |
case arr[i] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p "Enter the string" | |
line = gets.chomp | |
p "Enter pattern to be searched" | |
pattern = gets.chomp | |
p matchfound = line.gsub(/#{pattern}/,"(#{pattern})") | |
print "Number of occurences- " | |
p line.scan(/#{pattern}/).size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def power(x) | |
@x = x | |
@f= [] | |
self.each do |i| | |
b = i | |
c = 1 | |
@x.times{ c = c * b } | |
@f << c | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prime(n) | |
prime_no = [] | |
a =0 | |
if n==2 | |
print "#{n} is prime no" | |
end | |
prime_no << 2 | |
3.step(n,1) do |num| | |
2.step(num/2,1) do |k| | |
if num % k == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fact(n) | |
if n == 0 | |
1 | |
else | |
n * fact(n-1) | |
end | |
end | |
def pascal_row(n) | |
for k in 0..n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def to_s | |
puts "---Swapping the case of string---" | |
p self.swapcase | |
end | |
end | |
p "Enter the string" | |
line = gets.chomp | |
line.to_s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Question- Fibonacci - Yield | |
#Fibonacci Series upto 1000 using 'yield'. | |
def fibonacci_series(number) | |
@first_number = 0 | |
@second_number = 1 | |
while @first_number <= number | |
yield(@first_number, @second_number) | |
end |