Ruby ( + Style Guide) Flashcards
PORO
Plain Old Ruby Object
Gem: Dotenv
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.
Restyle this expression:
next if prefix.nil?
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
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
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 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
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
What is the purpose of the method ‘SomeClass.class_eval’ ?
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.
What is the purpose of #module_eval ?
The #module_eval method is just an alias to class_eval so you can use them both for classes and modules.
What is the purpose of #instance_eval ? Where is it called on?
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
Qualified constant vs. relative constant. What is the difference when it comes to resolution (lookup?) of constant?
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.
Resolution algorithm for relative constants?
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.
Define a Proc ‘my_proc’ (asking for the Proc syntax)
my_proc = Proc.new { |test| puts “sdasdsd + #{test}” }
How must a Proc be called?
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)
What’s the difference between a Lambda and a Proc?
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?).
Can a class method be private?
Nope, in needs to be made private with ‘private_class_method’!!
What does the (unary) * operator do in Ruby method arguments such as:
def meth(bla, *args) end
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)