Showing posts with label ruby programming examples. Show all posts
Showing posts with label ruby programming 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 convert full name into initials



Write a Ruby program to convert full name into initials

Example.

Enter your name
Umesh kumar kushwaha
Initials are U.K.K.


class FullNameToInitials
  def fullNameToInitials
    print "Enter the name: "
    name = gets.chomp
    initial_name = ""
    name.split.each do |n|
      initial_name = initial_name +  n[0].upcase + "."
    end 
    puts "Initial name is: #{initial_name}"
  end
end

p = FullNameToInitials.new 
p.fullNameToInitials

Output:

Enter the name:  Umesh kumar kushwaha
Initial name is: U.K.K.


Ruby program to reverse string without using direct method



Write a Ruby program to reverse string without using direct method

class ReverseString
  def reverseString
    print "Enter the string: "
    input_string = gets.chomp
    reverse_string = ""
    (0..(input_string.length - 1)).reverse_each do |i|
      reverse_string = reverse_string + input_string[i]
    end
    puts "Reverse string is: #{reverse_string}"
  end
end

p = ReverseString.new 
p.reverseString

Output:

Enter the string:  umesh kumar kushwaha
Reverse string is: ahawhsuk ramuk hsemu


Program to find smallest and longest substring of a string in Ruby



Write a program to find smallest and longest substring of a string in Ruby

class FindSmallestAndLongestSubstring
  def findSmallestAndLongestSubstring
    print "Enter the string: "
    input_string = gets.chomp
    all_substrings = input_string.split(" ")
    smallest_string = all_substrings[0]
    longest_string = all_substrings[0]
    all_substrings.each do |s|
      smallest_string = s if smallest_string.length > s.length
      longest_string = s if longest_string.length < s.length
    end 
    puts "Smallest substring: #{smallest_string}"
    puts "Longest substring: #{longest_string}"
  end
end

p = FindSmallestAndLongestSubstring.new 
p.findSmallestAndLongestSubstring

Output:


Enter the string:  umesh kushwaha best ror developer
Smallest substring: ror
Longest substring: developer


Ruby program to search a string in a sentence



Write a Ruby program to search a string in a sentence.

class CountStringInSentence
  def countOccurance
     print "Enter the main sentence: "
     main_string = gets.chomp
     print "Enter the search string: "
     search_string = gets.chomp
     count = 0
     main_string.split(" ").each do |s|
       count = count + 1 if s.casecmp(search_string).zero?
     end
     print "search string '#{search_string}' occured #{count} times"
  end
end

p = CountStringInSentence.new 
p.countOccurance

Output:


Enter the main sentence:  samajh samajh ke samajh ko samjho
Enter the search string:  samajh
search string 'samajh' occured 3 times


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