Study Code Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

to_s

A

convert object to a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

.reverse

A

reverses a string only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

.length

A

counts characters in a string only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

to_i

A

converts object to an integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

to_a

A

converts object to an array [an array is a list in these types of brackets separated by commas]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

.max

A

a method to find the highest number in an array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is being done here?

ticket = [12, 47, 35]

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

.sort

A

sorts an array of integers from the lowest to the highest.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

.join

A

converts an array to a single string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the ! for in the below code

.sort!

A

The exclamation means the the array in which the sort method is being called on will permanently alter the array when completed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

.clear

A

clears the called upon variable

example: > variable = “abcde”
variable. clear returns :> “”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

.downcase

A

converts a string to ALL lowercase “A” to “Z” characters only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

.empty?

A

Asks if a string is empty or not.
Example:> “hello”.empty? returns false
““.empty? returns true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What will the following code result in?

Variable.rstrip.lstrip

A

rstrip and lstrip will remove any white spaces from a string (right, or left sides). If no spaces are removed, will result in nil.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What will the following code result in?

“ Now’s the time”.split

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What will the following code result in?

books = { }

A

This action will create an empty hash named “books.” A hash can be considered a dictionary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

If you have a hash named “Books,” what would the following code result in?
books.length

A

The code would return the amount of book titles in the hash.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Describe the following code in detail.

books[“Jake’s Home Design Review”] = :quite_good.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

If you had a hash named “books,” what would the following code result in?
books.keys

A

The book’s hash, if created with book titles as the keys, would return all the book titles in the hash.

20
Q

When a code word has : what is this called?

A

Symbol

21
Q

Blocks are always attached to what?

A

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!”

22
Q

Dir.entries “/” does what?

A

Lists all the top level directories in the program.

23
Q

In the following code, what is the code that follows the method?
Dir.entries “/”

A

The Dir is a variable; .entries is a method; and anything listed after a method is considered an attachment.

24
Q

What would the following code result in?

Dir[”/*.txt”]

A

This would look up only the .txt files in the directory.

25
Q

What would the following code result in?

print File.­read(“/com­ics.txt”)

A

It calls open the comics.txt file and prints to screen.

26
Q

What would the following code result in?

FileUtils.­cp(‘/comic­s.txt’, ‘/Hom­e/comics.t­xt’)

A

The FileUtils.cp would copy the comics.txt and paste it in the /Home directory (same file name in this case).

27
Q

What would the following code result in?

File.mtime(“/Home/comics.txt”)

A

It would give the time that the file comics.txt was created.

Example:> 2013-02-25 21:41:51 +0000

28
Q

When in the rails console, what would pressing the UP arrow accomplish?

A

The current cursor position would paste the last command entered from the previous cursor.

29
Q

What’s an argument?

A

Arguments are a list of things sent into a method, with commas separating them.
Example:> print “pre”, “event”, “something”, “another”

30
Q

Besides using { } to create a code block, what is another way of doing this?

A

By using DO and END. Can also be used in the command prompt which will create a .. cursor for the second line of code.

31
Q
What would the following code result in?
def load_comics(path)
A

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).

32
Q

These two lines of code are the same but used differently. Why?
entry.time = Time.now
@time = Time.now

A

When outside a class we use accessors like entry.time = Time.now; but inside a class we use instance variables like @time = Time.now.

33
Q

True or False?

Classes explain objects; how certain objects work.

A

True

34
Q

What is this character used for? (%)

A

It’s called a modulus and it is used with floating point numbers to gather the remainder of the the devision.

35
Q
Explain the following terms:
.to_f
.abs
.floor
.ceil
.to_i
A

.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.
36
Q

What is this called?

Example: puts “bob” + “ “ + “Smith”

A

It’s called string concatenation (string-wise addition).

37
Q

What is this character used for in Ruby? ()

A

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.”

38
Q

What is this character called in programming? (!)

A

it’s called a BANG and it denotes a permanent revision to something.

39
Q

What is this character called in programming? (=)

A

It’s called an assignment operator.

40
Q

What does .chomp do?

A

It removes a newline (if it exists) from the end of a string. Example:
name = gets.chomp

41
Q

What value is always returned when using the “gets” method?

A

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

42
Q

What is this code used for? #{ }

A

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.

43
Q

What are two combined assignment operators in Ruby?

A

*= (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 %=.

44
Q

In Ruby, how must you name variables?

A

Variables must begin with a lowercase letter (a - z) or an underscore. After which they can be lower or upper.

45
Q

What is Syntactic Sugar?

A

When methods or code is abbreviated for ease of use like the operator methods +, *, /.

46
Q

What’s a method?

A
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