Ruby ( + Style Guide) Flashcards

1
Q

PORO

A

Plain Old Ruby Object

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

Gem: Dotenv

A

Shim to load environment variables from .env into ENV in development.

Github: https://github.com/bkeepers/dotenv
(First) used in: Realgestalt Skript (Konstantin)

Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.

But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. Dotenv loads variables from a .env file into ENV when the environment is bootstrapped.

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

Restyle this expression:

next if prefix.nil?

A

next unless prefix

Explanation:
=> Don’t do explicit non-nil checks unless you’re dealing with boolean values. [link]

# bad
do_something if !something.nil?
do_something if something != nil
# good
do_something if something
# good - dealing with a boolean
def value_set?
  !@some_boolean.nil?
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When checking an attribute’s value of contacts in the Google Contacts API, why is this expression error-prone (given that the attribute’s value is saved in “prefix_string” and supposed to be “Herr” or “Frau”)?
Give a better solution! :

if [“Herr”, “Frau”].include? prefix_string

A

Because independent from what it is supposed to be, at least the most likely deviations should and can be easily be taken into account. Like preceding and subsequent spaces (“ “) and wrong capitalization like “FRau”.

if [“herr”, “frau”].include? prefix_string.strip.downcase

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

How can this code be made more efficient and readable?

  title_string = contact.xpath("xmlns:title").first.content
  if title_string.include?(prefix_string)
    title_string.gsub!(prefix_string,"").gsub!(/^(\s)+/,"")
    # write new title
    contact.xpath("xmlns:title").first.content = title_string
  end
A

The code repetition shows, that
contact.xpath(“xmlns:title”) should be held in a variable as well, not just the content. Also the “gsub” method is not the best option here, but a combination thereof with “lstrip”:

  title_node = contact.xpath('xmlns:title').first
  title_string = title_node.content

  if title_string.include? prefix_string
    title_node.content =title_string.gsub(prefix_string, '').lstrip
  end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of the method ‘SomeClass.class_eval’ ?

A

With the class_eval you’re adding a method to a pre-existing class. If a class called SomeClass is not defined before our ‘class_eval’ runs you’d see an “NameError: uninitialized constant SomeClass”. A ‘class_eval’ call opens up an existing class for you, it won’t create or open a class that doesn’t exist yet.

Wrapping up, you can use class_eval to open classes (and modules) and add real code on it as you also can just pass a string containing valid Ruby code and it’s going to be ‘evaled as it was at the class definition body.

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

What is the purpose of #module_eval ?

A

The #module_eval method is just an alias to class_eval so you can use them both for classes and modules.

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

What is the purpose of #instance_eval ? Where is it called on?

A

The instance_eval method works just like class_eval but it will add the behavior you’re trying to define, to the object instance where it was called. Therefore it is called on an instance like: @some_instance.instance_eval

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

Qualified constant vs. relative constant. What is the difference when it comes to resolution (lookup?) of constant?

A

Qualified constants look like this: Billing::Invoice. Billing::Invoice is composed of two constants: Billing is relative and is resolved using the algorithm for relative constants.

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

Resolution algorithm for relative constants?

A

At any given place in the code, let’s define cref to be the first element of the nesting if it is not empty, or Object otherwise.

Without getting too much into the details, the resolution algorithm for relative constant references goes like this:

If the nesting is not empty the constant is looked up in its elements and in order. The ancestors of those elements are ignored.

If not found, then the algorithm walks up the ancestor chain of the cref.

If not found and the cref is a module, the constant is looked up in Object.

If not found, const_missing is invoked on the cref. The default implementation of const_missing raises NameError, but it can be overridden.

Rails autoloading does not emulate this algorithm, but its starting point is the name of the constant to be autoloaded, and the cref.

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

Define a Proc ‘my_proc’ (asking for the Proc syntax)

A

my_proc = Proc.new { |test| puts “sdasdsd + #{test}” }

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

How must a Proc be called?

A

Where a block is expected one can use a Proc instead by using the Proc’s name and a prepending &. Like so:
[1,2,3].each(&my_proc)

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

What’s the difference between a Lambda and a Proc?

A

First: A lambda can be exited with a return statement whereas “returning” from a Proc would exit the Proc’s enclosing structure/closure.
Second: A lambda is more strict about its inputs. Not passing in the correct number of arguments will result in an error whereas a Proc will disregard arguments that are too much(is that true?).

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

Can a class method be private?

A

Nope, in needs to be made private with ‘private_class_method’!!

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

What does the (unary) * operator do in Ruby method arguments such as:

def meth(bla, *args)
end
A

Splat! is the star (*) operator, typically used in Ruby for defining methods that take an unlimited number of arguments.

It can also be used to convert an array to the multiple-argument form when invoking a function:

some_ints = [1,2,3]
sprintf(“%%i %%i %%i”, *some_ints)

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

What does the ‘super’ keyword do in a method?

A

It calls a parent method of the same name, with the same arguments. It’s very useful to use for inherited classes. There’s no limit to how many times you can call super, so it’s possible to use it with multiple inherited classes. If there’s no parent method of the same name, however, Ruby raises an exception.