FAQs on Ruby
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!"
endend# Creating Obejct
Demo.new
- Constructor with argument.
class Person# constructor
def initialize (name)
puts "Person name is : #{name}."
endend# 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
How would you create getter and setter methods in Ruby?
Can you access private and protected methods outside of a class?
Difference between the private and protected methods in Ruby?
“In ruby, private methods are not accessible by an explicit receiver.”
To understand this statement, let's look at the next code snippet.
- Explain how (almost) everything is an object in Ruby.
Explain what singleton methods are. What is Eigenclass in Ruby?
Describe the Ruby method lookup path.
A simple question but hard to answer. Why is that hard? Because Ruby has various ways of defining a method and add it to a class:
- Adding it to the singleton class
- Adding it to the class
- Include a module
- Prepend a module
- Extend a module
- Inherit from superclass
Include vs extend
Modules are used
- To write reusable code
- To provide namespacing
Include a module — to pull the methods as instance methods.
Extend a module — to pull the methods as class methods.
Include vs prepend
Extend
When using the extend method instead of include, you are adding the module’s methods as class methods instead of as instance methods.
Require vs load
require reads and parses files only once, when they were referenced for the first time. It keeps track of whether that library was already loaded or not. You also don’t need to specify the “.rb” extension of the library file name.
require 'abc'
load reads and parses files every time you call `load`. Using the load method you must specify the “.rb” extension of the library file name.
load 'abc.rb'
Require vs require_relative
require_relative is just like require, except that instead of searching your Ruby path, it searches relative to the current directory.
What is the difference between block, proc, and lambda?
- Proc and lambda are objects but blocks are not.
Lambda vs Proc
Before I get into the differences between procs and lambdas, it is important to mention that they are both Proc objects.
- Lamdas check the number of arguments, while procs do not
In contrast, procs don’t care if they are passed the wrong number of arguments.
- Lamdas and procs treat the ‘return’ keyword differently
‘return’ inside of a lambda triggers the code right outside of the lambda code
‘return’ inside of a proc triggers the code outside of the method where the proc is being executed
Array Methods
- map || collect
- each
- select
- find || detect
- reject
- inject
map
| collect is the alias of map.
Performs an action on each array element. The original array is not modified. Returns the modified array.
each
Executes an action using as a parameter each element of the array. Returns the unmodified array.
select
Runs an expression for each array element and, if it is true
, that element gets added to the output which is returned. This is called filter in other languages.
find
|detect alias for find
Take an expression and returns the first element for which the expression returns true
:
reject
The opposite of select: runs an expression for each array element and includes that element in the output if the expression is false
inject
| reduce is the alias for inject
Takes an accumulator (sum
) and changes it as many times as there are elements in the array. Returns the final value of the accumulator.
You can also specify an initial value as a parameter before the block.
Difference between size, length, and count.
- count tends to refer to the number of elements in a looser collection.
- length or it’s alias size: tends to refer to contiguous elements — a string has a length for example.
Difference between .blank?, .nil?, .empty?
.blank? is the opposite of .present?. Through .blank? you can check nil as well empty.
Use of dig method.
and Vs &&
Operator ‘=’ has higher precedence than ‘and’ operator. So ‘true’ is assigned to variable ‘a’.
True or False
Loops in Ruby
- for loop
- while loop
- do..while loop
- until loop
for loop
while loop
| Execute as long as the condition is true
.
do-while loop
until loop
| Executes as long as the condition is false
. Or Until loop will continue to run until a certain condition is met.
while vs until
Until loop will continue to run until a certain condition is met.
While loops run as long as the condition is true.
Redo vs retry
Redo
| Redo the current loop
Retry
|Retry the loop from beginning
String vs symbol
A string is a mutable instance of String class and two strings with the same contents are two different objects.
"apple".object_id
=> 70280408292320"apple".object_id
=> 70280408286140
"apple".class
=> String
A symbol is an immutable instance of Symbol class and two symbols with the same contents point to the same object.
x = :aa
y = :aax.object_id
=> 1525788
y.object_id
=> 1525788
Explore more on string and symbol.
"apple".object_id
=> 70280408286140
"apple".class
=> String
Mixins
Ruby does not support multiple inheritances directly and instead uses a facility called mixin. Mixins in Ruby allows modules to access instance methods of another one using include method.
Alias and alias_method
You can only use this method at the class & module level, but not inside instance methods. Click here to explore more.
Tap method
|It just allows you to do something with an object inside of a block, and always have that block return the object itself.
Zip array method
| Combine two array into one hash.
Sample array method
| Returns a random element or n random elements from the array.
Thank you for reading.