Tuesday, 9 July 2019

How to find a given number is prime or not in ruby?

def check_prime n
    (2..n-1).each do |no|
         return false if n % no == 0
    end
    true
end

Tuesday, 2 July 2019

How to multiply the array elements in ruby?

a= [1,2,3,4,5,6,7,8]

Scenarios 1:
a.reduce(:*)

Scenarios 2:
b=1

a.each do |e|
  b=b*e
end


Factorial program using ruby

def factorial_no
  yield
end

n=gets.to_i
factorial_no do
  (1..n).reduce(:*)
end


Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...