Interview questions Flashcards

1
Q

Ruby Gem

A

Is ruby code packaged as a library so that it can be imported and used by others in their programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between a symbol and string?

A

Symbols are immutable. Symbols live duration of session. Strings are sent to garbage collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of Yield?

A

The interpreter essentially invokes a separate piece of code and places it in the location

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you declare a instance variable?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Does Ruby support constructors? How are they declared?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

WHAT IS A RANGE?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

HOW CAN YOU CREATE SETTER AND GETTER METHODS IN RUBY?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

WHAT IS THE CONVENTION FOR USING ‘!’ AT THE END OF A METHOD NAME?

A

The ! indicates that the method is about to change the object itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

WHAT IS A MODULE?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

DOES RUBY SUPPORT MULTIPLE INHERITANCE?

A

Ruby does not support multiple inheritance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

WHAT IS MIXIN?

A
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 : #”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

WHAT IS THE DEFAULT ACCESS MODIFIER (PUBLIC/PROTECTED/PRIVATE) FOR A METHOD?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are Blocks?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are Procs?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are lambadas?

A

A lambda is a piece of code that you can store in a variable, and is an Object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly