Showing posts with label ruby on rails examples. Show all posts
Showing posts with label ruby on rails examples. Show all posts

Ruby Code Examples



Here I will provide Ruby Code Examples for all basic ruby programs of String, Array, Logical programs and Number based programs. In Ruby programming language.

Array Based Logical Programs


Ruby program to remove duplicate values in array

Program to find the second smallest number in an array in ruby

Find second largest number from an array in Ruby

Find smallest element in array without using min function in Ruby

Find largest element in array without using max function in Ruby

String Based Logical Programs

Ruby program to remove duplicate characters of a string

Ruby program to find occurrence of character in string

Find smallest and longest substring of a string in Ruby

Ruby program to reverse string without using direct method

Ruby program to convert full name into initials

Ruby program to remove common characters from two strings

Ruby program to remove vowels from string

Ruby program to search a string in a sentence

Number Based Logical Programs

Strong number program in Ruby


Prime number program in Ruby

Write a Ruby program to check the armstrong number

Ruby program for random number between two numbers

Ruby program to check Palindrome Number

Ruby program to reverse a number

Ruby program to sum of digits of a number

Ruby program to calculate generic root

Write a Ruby program to swap two arrays

- Ruby program to split number into digits

- Ruby program to print perfect numbers from 1 to 1000

- Ruby program to check perfect number


Ruby program to remove duplicate values in array



Write a ruby program to remove duplicate values in array

class DeleteDuplicateElement
  def deleteDuplicateElement
    print "Enter the size of array"
    size = gets.chomp.to_i
    input_array = []
    print "Enter the elements of array"
    while size > 0 do 
      input_array.push(gets.chomp)
      size = size - 1 
    end
    print "Entered array is: #{input_array}"
    input_array.uniq!
    print "\nUniq array is: #{input_array}"
  end
end

p = DeleteDuplicateElement.new 
p.deleteDuplicateElement

Output:


Enter the size of array 5
Enter the elements of array 22
 33
 22
 11
 55
Entered array is: ["22", "33", "22", "11", "55"]
Uniq array is: ["22", "33", "11", "55"]


Program to find the second smallest number in an array in ruby



Write a program to find the second smallest number in an array in ruby

class SecondSmallest
  def findSecondSmallest
    print "Enter the size of array"
    size = gets.chomp.to_i
    input_array = []
    print "Enter the elements of array"
    while size > 0 do 
      input_array.push(gets.chomp.to_i)
      size = size - 1 
    end
    second_smallest = input_array.sort[1]
    print "Entered array is: #{input_array}"
    print "\nSecond smallest element is: #{second_smallest}"
  end
end

p = SecondSmallest.new 
p.findSecondSmallest

Output:


Enter the size of array 5
Enter the elements of array 88
 44
 22
 11
 99
Entered array is: [88, 44, 22, 11, 99]
Second smallest element is: 22


Ruby program to find occurrence of character in string



Ruby program to find occurrence of character in string

class CountCharOccurrence
  def countCharOccurrence
    print "Enter the string"
    input_string = gets.chomp.downcase
    print "Enter the search character"
    search = gets.chomp.downcase
    count = 0 
    input_string.split("").each do |s|
      count = count + 1 if s == search
    end
    print "character #{search} occurrerd #{count} times"
  end
end

p = CountCharOccurrence.new 
p.countCharOccurrence

Output:


Enter the string Umesh kumar kushwaha
Enter the search character u
character u occurrerd 3 times


Prime number program in Ruby



Write a prime number program in Ruby

Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.

class PrimeNumber
  def checkPrime
     print "Enter the number: "
     num = gets.chomp.to_i
     check = 0 
     if (num == 0) || (num == 1)
       check = 1 
     else 
       (2..(num - 1)).each do |i|
         if (num % i == 0) && (i != num)
           check = 1 
           break
         end
       end
     end
    if check == 0  
      print "#{num} is a prime number"
    else
      print "#{num} is not a prime number"
    end 
  end
end

p = PrimeNumber.new 
p.checkPrime

Output:

Enter the number:  13
13 is a prime number


Ruby program to check Palindrome Number



Write a Ruby program to check Palindrome Number

Palindrome Number: If the number is equal to it's reversed number, then the given number is a palindrome number. For example: 121 is a palindrome number while 122 is not.

class PalindromeNumber
  def checkPalindrom
     print "Enter the number: "
     input_num = gets.chomp.to_i
     num = input_num
     new_num = 0 
     while num > 0 do
       rem = num % 10 
       num = num / 10 
       new_num = (new_num * 10) + rem 
     end
     if input_num == new_num
       print "#{input_num} is a palindrome number"
     else 
       print "#{input_num} is not a palindrome number"
     end 
  end
end

p = PalindromeNumber.new 
p.checkPalindrom

Output:

Enter the number:  1221
1221 is a palindrome number


Ruby program to check the armstrong number



Write a Ruby program to check the armstrong number

Example of armstrong number:
Input - 153
Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no.


class ArmstrongNumber
  def checkArmstrong
    print "Enter the number: "
    input_num = gets.chomp.to_i
    num = input_num
    check = 0 
    while (num > 0) do 
      rem = num % 10 
      num = num / 10 
      check = check + (rem * rem * rem)
    end
    if check == input_num
      print "#{input_num} is an armstrong number"
    else
      print "#{input_num} is not an armstrong number"
    end 
  end
end

p = ArmstrongNumber.new 
p.checkArmstrong

Output:

Enter the number:  371
371 is an armstrong number


Ruby program to reverse a number



Write a Ruby program to reverse a number


class ReverseNumber
  def checkReverse
     print "Enter the number: "
     input_num = gets.chomp.to_i
     num = input_num
     new_num = 0 
     while num > 0 do
       rem = num % 10 
       num = num / 10 
       new_num = (new_num * 10) + rem 
     end
      print "Reverse of #{input_num} is #{new_num}"
  end
end

p = ReverseNumber.new
p.checkReverse

----------------------------------------------------------------------------

class ReverseNumber
  def doReverse
    print "Enter the number: "
    num = gets.chomp
    rev = num.reverse.to_i
    print "Reverse of number is #{rev}"
  end
end

p = ReverseNumber.new 
p.doReverse

Output:


Enter the number:  852
Reverse of number is 258


Ruby program to sum of digits of a number



Write a Ruby program to sum of digits of a number

class SumNumberDigits
  def getSumOfDigits
    sum = 0 
    rem = 0 
    print "Enter the number: "
    num = gets.chomp.to_i 
    while num !=0 do
      rem = num % 10 
      num = num / 10 
      sum += rem 
    end
    print "Sum of digits of number is :" + sum.to_s
  end
end

p = SumNumberDigits.new 
p.getSumOfDigits

Output:


Enter the number:  12345
Sum of digits of number is :15


Ruby program to calculate generic root



Write a Ruby program to calculate generic root.

Generic Root: It sum of digits of a number unit we don't get a single digit.
Example:

Generic root of 4563: 4+5+6+3 = 18 since 18 is two digit numbers so 1 + 8 = 9
So, generic root of 4563 = 9


class GenericRoot
  def getGenericRoot
    sum = 0 
    rem = 0 
    print "Enter the number: "
    num = gets.chomp.to_i 
    while num >= 10 do
      sum = 0 
      while num != 0 do
        rem = num % 10 
        num = num / 10 
        sum += rem 
      end 
      (sum >= 10) ? (num = sum) : break
    end
    print "Generic root of given number is :" + sum.to_s
  end
end

p = GenericRoot.new 
p.getGenericRoot

Output:

Enter the number:  123456
Generic root of given number is :3


Ruby program to swap two arrays



Write a Ruby program to swap two arrays

class SwappingArray

  def doSwap
    a = []
    b = []
    c = []
    puts "Enter the first array values "
    (0..4).each do |i|
      a[i] = gets.chomp
    end
    puts "Enter the Second array values "
    (0..4).each do |i|
      b[i] = gets.chomp
    end
    puts "Array before swapping"
    puts "Array first: "+ a.to_s
    puts "Array second: "+ b.to_s
    c = a 
    a = b 
    b = c 
    puts "Array after swapping"
    puts "Array first: "+ a.to_s
    puts "Array second: "+ b.to_s
  end
end

p = SwappingArray.new 

p.doSwap

Output:



Enter the first array values 
 1
 2
 3
 4
 5
Enter the Second array values 
 11
 22
 33
 44
 55
Array before swapping
Array first: ["1", "2", "3", "4", "5"]
Array second: ["11", "22", "33", "44", "55"]
Array after swapping
Array first: ["11", "22", "33", "44", "55"]
Array second: ["1", "2", "3", "4", "5"]


Ruby program to split number into digits



Write a Ruby program to split number into digits

class NumberToDigits
  def printNumberToDigit
    print "Enter the number "
    num = gets.chomp
    print "Digits are: "
    num.split("").each do |n|
      print n+ " "
    end
  end
end

p = NumberToDigit.new 
p.printNumberToDigits

Output:


  
Enter the number  589
Digits are: 5 8 9 


Ruby program to print perfect numbers from 1 to 1000



Write Ruby program to print perfect numbers from 1 to 1000

Perfect Number:  a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.

class PerfectNumber
  def printPerfectNumbers
    i=1
    sum=0
    (1..1000).each do |n|
      i = 1
      sum = 0
      while i < n do 
        sum = (sum + i) if n % i == 0
        i = i + 1
      end
      puts n if sum == n
    end
  end
end

p = PerfectNumber.new 
p.printPerfectNumbers

Output:

  
6
28
496


Ruby program to check perfect number



Write a Ruby program to check perfect number

Perfect Number:  a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.

class PerfectNumber
  def checkPerfectNumber
    i=1
    sum=0
    puts "Enter the number"
    n = gets.chomp.to_i
    while i < n do 
      sum = (sum + i) if n % i == 0
      i = i + 1
    end
    if sum == n
      puts "Given number is perfect number"
    else
      puts "Given number is not a perfect number"
    end
  end
end

p = PerfectNumber.new 
p.checkPerfectNumber


Output:


Enter the number
 6
Given number is perfect number
=> ni