Ruby is an open-source pure object-oriented programming language developed by Yukihiro Matsumoto. The first version of the language (0.95) was released in 1995, and in 2011, version 1.9.3 was released.

What is the difference between a class and a module?

  • Basically, a class can be instantiated but a module cannot. A module will never be anything other than a library of methods. A class can be so much more — it can hold its state (by keeping track of instance variables) and be duplicated as many times as you want. It’s all about objects.
  • If you need to instantiate something or otherwise have it exist over time, that’s when you need to use a class instead of a module.

How would you declare and use a constructor in Ruby?

  • Constructor without argument.
class Demo

  # constructor
  def initialize
    puts "Welcome to GeeksforGeeks!"
  end
end
# Creating Obejct
Demo.new
  • Constructor with argument.
class Person

# constructor
  def initialize (name)
    puts "Person name is : #{name}."
  end
end
# Creating Obejct
Person.new 'Kishan'

Comment code in ruby

  • Single line comment
# this is a commented line
  • Multi-line comment
=begin
all
text
inside the
block are
commented
=end

#ruby #tips-and-tricks #interview-questions #faq

FAQs on Ruby and What is the difference between a class and a module?
1.30 GEEK