try ruby Flashcards

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

reverse a string

A

.reverse

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

length of a string

A

.length

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

print string x times

A

”” * X

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

reverse number

A

1.to_s.reverse

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

to string

A

.to_s

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

to integer

A

to_i

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

to array

A

to_a

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

declare array / assign array

A

[] / x=[1,2,3]

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

[] creates an empty what

A

list

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

does a list store thing out of order

A

false

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

get max from array

A

.max

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

sort array

A

.sort!

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

method with ! does what

A

changes value for good

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

replace all string x in an array containing x

A

arr[‘x’]=’y’

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

reverse an array with a string of lines, (only each individual line) without writing words backwards

A

poem.lines.to_a.reverse

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

join elements in an array

A

.join

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

check to see if something is included

A

.include?

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

lowercase everything

A

.downcase

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

delete an obj

A

.delete

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

dictionary or hash

A

books={}

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

add elements to a dictionary/hash

A

books[“title”] = :splendid

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

can you use length on a dictionary/hash?

A

yes, will return number of items within

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

iterate through a dictionary/hash’s values

A

books.values.each{|rate| ratings[rate]}

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

iterate and count number of times rate appears through a dictionary/hash’s values

A

books.values.each{|rate| ratings[rate]+=1}

returns like {:good=>2, :bad:=>5}

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

how do you create a block in ruby

A

curly braces

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

a way of performing something x times

A

5.times {print “hi! “}

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

how to create a symbol

A

:symbolhere

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

blocks

A

chunk of code which can be tacked oon to many of rubys methods

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

access directory

A

Dir.entries “/” (anything listed after a method is called an attatchment, like the / in this example)

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

Dir[] does what

A

searches for files with wildcard characters

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

read a file

A

File.read()

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

copy a file to new dir

A

FileUtils.cp(‘file1’, ‘destination’)

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

open file

A

File.open()

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

using the pipe character in a do statement

A

File.open(“txt.txt”, “a”) do |f|
f&laquo_space;“Cat”
end

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

check when a file was changed

A

File.mtime(filename.txt)

can also append .hour to it to just get hour

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

how are arguments seperated in ruby

A

commas

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

define a method in ruby

A

def methodname
code
end

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

split

A

method for strings, breaking it into an array based on the given char to split on

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

strip

A

removes extra whitespace around the url

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

p line is short for what

A

paragraph

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

create a list

A

list do
p”1”
p “2”
end

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

to load a library

A

require ‘library’

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

the .new method does what

A

used to make objects of a certain class

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

define a class

A

class Nameofclass

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

How to instantiate a class

A

x=NAmeofclass.new

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

what is attr_accessor

A

attributes or variables attatched to the class, used for acccesing outside the class

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

how to declare an instance variable

A

@x

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

what are instance variables

A

vars for accesing inside the class

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

sort attributes with a time association in ascending order

A

blog.sort_by { |entry| entry.time }.reverse

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

add new entries

A

&laquo_space;x

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

what can the map method do

A

cycle through an array and replace each item with something new

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

Time method

A

Time.now

Time.now-2.weeks

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

use a variable in a string

A

“Hello #{variable}”

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

execute a block of code x times

A

x.times do
code
end

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

iterate through an array

A

array.each do |n|
puts n
end

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

unless

A

evaluates to if not, do

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

an if, else if, else statement in ruby

A
if x==0
  puts x
elsif x==1
  puts x
else
 puts x
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

.empty?

A

boolean for if something is empty

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

nil is treated as what in ruby

A

false

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

path of a file method

A

.file_path

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

empty strings, 0, and empty arrays are treated as what

A

true

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

fail raises what kind of error if true

A

runtimeerror

written fail “wrong password”

will raise exception wrong password

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

what hapens in result= 1||2

A

returns 1, because it doesnt need to evaluate the second half

64
Q

create an array using ||

A

tweets=timeline.tweets||[]

if tweets returns nil, it creates an array otherwise it assigns them

65
Q

does every method in ruby return a value

A

yes

66
Q

case statements in ruby

A

case, when, then, else, end

67
Q

use grep method in ruby

A
search = "Super Mario Bros."
games = ["Super Mario Bros.", "Contra", "Metroid", "Mega Man 2"]
matched_games = games.grep(Regexp.new(search))
68
Q

uppercase all letters

A

.upcase on stringleas

69
Q

are variables an object

A

kind of, its a reference to an object (east previous to ruby 2)

70
Q

ruby convention for naming variable

A

underscores betweeen words

71
Q

class variable indicator

A

@@

72
Q

instance variable indicator

A

@

73
Q

exponential operator

A

**

74
Q

find class type of something

A

.class

75
Q

what operator appends an item to an array

A

«

76
Q

ruby is inspired by

A

perl, smalltalk, eiffel, ada

77
Q

ruby is a blank language

A

dynamic

78
Q

when ruby runs, it is translated and executed how

A

on the fly

79
Q

what is the standard ruby interpreter

A

MRI (alternative:rubinius amd jruby, whihc runs on java vm)

80
Q

to run ruby what do you need

A

a ruby interpreter

81
Q

in ruby what is repl

A

read evaluate print loop

82
Q

in ruby what is the => operator called

A

hash rocket

83
Q

in ruby 1.9.3 how many reserved words are there

A

40

84
Q

what are the 4 types of variables in ruby 1.9.3

A

local, global, instance, constant

85
Q

in ruby how do you define a global variable

A

$– can be cap or lowercase

86
Q

in ruby how do you define a constant var

A

all upercase

87
Q

in ruby how do you define a instance variable

A

@

88
Q

in ruby what is the difference bw an instance variable and a local

A

instance variables are accesible throughout the class

89
Q

in ruby an class typically uses what case struct

A

camel, upper first too

90
Q

how to define a case statement in ruby

A

case

when x

91
Q

in ruby, def initialize is like what

A

a constructor

92
Q

in ruby how can you output the value of a variable from within a sting

A

using the#{var} // this is called interpolation– if wrapped in single quotes, this will not work

93
Q

in ruby how do we get input from the user

A

the gets method

94
Q

inrubywhat does chomp do

A

removes whitespace

95
Q

3 ways to create a string in ruby

A

String.new(), single quotes, double quotes

96
Q

in ruby when using single quotes, do you need an escape for example a double quote?

A

no

97
Q

what are 3 concatination operators for strings in ruby

A

&laquo_space;and + and .concat

98
Q

if using the [n] on a string like name[0] what does it return

A

the ascii representaton of that letter

99
Q

in ruby what is the method for removing trailing whitespace

A

.chop

100
Q

how to find the index of a letter in a string ruby

A

.index(“x”)

101
Q

in ruby how do you find how many letters are in the string

A

.length or .size

102
Q

in ruby how do you find what is swapcase

A

swaps upper for lower and vice versa

103
Q

in ruby what does split do

A

returns an array split on the given value .split(“ “)

104
Q

runnin g .size in ruby on a fixnum (i) what is returned

A

the bytes it takes up

105
Q

in ruby what is a combined operator comparison symbol and what does it return

A

0 if equal 1 is lef is greater -1 if right is greater

106
Q

in ruby the .eql? does what

A

checks if the value AND type are the same

107
Q

how to access values in a library

A

libname::x

108
Q

how to generate a random number in ruby

A

rand (addditonally you can use a follow number to find rand between 0->x)

109
Q

to use a library in ruby what do you do

A

require “lib”

110
Q

in ruby bigdecimal fixes what

A

the floating point math problem

111
Q

in ruby how to use make a floating umber to bigdecimal

A

BigDecimal.new(x)

112
Q

in ruby [1,2,3]&[2,3] wpold return what

A

[1]

113
Q

in ruby how to put values at the beginning of an array

A

unshift(x)

114
Q

how to clear an array in ruby

A

.clear

115
Q

in ruby how to flatten a multi-d array

A

.flatten!

116
Q

what does array.select do with a given block, what about reject and any and all

A

returns values iwthin the array that are validaated as true. i.e array.select {|item| item>x}

alternatively you can use reject to basically perform a !=

any? and all? return booleans – selef explanatory on when it would return t/f

117
Q

in a ruby hash how can you find a single element, what about all elements that match? You can allso use .all

A

hash. find{|key,value| key==’x’} //returns array
hash. find_all{|key,value| value.match(‘x’)} //returns array

You can allso use .all? and any for their boolena return

118
Q

how to find how many values are in a has

A

.any method

119
Q

how to convert a hash to an array

A

hash.map{| key, value| value =”(hash) #{value}”} //returns an array with the hash in parenthesis prepended to the value [“(hash) value”, “(hash) value2)”]

120
Q

do ruby methods accept default values

A

yes

121
Q

in ruby how do you create a class method

A

prefix it with the word self //self.method

122
Q

in ruby what does ||= do

A

this is a conditional assignment operator– for example it checks if it exists, and if it doesnt, then it does

123
Q

in ruby what is the way to set a set and get method

A

attr_reader/writer :x

or

attr_accesor

124
Q

what s=is an alternate way of creating a group of class methods without using self on each method

A
class << self
 def method
 end
 def method2
 end
end
125
Q

in ruby how do you make a private method

A

define the method normally, and then use private :method at the end, or you can use the word private above all private methods

126
Q

in ruby methods have what protection level by default

A

public

127
Q

in ruby how can a class call its own private method

A

creating a new public method that has the private method nested within the body
def call_private
private_method
end

128
Q

in ruby what is a protected method

A

protected methods can be called within the class and by other objects/subclasses

129
Q

in ruby how do you write a forloop

A

for i in array do
x
end

130
Q

in ruby what is the keyword that skips to the next iterationn oif the loop

A

next

131
Q

in ruby what is the keyword that breaks out of a loop entireley

A

break

132
Q

how can you run a loop until a certain value is gotten without using the until loop

A

loop do combined with break

133
Q

in ruby what does retry do? what about redo

A

retry reexecutes the entire loop, redo will only redo the current iteration it is on until end

134
Q

In ruby how do we pass a block into a method

A

method(&blockname)

135
Q

in ruby how do you create a block

A

curly braces, or do and end (curly braces have a higher priority

136
Q

in ruby how do you call a block

A

the call method

137
Q

in ruby what is the method to check if a block was passed to a method

A

block_given?

138
Q

in ruby what is the keyword to make sure something is given (in error checking)

A

ensure

139
Q

in ruby how do you raise an error

A

using the raise keyword followed by an error

140
Q

in ruby if an error is expcted what can we use to counter it

A

using the rescue statement

141
Q

in ruby what do BEGIN and END blocks do

A

BEGIN is run at the beginning of a program, end is run at the end of a program

142
Q

in ruby what does yield do

A

The yield statement gives control of the current method over to the block that is passed in. Yield can also be used to send a variable value in to, or out of, a block of code

143
Q

in ruby how do you create a proc

A

proc.new with a block
Proc.new{block here}

or you can use lower case proc with block assigned to a variable

144
Q

in ruby how do you create a lambda

A

similar to proc, you assign a variable to lambda { block here}

you can also use the skinny arrow ->{ block }

145
Q

in ruby how does a proc take an argument

A

using the pipes. mproc= proc {|passedin| }

146
Q

in ruby what is the major difference between a proc and a lambda

A

lamdas require the exact number of arguments specified, also a specified return in a proc prior to the final line will return that, whereas a lamda will return the final return value

147
Q

in ruby how do you write multi line comments

A

=begin

=end

148
Q

in ruby what does capitalize do

A

capitalizes the first lettter

149
Q

in ruby what is the naming convention and keyword to create a new module

A

module in camelcase (first upper too)

150
Q

in ruby, what naming convention do you use to create a constant

A

all uppercase

151
Q

In ruby (specifically irb) what symbols do you use to access something from within a loaded module

A

::

152
Q

in ruby modules are primarily used to accomplish what

A

namespacing

153
Q

in ruby what does extending allow us to do in terms of modules and classes

A

using .extend, a class can access a modules internals

154
Q

in ruby, alternative to using extend to access methods inside a module, what can we do

A

include the module inside the class

155
Q

what is the main difference between a symbol and string in ruby

A

a symbol only exists once in memory for a session

156
Q

in ruby how can we override default methods for instance to_S

A

within a classs, you can create a to_s method