Ques 66. Describe the difference between class and instance variables?
Class variables are created with the prefix ‘@@’ and are shared by all objects in a class.
Instance variables are created with the prefix ‘@’ and belong to a single object within a class.
Is it helpful?
Add CommentView Comments
Ques 67. Example some of the looping structures available in Ruby?
For loop, While loop, Until Loop.
Is it helpful?
Add CommentView Comments
Ques 68. Explain the difference between a has_one and belongs_to association in Ruby on Rails.
has_one: Indicates a direct 1:1 relationship between objects where each instance of a model contains one instance of another model. A product has_one provider, a customer has_one order.
Is it helpful?
Add CommentView Comments
Ques 69. Explain a polymorphic association in Ruby on Rails.
Polymorphic associations allow a model to belong to more than one other model through a single association.
class Picture < ActiveRecord::Base
belongs_to : imageable, polymorphic: true
end
class Employee < ActiveRecord::Base
has_many : pictures, as: : imageable
end
class Product < ActiveRecord::Base
has_many : pictures, as: : imageable
end
Here, the class Picture belongs_to both Employee and Product, but does so through a single association rather than through multiple.
Be sure to know an appropriate situation to create a polymorphic association, such as creating a comment model associated with multiple other models (articles, photos, etc.). The advantage of using polymorphism here is that it allows you to create a single comment model, rather than separate models for each one (PhotoComment model, ArticleComment model, etc.)
Is it helpful?
Add CommentView Comments
Ques 70. What is a Proc?
Procs, short for procedures, act similar to blocks, but can be saved as variables and reused. Think of them as blocks you can call over and over again on multiple arrays.