Interview questions Flashcards
Ruby Gem
Is ruby code packaged as a library so that it can be imported and used by others in their programs.
What is the difference between a symbol and string?
Symbols are immutable. Symbols live duration of session. Strings are sent to garbage collection.
What is the purpose of Yield?
The interpreter essentially invokes a separate piece of code and places it in the location
How do you declare a instance variable?
Instance variables are defined using single @ symbol.
@foo = "Hello" Within a class they can be declared as below:
class Animal
attr_accessor :name, :age
end
Does Ruby support constructors? How are they declared?
Constructors are supported in Ruby. They are declared as the method initialize, shown below. The initialize method gets called automatically when Album.new is called.
class Album def initialize(name, artist) @name = name @artist = artist end end
WHAT IS A RANGE?
Range is a great way to declare continuous variables. You should use it to declare arrays and other types of collections.
range1 = (1..4).to_a
=> [1, 2, 3, 4]
HOW CAN YOU CREATE SETTER AND GETTER METHODS IN RUBY?
class Animal attr_accessor :name, :age end anim = Animal.new => # anim.age = 3 => 3 anim.name = "Steve" => "Steve" puts anim.name, anim.age Steve 3
class Animal def name # this is the getter method @name end def name=(name) # this is the setter method @name = name end #...same for age... end
WHAT IS THE CONVENTION FOR USING ‘!’ AT THE END OF A METHOD NAME?
The ! indicates that the method is about to change the object itself.
WHAT IS A MODULE?
A module is like a class. Except that it can’t be instantiated or subclassed.
module Employee
..variables.
…methods
end
There are two typical uses of modules:
One is to collect related methods and constants in a central location.
Another use of modules is called mixin. Ruby purposely does not implement true multiple inheritance, but the mixin technique is a good alternative.
DOES RUBY SUPPORT MULTIPLE INHERITANCE?
Ruby does not support multiple inheritance.
WHAT IS MIXIN?
Modules can be imported inside other class using mixin. They are then mixed-in with the class in which they are imported. Here’s an example:
module Debug def whoAmI? "I am #{self.to_s}" end end
class Photo
include Debug
end
ph = Photo.new
“I am : #”
WHAT IS THE DEFAULT ACCESS MODIFIER (PUBLIC/PROTECTED/PRIVATE) FOR A METHOD?
By default all methods are public, except the initialize(constructor) method.
If private is invoked without arguments, it sets access to private for all subsequent methods. Can only be accessed within the class itself.
if protected invoked, it can be accessed within the same file and subclasses.
What are Blocks?
A block is that it is a piece of code that can’t be stored in a variable and isn’t an object. It is, as a consequence, significantly faster than a lambda, but not as versatile and also one of the rare instances where Ruby’s “everything is an object” rule is broken.
What are Procs?
Procs are identical to blocks but you can store them in variables, which lets you pass them into functions as explicit arguments and save them for later. Used explicitly sometimes.
What are lambadas?
A lambda is a piece of code that you can store in a variable, and is an Object.