Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
10 Cards in this Set
- Front
- Back
What would you expect the code below to print out? numbers = [1, 2, 2, 3] numbers.uniq puts numbers |
1 2 2 3 numbers.uniq returned a new Array object with unique elements, but it did not modify the numbers object. Further, the putsmethod automatically calls to_s on its argument, and that’s why you see the output like above. Additional note: had the last line been p numbers instead, the output would have been [1, 2, 2, 3] because the p method automatically calls inspect on its argument, which gives a different formatting on the output. Furthermore, we could have also done puts numbers.inspect and the output would have been the same as using the p method. |
|
Describe the difference between ! and ? in Ruby. And explain what would happen in the following scenarios:
|
Just to clarify, if you see ! or ? at the end of the method, it's actually part of the method name, and not Ruby syntax. Therefore, you really don't know what the method is doing, even if it ends in those characters -- it depends on the method implementation. The other uses are actual Ruby syntax:
|
|
Replace the word "important" with "urgent" in this string: advice = "Few things in life are as important as house training your pet dinosaur." |
advice.gsub!('important', 'urgent') |
|
The Ruby Array class has several methods for removing items from the array. Two of them have very similar names. Let's see how they differ: numbers = [1, 2, 3, 4, 5] What do the following method calls do (assume we reset numbers to the original array between method calls)? numbers.delete_at(1) numbers.delete(1) |
numbers.delete_at(1) # numbers is now [1, 3, 4, 5] (note that the array is operated on directly and the return value of the call is the removed item 2) numbers.delete(1) # numbers is now [2, 3, 4, 5] (note that the array is operated on directly and the return value of the call is the removed item 1) Another thing to notice is that while both of these methods operate on the contents of the referenced array and modify it in place (rather than just returning a modified version of the array) these methods do NOT have the usual ! at the end of the method name for "modify in place" method names. |
|
Programmatically determine if 42 lies between 10 and 100. hint: Use Ruby's range object in your solution. |
(10..100).cover?(42) |
|
Starting with the string: famous_words = "seven years ago..." show two different ways to put the expected "Four score and " in front of it. |
"Four score and " + famous_words famous_words.prepend("Four score and ") "Four score and " << famous_words |
|
This gives us a string that looks like a "recursive" method call: "add_eight(add_eight(add_eight(add_eight(add_eight(number)))))" If we take advantage of Ruby's Kernel#eval method to have it execute this string as if it were a "recursive" method call eval(how_deep) what will be the result? |
42 Note: The Kernel#eval method is a rarely used Ruby method. You're not expected to understand how it works at this stage. |
|
If we build an array like this: flintstones = ["Fred", "Wilma"] flintstones << ["Barney", "Betty"] flintstones << ["BamBam", "Pebbles"] We will end up with this "nested" array: ["Fred", "Wilma", ["Barney", "Betty"], ["BamBam", "Pebbles"]] Make this into an un-nested array. |
flintstones.flatten! |
|
Given the hash below flintstones = { "Fred" => 0, "Wilma" => 1, "Barney" => 2, "Betty" => 3, "BamBam" => 4, "Pebbles" => 5 } Turn this into an array containing only two elements: Barney's name and Barney's number |
flintstones.assoc("Barney") #=> ["Barney", 2] |
|
Given the array below flintstones = ["Fred", "Barney", "Wilma", "Betty", "Pebbles", "BamBam"] Turn this array into a hash where the names are the keys and the values are the positions in the array. |
flintstones_hash = {}flintstones.each_with_index do |name, index| flintstones_hash[name] = indexend |