Ruby Interview Questions and Answers
Question - 41 : - Explain Ruby class.
Answer - 41 : -
Each Ruby class is an instance of Ruby class. Classes in Ruby are first class objects. It always starts with a keyword class followed by the class name.
Syntax:
class ClassName
codes...
end
Question - 42 : - Define Ruby methods.
Answer - 42 : -
Ruby method prevent us from writing the same code in a program again and again. Ruby methods are similar to functions in other languages.
Question - 43 : - How to use Ruby methods.
Answer - 43 : -
To use a Ruby method, we need to first define it. It is defined with def and end keyword.
Method name should always start with a lowercase letter.
Syntax:
def methodName
code...
end
Question - 44 : - What are Ruby blocks.
Answer - 44 : -
Ruby code blocks are called closures in other programming languages. It consist of a group of codes which is always enclosed with braces or written between do...end.
Question - 45 : - What Is The Difference Between A Class And A Module?
Answer - 45 : -
A module cannot be subclassed or instantiated, and modules can implement mixins.
Question - 46 : - Explain Some Of The Looping Structures Available In Ruby?
Answer - 46 : -
For loop, While loop, Until Loop.
Question - 47 : - What Are The Three Levels Of Method Access Control For Classes And What Do They Signify? What Do They Imply About The Method?
Answer - 47 : -
Public, protected, and private.
- Public methods can be called by all objects and subclasses of the class in which they are defined in.
- Protected methods are only accessible to objects within the same class.
- Private methods are only accessible within the same instance.
Question - 48 : - How Does A Symbol Differ From A String?
Answer - 48 : -
symbols are immutable and reusable, retaining the same object_id.
Be prepared to discuss the benefits of using symbols vs. strings, the effect on memory usage, and in which situations you would use one over the other.
Question - 49 : - How Would You Create Getter And Setter Methods In Ruby?
Answer - 49 : -
Setter and getter methods in Ruby are generated with the attr_accessor method. attr_accessor is used to generate instance variables for data that’s not stored in your database column.
You can also take the long route and create them manually.
Question - 50 : - Mention What Is The Command To Create A Migration?
Answer - 50 : -
To create migration command includes
C:rubyapplication>ruby script/generate migration table_name