Study Code Flashcards
to_s
convert object to a string
.reverse
reverses a string only
.length
counts characters in a string only
to_i
converts object to an integer
to_a
converts object to an array [an array is a list in these types of brackets separated by commas]
.max
a method to find the highest number in an array.
What is being done here?
ticket = [12, 47, 35]
A variable is being created named “ticket”, which now defines the array of integers [12,47,35]. This new variable can now have methods called on it such as ticket.max to get the highest number in the array.
.sort
sorts an array of integers from the lowest to the highest.
.join
converts an array to a single string.
What is the ! for in the below code
.sort!
The exclamation means the the array in which the sort method is being called on will permanently alter the array when completed.
.clear
clears the called upon variable
example: > variable = “abcde”
variable. clear returns :> “”
.downcase
converts a string to ALL lowercase “A” to “Z” characters only.
.empty?
Asks if a string is empty or not.
Example:> “hello”.empty? returns false
““.empty? returns true
What will the following code result in?
Variable.rstrip.lstrip
rstrip and lstrip will remove any white spaces from a string (right, or left sides). If no spaces are removed, will result in nil.
What will the following code result in?
“ Now’s the time”.split
The .split method splits each word into a separate string using the empty white spaces to determine word beginning and end. Other types of characters can be used to split by defining at the end of the .split method.
What will the following code result in?
books = { }
This action will create an empty hash named “books.” A hash can be considered a dictionary.
If you have a hash named “Books,” what would the following code result in?
books.length
The code would return the amount of book titles in the hash.
Describe the following code in detail.
books[“Jake’s Home Design Review”] = :quite_good.
This is a hash named “books”, which includes a book titled “Jake’s Home Design Review,” which is the KEY of the hash. Then there is a VALUE (noted by the :) which rates the book as quite good.
If you had a hash named “books,” what would the following code result in?
books.keys
The book’s hash, if created with book titles as the keys, would return all the book titles in the hash.
When a code word has : what is this called?
Symbol
Blocks are always attached to what?
Block code is always attached to a method.
Example:> 5.times { print “Odelay!” }
This code will print Odelay! five times like
“Odelay!Odelay!Odelay!Odelay!Odelay!”
Dir.entries “/” does what?
Lists all the top level directories in the program.
In the following code, what is the code that follows the method?
Dir.entries “/”
The Dir is a variable; .entries is a method; and anything listed after a method is considered an attachment.
What would the following code result in?
Dir[”/*.txt”]
This would look up only the .txt files in the directory.
What would the following code result in?
print File.read(“/comics.txt”)
It calls open the comics.txt file and prints to screen.
What would the following code result in?
FileUtils.cp(‘/comics.txt’, ‘/Home/comics.txt’)
The FileUtils.cp would copy the comics.txt and paste it in the /Home directory (same file name in this case).
What would the following code result in?
File.mtime(“/Home/comics.txt”)
It would give the time that the file comics.txt was created.
Example:> 2013-02-25 21:41:51 +0000
When in the rails console, what would pressing the UP arrow accomplish?
The current cursor position would paste the last command entered from the previous cursor.
What’s an argument?
Arguments are a list of things sent into a method, with commas separating them.
Example:> print “pre”, “event”, “something”, “another”
Besides using { } to create a code block, what is another way of doing this?
By using DO and END. Can also be used in the command prompt which will create a .. cursor for the second line of code.
What would the following code result in? def load_comics(path)
This would create a method by defining (def) a method called load_comics followed by a list of arguments the method would need e.g. (path).
These two lines of code are the same but used differently. Why?
entry.time = Time.now
@time = Time.now
When outside a class we use accessors like entry.time = Time.now; but inside a class we use instance variables like @time = Time.now.
True or False?
Classes explain objects; how certain objects work.
True
What is this character used for? (%)
It’s called a modulus and it is used with floating point numbers to gather the remainder of the the devision.
Explain the following terms: .to_f .abs .floor .ceil .to_i
.to_f = makes the number a floating number. So 4.to_f would then be 4.0.
.abs = gives the absolute value of the number. So a -3.abs would then be |3|
.floor = rounds down a floating number. So 4.2 would be 4.0
.ceil = rounds up a floating number. So 4.2 would be 5.0 .to_i = changes a number character to an integer.
What is this called?
Example: puts “bob” + “ “ + “Smith”
It’s called string concatenation (string-wise addition).
What is this character used for in Ruby? ()
It’s used as an escape. So “This is a newline \n it puts strings on a new line.” would have two lines of code. Two more examples:
“"These double quotes are escaped", he explained.”
or “Even the backslash ( \ ) is escaped.”
What is this character called in programming? (!)
it’s called a BANG and it denotes a permanent revision to something.
What is this character called in programming? (=)
It’s called an assignment operator.
What does .chomp do?
It removes a newline (if it exists) from the end of a string. Example:
name = gets.chomp
What value is always returned when using the “gets” method?
The value will ALWAYS be a string. Example: a number input would be just a character string. to change the value to an integer, use the .to_i
What is this code used for? #{ }
It’s the string interpolation method which allows us to embed Ruby code or variable values without have to use the + operator to form the concatenation.
What are two combined assignment operators in Ruby?
*= (this means we can now type this “num *= 4” instead of “num = num * 4”)
and
+= (this means we can now type this “num += 4” instead of “num = num + 4”)
Also, can use -=, /=, and %=.
In Ruby, how must you name variables?
Variables must begin with a lowercase letter (a - z) or an underscore. After which they can be lower or upper.
What is Syntactic Sugar?
When methods or code is abbreviated for ease of use like the operator methods +, *, /.
What’s a method?
A method can be written in a Class or a Module; in a module it can be called without explicitly defining an instance object by just using the name and the arguments e.g. add(3,4). A method begins with def and ends with end. example def method_name(parameter list) guts of the method and a return end