Ruby Programms of all types :: Most Important :: Run Each and Every Program individually

 https://www.includehelp.com/ruby/programs.aspx


Ruby ::


# The Greeter class
class Greeter
  def initialize(name)
    @name = name.capitalize
  end

  def salute
    puts "Hello #{@name}!"
  end
end

# Create a new object
g = Greeter.new("world")

# Output "Hello World!"
g.salute



Ruby programs/examples


Ruby basic programs

  1. Ruby program to print Hello World!

  2. Ruby program to add two integer numbers

  3. Ruby program to find the area of the rectangle

  4. Ruby program to check leap year

  5. Ruby program to print power of a number

  6. Ruby program to print Fibonacci series

  7. Ruby program to check whether the given number is prime or not

  8. Ruby program to find factorial of a given number

  9. Ruby program to find number of characters in a string (string length)

  10. Ruby program to reverse a string

  11. Ruby program to reverse a string | Set 2

  12. Ruby program to count the number of words in a string

  13. Ruby program to print an array as string

  14. Ruby program to check whether a variable is defined or not

  15. Ruby program to check whether the hash contains specified key or not

  16. Ruby program to convert the string into lowercase and uppercase

  17. Ruby program to check whether a string contains a substring or not

  18. Ruby program to generate random numbers

  19. Ruby program to print an array

  20. Ruby program to check whether an element exists in an array or not

  21. Ruby program to calculate the sum of all even numbers

  22. Ruby program to calculate the sum of all odd numbers up to N

  23. Ruby program to count the number of digits in a number

  24. Ruby program to print multiplication table of a number

  25. Ruby program to sort an array with command line arguments


Ruby set programs



Print the elements of a set in Ruby

  1. Find the length of a set in Ruby

  2. Check the presence of an element in the set in Ruby

  3. Join all the elements of two sets in Ruby

  4. Find the common elements from two sets in Ruby

  5. Find the difference between two sets in Ruby

  6. Merge two sets in Ruby

  7. Check whether the set is empty or not in Ruby

  8. Delete and replace an element from the set in Ruby


Ruby hash programs



How to check if a hash key exists in Ruby

How to add elements to a Hash in Ruby?



TOP Interview Coding Problems/Challenges

Run-length encoding (find/print frequency of letters in a string)





Ruby code to print "Hello World!"

=begin 
Ruby program to print Hello World.
=end

puts "Hello World!"
print "Hello World!"
puts "Hello World!"

Output

Hello World!
Hello World!Hello World!



Adding Two Numbers in Ruby::

Methods Used ::

puts :: This method is used to display some message to the user

gets :: This method is used to take input from the user

.to_i :: When we take input through gets method it is a string.for further calculations it is mandatory to convert it into an integer for exact results and this method returns the integer conversion of the given string

Varibale Used ::

num1 :: to store first value

num2 :: to store second value

sum :: to store the sum i.e. result











Progrm ::

=begin

Ruby program to add two numbers

=end

puts “Enter First Value: ”

num1=gets.chomp.to_i

puts “Enter Second Value:”

num2=gets.chomp.to_i

sum=num1+num2

puts “The sum is #{sum}”





Program ::

=begin

Ruby program to find the area of the Rectangle

=end

puts “Enter Length:”

l=gets.chomp.to_f

puts “Enter width:”

w=gets.chomp.to_f

area=l*w

puts “Area of Rectangle is #{area}”




Comments

Popular posts from this blog

Rails 7 Features :: Comparison with Rails 6 and Rails 5