Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Ruby%20On%20Rails%20Interview%20Questions%20and%20Answers

Question: How would you declare and use a constructor in Ruby?
Answer: Constructors are declared via the initialize method and get called when you call on a new object to be created.
Using the code snippet below, calling Order.new acts as a constructor for an object of the class Order.

class Order
  def initialize(customer, meal, beverage)
    @customer = customer
    @meal = meal
    @beverage = beverage
  end
end
Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook