• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/47

Click to flip

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;

47 Cards in this Set

  • Front
  • Back

For nerd points, answer any of the following:



Who created Ruby?


Where (in what country) was Ruby created? When?


Who? Matz (Yukihiro Matsumoto)


Where? Japan


When? The 90s (specifically, 1994)

What does REPL stand for?



And what does a REPL do?

Read, Evaluate, Print, Loop



A REPL takes user input (read), evaluates it (eval), returns a result to the user (print), then asks for another input (loop).

Ruby variables assign names to pieces of data.


A variable can consist of which types of characters?

Letters, numbers or underscores.



Ruby variables must also be lowercase and have no spaces. They cannot start with a number and cannot include special characters like $, @ or &. Best practice is to use snakecase.


Examples: count, first_lesson, students_in_class

Assign the number 5 to the variable x.

x = 5

What are the two basic types of numbers in Ruby?

Integers (Fixnum) and decimals (Floats).

What are surrounded by quotes and used to store collections of letters and numbers?

Strings.



FYI, the shortest possible string is called the empty string: ""

"Hello world!"



What are the index locations for the substring "Hello"?

[0,1,2,3,4]



The characters in a string each have a position number called the index location. The first index location starts with zero.

One common string method is .length, which counts how many characters are in a string -- does that character count include spaces?

Yes, .length tells you how many characters are in the string, including spaces.


Explain the difference between these two versions of .split, one of the common string methods:



.split


.split(argument)


.split breaks up the string wherever it encounters a space (" ") character.



If you want to split on a character other than space (like a comma), you must use an argument. ex: .split(",")

Both the common string methods .sub and .gsub can be used to replace parts of a string, like using "Find & Replace" in a word processor. How many arguments do these string methods need to be successful?

Two. For both sub (substitute) and .gsub (global substitute) you’ll need to specify two arguments: first the substring you’re wanting to replace and second the string you want to replace it with.


ex. greeting = "Hello Everyone!"


greeting.gsub("Everyone!","Friends!")

Define concatenate.



Cite an example of when something is concatenated in Ruby.

con·cat·e·nate


to link (things) together in a chain or series.



Ex. string concatenation connecting strings and a variable (name):


name = "Frank"


puts "Good morning, " + name + "!"

Define interpolate.



Cite an example of when something is interpolated in Ruby.

in·ter·po·late


to insert (something) between fixed points.



Ex. string interpolation (which only works on double quoted strings) inserting data into a string using the interpolation marker #{}:


name = "Frank"


puts "Good morning, #{name}!"


Note: Anything inside those brackets will be converted into a string.

Using the following variables, write a puts statement that prints "I am very very very excited for today!" -- use string interpolation with code/calculation inside the interpolation marker brackets:



modifier = "very "


mood = "excited"


Possible answers:


puts "I am #{modifier * 3 + mood} for today!"


puts "I am #{modifier * 3}#{mood} for today!"



The snippet between the brackets is evaluated first, then the result is injected into the outer string. Note: you need a space in "very "...

In the following code, capitalize is a ______ called on the _____ named name and assigned to the ______ named text.



text = "#{name.capitalize} has a dog."

text = "#{name.capitalize} has a dog."



Answer: capitalize is a method called on the object named name and assigned to the variable named text.

In the following code, center is a ______ that takes two _____.



puts text.center(50, '*')

puts text.center(50, '*')



Answer: center is a method that takes two parameters.

What objects start with a colon, represent names and strings, and have a value that cannot change?

Symbols. The same Symbol object will be created for a given name or string for the duration of a program's execution, regardless of the context or meaning of that name.



ex. :name, :b, :inigo_montoya

Write a simple loop that repeats the string "Hello, World!" five times.

5.times do


puts "Hello, World!"


end



or


5.times{ puts "Hello, World!" }



Note: these answers are both blocks.

In Ruby, what describes characters (either surrounded by braces or starting with do/end) that group a set of instructions together?

Block.



Blocks are a powerful concept used frequently in Ruby. Think of them as a way of bundling up a set of instructions for use elsewhere.

Blocks can be put between curly braces, but can also begin with the keyword _____ and end with the keyword ___.

Blocks can begin with the keyword do and end with the keyword end. The do/end style is always acceptable.



ex. 5.times do


puts "Hello, World!"


end

What will print to the screen if you run this code?


"this is a sentence".gsub("e"){|letter| letter.upcase}

"this is a sentence".gsub("e"){|letter| letter.upcase}



Answer: "this is a sEntEncE"


- gsub is using the result of the block as the replacement for the original match.

In Ruby, the most common collection of data is a number-indexed list called an _____.

Array.



Arrays are created by putting pieces of data between square brackets ([]). They are separated by commas.

Ruby arrays can grow and shrink. You can add an element to the end of an existing array by using what operator?

The shovel operator (<<).



ex. meals = ["Breakfast", "Lunch", "Dinner"]


=> ["Breakfast", "Lunch", "Dinner"]


meals << "Dessert"


=> ["Breakfast", "Lunch", "Dinner", "Dessert"]

What is the index location of "Dinner" in the following array?



["Breakfast", "Lunch", "Dinner", "Dessert"]

Answer: 2



["Breakfast", "Lunch", "Dinner", "Dessert"]


0 1 2 3

Which common array method returns strings in alphabetical order or numbers in ascending value order.

Answer: .sort



The sort method will return a new array where the elements are sorted.

Which common array method mashes elements within an array into a single string?

Answer: .join



The join method returns a string created by converting each element of the array to a string, separated by the given separator.



ex. [ "a", "b", "c" ].join #=> "abc"


[ "a", "b", "c" ].join("-") #=> "a-b-c"

You can ask an array if an object is present within the array by using which common array method?

Answer: .include?



array.include?(object) returns true if the object exists within the array, false if it does not.



ex. a = [ "a", "b", "c" ]


a.include?("b") #=> true


a.include?("z") #=> false


Explain the difference between these common array methods:



.each


.map (also called .collect)

Both .each and .map/collect apply the values within a given block once for each element in the array. The difference is that .map/collect returns a new array.



ex. a = [ "a", "b", "c", "d" ]


a.each {|x| print x, "*" } #=> a*b*c*d*


a.collect { |x| x + "!" } #=> ["a!", "b!", "c!", "d!"]

Explain the difference between zero and nil.

Zero is something, it’s a number, and it’s not nothing. Nil is nothingness, something that doesn’t exist. If we create a five-element array then asked Ruby to give us the sixth element, Ruby will give us nil. There is no sixth element. It isn’t that there’s a blank in that sixth spot (""), it’s not a number 0, it’s nothingness: nil.

A dictionary-like collection of unique keys and their values is called a ____.

Hash.



A hash is an unordered collection where the data is organized by name into "key/value pairs".



ex. produce = {"apples" => 3, "oranges" => 1, "carrots" => 12}


puts "There are #{produce['oranges']} oranges in the fridge."

In the following code, produce is a ____, apples is a _____ and 3 is a _____. The => is called a _____.



produce = {"apples" => 3, "oranges" => 1, "carrots" => 12}

produce = {"apples" => 3, "oranges" => 1, "carrots" => 12}



Answer: produce is a hash, apples is a key and 3 is a value. The => is called a hashrocket.

Instead of separating hash elements with a hashrocket, you can use symbols to simplify syntax. Simplify the following hash:



animals = {"elephant" => 9, "monkey" => 15, "sloth" => 2}


Original hash:


animals = {"elephant" => 9, "monkey" => 15, "sloth" => 2}


Simplified hash:


animals = {elephant: 9, monkey: 15, sloth: 2}



Notice that the keys end with a colon rather than beginning with one, even though these are symbols.

What do the following operators mean?


==


&&


||

== is a way to question equality. It means "is the thing on the right equal to the thing on the left?" The = used alone attaches something on the left with something on the right (x = y). The == means that x and y are actually equal (x == y).



&& - and ("logical and") ex. if x && y, then z


|| - or ("logical or") ex. command = "q" || command = "quit"

Conditional statements such as ==, > and < evaluate to _____ or _____.

True or false.


The most common conditional operators:


== (equal)


> (greater than)


>= (greater than or equal to)


< (less than)


<= (less than or equal to)

In conditional instructions with more than two statements, what comes between if and else?

Answer: elsif


An if statement has one if statement (executed only if the statement is true), zero or more elsif statements (executed only if the statement is true), and zero or one else statement (executed if all other statements are false). Only one section of the if/elsif/else structure can execute. Once one block runs, that’s it.

In Ruby, _____ are used to bundle one or more repeatable statements into a single unit of reusable code that you call by name.

Method.



In Ruby, methods are used to bundle one or more repeatable statements into a single unit of reusable code that you call by name.

Think of your human self in Ruby terms. You are an _____. If you're in school, you are an instance of the _____ Student. You have _____ like height, weight and eye color. When you "walk", "run", "wash dishes", and "daydream", you are calling _____ on yourself.

You are an object. If you're in school, you are an instance of the class Student. You have attributes like height, weight and eye color. When you "walk", "run", "wash dishes", and "daydream", you are calling methods on yourself.

In the following code, frank is the _____, Student is the _____ and new is the _____.



frank = Student.new

In the following code, frank is the variable, Student is the class and new is the method.



frank = Student.new


variable = Class.method

Explain what a predicate method is and how it differs from a regular method.

Unlike a regular method, which applies an action to an object, a predicate method ends in a ? and is asking for a boolean return of true/false.


ex. def help?


command = "h" || command = "help"


end


This method asks "Has the user entered h or help?" Next steps depend on a true/false answer.

What is the end result of the following code?



a = %w(taco burrito quesadilla tostada)


a.each { |food| puts food}

Result:


taco
burrito
quesadilla
tostada

There are three different ways to create a new array named "farm" that has the following values: pig, cow, chicken.



Explain/execute all three ways.

1. make a list: farm = ["pig", "cow", "chicken"]


2. shortcut list, split on whitespace:


farm = %w(pig cow chicken)


3. individual steps with shovel operator:


farm = Array.new


farm << "pig"


farm << "cow"


farm << "chicken"

Sometimes methods take one or more _____ that tell them how to do what they’re supposed to do. For instance, frank.introduction('Katrina') is a call for Frank to introduce himself to Katrina.

Sometimes methods take one or more arguments.


ex. def introduction(target)


puts "Hi #{target}, I'm #{first_name}!"


end


frank.introduction('Katrina') => "Hi Katrina, I'm Frank!"


Note: frank has already been created as a new instance of Student class, first_name has been defined and frank.first_name = "Frank".

If you called the following method, what would print to the screen?


"Frank's favorite number is #{frank.favorite_number}."


def favorite_number


5


6


7


end

Answer: 7.



By default, a Ruby method returns the value of the last expression it evaluated. The last line of the favorite_number method is 7.

Objects have a unique _____ and a common set of _____.

Objects have a unique state and a common set of methods (or behaviors).

An _____ variable exists for the life of an _____.

An instance variable exists for the life of an object.

A _____ is an abstract description of a category or type of thing.

Answer: class.



A class is an abstract description of a category or type of thing. For example, if you are in school, you are an instance of the Student class. Your class defines what attributes (ex. first_name) and methods (ex. do_homework) all objects of the student type have.

Write a small Ruby program that asks you what your name is. Have the program print out the number of characters in your name. Also have the program print out a message if your name is longer than 25 characters.

def small_ruby_program
puts "What is your name?"
input = gets.strip
if input.length < 25
puts input.length
else
puts "Wow, kindergarten must have sucked for you!"
end
end

When writing methods, what is the difference between arguments and parameters?

The argument is the piece of code you actually put between the method's parentheses when you call it, and the parameter is the name you put between the method's parentheses when you define it.