Reference Page: PHP|Python|Perl|Ruby|MatLab

Ruby Links : Learn     Interview Questions     Software IDE Ruby Jobs : Indeed.com     ZipRecruiter.com     Monster.com

Ruby Interview Questions - Page 4

< Previous Page
Question: What is the purpose of 'yield' in Ruby?
Answer: In Ruby 'yield' is used within a method to transfer control from the method to a block passed to it. It allows the method to execute the block at a specific point within its implementation. The method can also pass arguments to the block using yield.

Question: Can you explain the difference between == and === operators in Ruby?
Answer: Please find the difference below:
'==' is the equality operator used to compare two objects for equality.
'===', often referred to as the "case equality" or "threequals" operator, is typically used in case statements and behaves differently depending on the class. In many cases, it's synonymous with '==', but certain classes like Range and Regexp have custom implementations for '==='.

Question: Can you explain the concept of garbage collection in Ruby. How does it work?
Answer: The garbage collection is the process of automatically reclaiming memory occupied by objects that are no longer in use. In Ruby, the garbage collector periodically scans the memory for objects that are no longer referenced by any part of the program and frees up their memory.

Question: What is the purpose of the tap method in Ruby?
Answer: The 'tap' method is used to perform operations on an object within a block and then return the object itself. It's often used for method chaining or debugging.

Question: What is the purpose of the 'initialize' method in Ruby classes?
Answer: The 'initialize' method is a special method in Ruby classes that is automatically called when a new instance of the class is created using the new method. It's used to initialize instance variables and perform any necessary setup for newly created objects.

Question: What is the purpose of the 'respond_to?' method in Ruby? How is it used?
Answer: The 'respond_to?' method is used to determine whether an object responds to a particular method. It takes a method name as an argument and returns true if object can respond to that method, otherwise false. This is often used for conditional method invocation to avoid runtime errors.

Question: Can you explain the purpose of the attr family of methods in Ruby?
Answer: The attr family of methods (such as attr_reader, attr_writer, and attr_accessor) are used to define attribute accessors for a class.
They automatically generate getter and/or setter methods for instance variables, reducing boilerplate code.

Question: What is the purpose of the 'module_function' declaration in Ruby?
Answer: The 'module_function' declaration in Ruby allows methods defined within a module to be called both at the module level (as class methods) and when included in other classes (as instance methods).
It creates copies of the specified methods as private instance methods in the module.

Question: What is a lambda in Ruby and how does it differ from a proc?
Answer: A lambda in Ruby is a type of anonymous function or closure. It's created with the lambda keyword or the -> syntax.
Lambdas enforce strict argument count and return behavior, similar to methods.
Procs, on the other hand, are more flexible and lenient regarding argument counts and returns.

< Previous Page