Intro to Ruby Flashcards
What’s an exception to the ‘everything is an object’ rule in Ruby?
Blocks are not objects, and this is one of the very few exceptions to the “everything is an object” rule in Ruby.
Because of this, blocks can’t be saved to variables and don’t have all the powers and abilities of a real object. For that, we’ll need… procs!
What is a Proc? And how do you define one?
After you’ve defined it, how do you use it?
A Proc is essentially a block that has been assigned to a variable.
To define a Proc, you can do the following:
variable_name = Proc.new {|x| x*2}
You can then pass this Proc to a method, but you’ll need to add an & to convert it back to a block before the method will accept it. For example:
other_variable.collect(&variable_name)
What’s the difference between a PROC and a lambda?
First, a lambda checks the number of arguments passed to it, while a proc does not. This means that a lambda will throw an error if you pass it the wrong number of arguments, whereas a proc will ignore unexpected arguments and assign nil to any that are missing.
Second, when a lambda returns, it passes control back to the calling method; when a proc returns, it does so immediately, without going back to the calling method.
def batman_ironman_proc victor = Proc.new { return "Batman will win!" } victor.call "Iron Man will win!" end
puts batman_ironman_proc
def batman_ironman_lambda victor = lambda { return "Batman will win!" } victor.call "Iron Man will win!" end
puts batman_ironman_lambda
See how the proc says Batman will win? This is because it returns immediately, without going back to the batman_ironman_proc method.
Our lambda, however, goes back into the method after being called, so the method returns the last code it evaluates: “Iron Man will win!”
What’s the difference between a Proc, A Lambda and a block?
A block is just a bit of code between do..end or {}. It’s not an object on its own, but it can be passed to methods like .each or .select.
A proc is a saved block we can use over and over. A lambda is just like a proc, only it cares about the number of arguments it gets and it returns to its calling method rather than returning immediately.
There are obviously lots of cases in which blocks, procs, and lambdas can do similar work, but the exact circumstances of your program will help you decide which one you want to use.
What does ‘puts’ actually do?
Before ‘puts’ tries to write out an object, it uses the to_s to get the string version of that object. In fact, the s in puts stands for string; puts really means ‘put string’
What does ‘chomp’ actually do?
It takes off any trailing ‘enter’ characters hanging out at the end of the string you enter.
What are the 2 different classes of integers in Ruby?
Fixnum and Bignum
Really big integers are BigNum, and smaller ones are FixNum.
Most programming languages don’t have the concept of a Bignum
Why does ‘pig’ * 5 work but 5 * ‘pig’ does not?
in ‘pig’ * 5, you’re asking pig to do the multiplying. ‘pig’ knows how to make 5 copies of itself.
However, 5 doesn’t know how to make ‘pig’ copies of itself!
Do methods like .upcase or .capitalize change the variable they’re called upon?
No - the original variable remains unchanged.
What’s the difference between using single quotes or double quotes in ruby?
With double quotes you can easily do string interpolation using the #{variable_name} way
What’s a ruby symbol? How do you use it?
Ruby symbols are created by placing a colon (:) before a word.
Examples of symbols
:name
:a_symbol
:“surprisingly, this is also a symbol”
Basically, a symbol is used when you want to reference something like a string but don’t ever intend to print it to the screen or change it. It is often referred to as an immutable (i.e. unchangeable) string.
What is nil? What’s it used for?
In programming, we need a way to express “nothing”, and in Ruby, we do this through something called nil. A variable with a value of nil could be described as having ‘nothing’ or being ‘completely empty’, or even just simply ‘not any specific type’. A situation where this may occur is where output is expected but none is returned, such as:
irb :001 > puts “Hello, World!”
Hello, World!
=> nil
The puts method prints out a string and returns nothing, so we see nil being returned after the string is displayed.
When doing string comparison, what do you need to watch out for?
What will ‘bug lady’ < ‘Xander’ return, true or false?
It will return false.
When Ruby compares strings, it does so on their lexicographical ordering - but it goes over the capital letters first before moving on to the other letters.
So if you wanna do a proper comparison, you have to compare the .downcase version of each word.
What is a clasS?
A class is a another word for a ‘kind’ of object. Examples are strings, integers, floats, arrays, hashes, etc. They are always capitalized (String, Integer, Float, etc)
What is the difference between new and initialize
The methods new and initialize work hand in hand.
You use new to create a new object, and initialize is called automatically (if you’ve defined it in your class).
First, new is a method of the class, while initialize is a method of the instance. That means that new needs to be called first. Otherwise, there is no instance to initialize upon.
Second, you define initialize in your class, but you don’t need to define new (it’s already built into to all the classes). Conversly, you call new to create an object, but you never call initialize (the method new already takes care of that for you).
The reason for having these 2 methods is that you really need one of them to be a class method and the other one to be an instance method.