Ruby Flashcards

1
Q

WHAT ARE RUBY GEMS?

A

A gem is a piece of ruby code packaged as a library so that it can be imported and used by others in their programs.

One of the most common gems that I use is pry-rails for debugging.

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

What is an object? (or what is an instance?)

A

An instance of a class. (or a single and unique unit of a class).

Classes themselves descend from the Object root class.

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

What is a class?

A

Classes are blueprints in which individual instances of a class can be derived from.

Classes hold data, have methods that interact with that data, and are used to instantiate objects.

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

What is a module? Can you tell me the difference between classes and modules?

A

Like a class, a module contains data and may have methods that interact with that data, and can be thought as a library of methods (ex: ruby’s Math module). But modules cannot be instantiated like classes can, nor do they have instance variables like classes do, and cannot be subclassed. Modules can be used across many classes.

Classes are classified by what they are, modules are classified by what they do.

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

Can you tell me the three levels of method access control for classes and modules? What do they imply about the method?

A

All methods, no matter the access control, can be accessed within the class. But what about outside callers?

Public methods enforce no access control – they can be called in any scope.

Protected methods are only accessible to other objects of the same class.

Private methods are only accessible within the context of the current object.

https://gist.github.com/ryansobol/5252653

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

There are three ways to invoke a method in ruby. Can you give me at least two?

A

The dot operator (or period operator), the Object#send method, or method(:foo).call

https://gist.github.com/ryansobol/5252653

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

Explain this ruby idiom: a ||= b

A

a = b when a == false

otherwise a remains unchanged

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

What does self mean?

A
  • at the class level, self is the class.

- at the instance level, self is the instance in context (refers to the current object).

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

What is a Proc?

A

Essentially, Procs are anonymous methods (or nameless functions) containing code. They can be placed inside a variable and passed around like any other object or scalar value. They are created by Proc.new, lambda, and blocks (invoked by the yield keyword).

https://gist.github.com/ryansobol/5252653

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

What is unit testing (in classical terms)? What is the primary technique when writing a test?

A

Unit testing, simply put, is testing methods – the smallest unit in object-oriented programming. Strong candidates will argue that it allows a developer to flesh out their API before it’s consumed by other systems in the application.

The primary way to achieve this is to assert that the actual result of the method matches an expected result.

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

What is your favorite ruby gem?

A

pry-rails. I’m always using this for debugging, and I like how it includes Rails specific methods (such as show model and show routes).

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

What are the 4 types of variables in ruby?

A

Global, local, class, instance

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

What’s the difference between a symbol and string?

A

Strings are mutable and symbols are not.

A symbol will exist for the duration of a session, while a string does not. For ex, if you call object_id on a symbol twice, it will output the same id twice. If you did this on a string, it will be 2 different IDs.

http://anilpunjabi.tumblr.com/post/25948339235/ruby-and-rails-interview-questions-and-answers

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

WHAT ARE CLASS VARIABLES? HOW DO YOU DEFINE THEM?

A

Class variables are created using the @@ prefix to denote the variable as class level. In the case of inheritance it works more like a static variable that is accessed across all variable instances.

http://anilpunjabi.tumblr.com/post/25948339235/ruby-and-rails-interview-questions-and-answers

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

HOW DO YOU DEFINE GLOBAL VARIABLES?

A

Global variables are defined using single $ symbol. It can be declared anywhere and used anywhere.

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

DOES RUBY SUPPORT CONSTRUCTORS? HOW ARE THEY DECLARED?

A

Constructors are supported in Ruby. They are declared as the method ‘initialize’. The initialize method gets called automatically when Album.new is called (when new instance of Album is created).

17
Q

WHAT IS A RANGE?

A

Range is a way to declare continuous variables, like arrays and other types of collections. Ex: (1..4).to_a

18
Q

WHAT IS THE DIFFERENCE BETWEEN ’&&’, ‘AND’ AND ’&’ OPERATORS?

A

The ’&&’ and ‘and’ are both logical and statements. They ’&&’ operator has higher precedence though.

http://anilpunjabi.tumblr.com/post/25948339235/ruby-and-rails-interview-questions-and-answers

19
Q

HOW CAN YOU CREATE SETTER AND GETTER METHODS IN RUBY?

A

The setter and getter methods can be created manually by the developer or it can be auto-generated by Ruby using the attr_accessor method specifier.

20
Q

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

A

In general, methods that end in ! indicate that the method will modify the object it’s called on. Ruby calls these “dangerous methods” because they change state that someone else might have a reference to.

21
Q

DOES RUBY SUPPORT MULTIPLE INHERITANCE?

A

No

22
Q

HOW CAN YOU ACHIEVE THE SAME EFFECT AS MULTIPLE INHERITANCE USING RUBY? WHAT IS MIXIN?

A

Ruby offers an alternative concept called mixin. Modules can be imported inside other class using mixin. They are then mixed-in with the class in which they are imported.

*see mixin_example notes

23
Q

HOW WILL YOU IMPLEMENT A SINGLETON PATTERN?

A

Singleton means single instance. The goal of a singleton pattern is to write a class definition but only allow the creation of the single instance of that object.

You cannot use the Logger.new to create an object instance because this is a singleton object and therefore calling ‘new’ would fail.

http://anilpunjabi.tumblr.com/post/25948339235/ruby-and-rails-interview-questions-and-answers

24
Q

WHAT IS THE PURPOSE OF ENVIRONMENT.RB AND APPLICATION.RB FILE?

A

There are two files where variables and configuration settings are stored.

  • config/environment.rb : Environment settings go here
  • config/application.rb : Application level global settings go here
    http: //anilpunjabi.tumblr.com/post/25948339235/ruby-and-rails-interview-questions-and-answers
25
Q

HOW CAN YOU DEFINE A CONSTANT?

A

Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally. Constants are just like variables but all caps. Constants may not be defined within methods

26
Q

HOW CAN YOU FIRE A METHOD WHEN A MODULE IS INCLUDED INSIDE A CLASS?

A

Import this module inside your class and invoke the method using the “module.method_name” syntax as shown below

require “trig”

class myclass
y = Trig.sin(Trig::PI/4)
27
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. You can make methods private using this declaration within your class.

28
Q

HOW CAN YOU CALL THE BASE CLASS METHOD FROM INSIDE OF ITS OVERRIDDEN METHOD?

A

If you are inside the overridden method in the derived class then a simple call to super will call the right method in the base class.

if you just want to call the base class without calling the derived class then the best way to do that is to simply assign an alias to the parent method like.

http://anilpunjabi.tumblr.com/post/25948339235/ruby-and-rails-interview-questions-and-answers

29
Q

What’s the difference between single and double quotes?

A

Can only interpolate with double quotes.

30
Q

How do you define instance variables?

A

Instance variables begin with @

31
Q

What is the difference between puts and print ?

A

puts appends a new line and outputs each argument to a new line but print doesn’t append anything.

32
Q

What is a Range in Ruby?

A

The first and perhaps most natural use of ranges is to express a sequence. Sequences have a start point, an end point, and a way to produce successive values in the sequence. In Ruby, these sequences are created using the “..” and “…” range operators.

33
Q

What is the Purpose of “!” and “?” at the end of method names?

A

Methods ending in ! perform some permanent or potentially dangerous change.
Methods ending in ? return a boolean.