try ruby Flashcards

(156 cards)

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
how do you create a block in ruby
curly braces
26
a way of performing something x times
5.times {print "hi! "}
27
how to create a symbol
:symbolhere
28
blocks
chunk of code which can be tacked oon to many of rubys methods
29
access directory
Dir.entries "/" (anything listed after a method is called an attatchment, like the / in this example)
30
Dir[] does what
searches for files with wildcard characters
31
read a file
File.read()
32
copy a file to new dir
FileUtils.cp('file1', 'destination')
33
open file
File.open()
34
using the pipe character in a do statement
File.open("txt.txt", "a") do |f| f<< "Cat" end
35
check when a file was changed
File.mtime(filename.txt) can also append .hour to it to just get hour
36
how are arguments seperated in ruby
commas
37
define a method in ruby
def methodname code end
38
split
method for strings, breaking it into an array based on the given char to split on
39
strip
removes extra whitespace around the url
40
p line is short for what
paragraph
41
create a list
list do p"1" p "2" end
42
to load a library
require 'library'
43
the .new method does what
used to make objects of a certain class
44
define a class
class Nameofclass
45
How to instantiate a class
x=NAmeofclass.new
46
what is attr_accessor
attributes or variables attatched to the class, used for acccesing outside the class
47
how to declare an instance variable
@x
48
what are instance variables
vars for accesing inside the class
49
sort attributes with a time association in ascending order
blog.sort_by { |entry| entry.time }.reverse
50
add new entries
<< x
51
what can the map method do
cycle through an array and replace each item with something new
52
Time method
Time.now | Time.now-2.weeks
53
use a variable in a string
"Hello #{variable}"
54
execute a block of code x times
x.times do code end
55
iterate through an array
array.each do |n| puts n end
56
unless
evaluates to if not, do
57
an if, else if, else statement in ruby
``` if x==0 puts x elsif x==1 puts x else puts x end ```
58
.empty?
boolean for if something is empty
59
nil is treated as what in ruby
false
60
path of a file method
.file_path
61
empty strings, 0, and empty arrays are treated as what
true
62
fail raises what kind of error if true
runtimeerror written fail "wrong password" will raise exception wrong password
63
what hapens in result= 1||2
returns 1, because it doesnt need to evaluate the second half
64
create an array using ||
tweets=timeline.tweets||[] if tweets returns nil, it creates an array otherwise it assigns them
65
does every method in ruby return a value
yes
66
case statements in ruby
case, when, then, else, end
67
use grep method in ruby
``` search = "Super Mario Bros." games = ["Super Mario Bros.", "Contra", "Metroid", "Mega Man 2"] matched_games = games.grep(Regexp.new(search)) ```
68
uppercase all letters
.upcase on stringleas
69
are variables an object
kind of, its a reference to an object (east previous to ruby 2)
70
ruby convention for naming variable
underscores betweeen words
71
class variable indicator
@@
72
instance variable indicator
@
73
exponential operator
**
74
find class type of something
.class
75
what operator appends an item to an array
<<
76
ruby is inspired by
perl, smalltalk, eiffel, ada
77
ruby is a blank language
dynamic
78
when ruby runs, it is translated and executed how
on the fly
79
what is the standard ruby interpreter
MRI (alternative:rubinius amd jruby, whihc runs on java vm)
80
to run ruby what do you need
a ruby interpreter
81
in ruby what is repl
read evaluate print loop
82
in ruby what is the => operator called
hash rocket
83
in ruby 1.9.3 how many reserved words are there
40
84
what are the 4 types of variables in ruby 1.9.3
local, global, instance, constant
85
in ruby how do you define a global variable
$-- can be cap or lowercase
86
in ruby how do you define a constant var
all upercase
87
in ruby how do you define a instance variable
@
88
in ruby what is the difference bw an instance variable and a local
instance variables are accesible throughout the class
89
in ruby an class typically uses what case struct
camel, upper first too
90
how to define a case statement in ruby
case | when x
91
in ruby, def initialize is like what
a constructor
92
in ruby how can you output the value of a variable from within a sting
using the#{var} // this is called interpolation-- if wrapped in single quotes, this will not work
93
in ruby how do we get input from the user
the gets method
94
inrubywhat does chomp do
removes whitespace
95
3 ways to create a string in ruby
String.new(), single quotes, double quotes
96
in ruby when using single quotes, do you need an escape for example a double quote?
no
97
what are 3 concatination operators for strings in ruby
<< and + and .concat
98
if using the [n] on a string like name[0] what does it return
the ascii representaton of that letter
99
in ruby what is the method for removing trailing whitespace
.chop
100
how to find the index of a letter in a string ruby
.index("x")
101
in ruby how do you find how many letters are in the string
.length or .size
102
in ruby how do you find what is swapcase
swaps upper for lower and vice versa
103
in ruby what does split do
returns an array split on the given value .split(" ")
104
runnin g .size in ruby on a fixnum (i) what is returned
the bytes it takes up
105
in ruby what is a combined operator comparison symbol and what does it return
0 if equal 1 is lef is greater -1 if right is greater
106
in ruby the .eql? does what
checks if the value AND type are the same
107
how to access values in a library
libname::x
108
how to generate a random number in ruby
rand (addditonally you can use a follow number to find rand between 0->x)
109
to use a library in ruby what do you do
require "lib"
110
in ruby bigdecimal fixes what
the floating point math problem
111
in ruby how to use make a floating umber to bigdecimal
BigDecimal.new(x)
112
in ruby [1,2,3]&[2,3] wpold return what
[1]
113
in ruby how to put values at the beginning of an array
unshift(x)
114
how to clear an array in ruby
.clear
115
in ruby how to flatten a multi-d array
.flatten!
116
what does array.select do with a given block, what about reject and any and all
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
in a ruby hash how can you find a single element, what about all elements that match? You can allso use .all
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
how to find how many values are in a has
.any method
119
how to convert a hash to an array
hash.map{| key, value| value ="(hash) #{value}"} //returns an array with the hash in parenthesis prepended to the value ["(hash) value", "(hash) value2)"]
120
do ruby methods accept default values
yes
121
in ruby how do you create a class method
prefix it with the word self //self.method
122
in ruby what does ||= do
this is a conditional assignment operator-- for example it checks if it exists, and if it doesnt, then it does
123
in ruby what is the way to set a set and get method
attr_reader/writer :x or attr_accesor
124
what s=is an alternate way of creating a group of class methods without using self on each method
``` class << self def method end def method2 end end ```
125
in ruby how do you make a private method
define the method normally, and then use private :method at the end, or you can use the word private above all private methods
126
in ruby methods have what protection level by default
public
127
in ruby how can a class call its own private method
creating a new public method that has the private method nested within the body def call_private private_method end
128
in ruby what is a protected method
protected methods can be called within the class and by other objects/subclasses
129
in ruby how do you write a forloop
for i in array do x end
130
in ruby what is the keyword that skips to the next iterationn oif the loop
next
131
in ruby what is the keyword that breaks out of a loop entireley
break
132
how can you run a loop until a certain value is gotten without using the until loop
loop do combined with break
133
in ruby what does retry do? what about redo
retry reexecutes the entire loop, redo will only redo the current iteration it is on until end
134
In ruby how do we pass a block into a method
method(&blockname)
135
in ruby how do you create a block
curly braces, or do and end (curly braces have a higher priority
136
in ruby how do you call a block
the call method
137
in ruby what is the method to check if a block was passed to a method
block_given?
138
in ruby what is the keyword to make sure something is given (in error checking)
ensure
139
in ruby how do you raise an error
using the raise keyword followed by an error
140
in ruby if an error is expcted what can we use to counter it
using the rescue statement
141
in ruby what do BEGIN and END blocks do
BEGIN is run at the beginning of a program, end is run at the end of a program
142
in ruby what does yield do
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
in ruby how do you create a proc
proc.new with a block Proc.new{block here} or you can use lower case proc with block assigned to a variable
144
in ruby how do you create a lambda
similar to proc, you assign a variable to lambda { block here} you can also use the skinny arrow ->{ block }
145
in ruby how does a proc take an argument
using the pipes. mproc= proc {|passedin| }
146
in ruby what is the major difference between a proc and a lambda
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
in ruby how do you write multi line comments
=begin =end
148
in ruby what does capitalize do
capitalizes the first lettter
149
in ruby what is the naming convention and keyword to create a new module
module in camelcase (first upper too)
150
in ruby, what naming convention do you use to create a constant
all uppercase
151
In ruby (specifically irb) what symbols do you use to access something from within a loaded module
::
152
in ruby modules are primarily used to accomplish what
namespacing
153
in ruby what does extending allow us to do in terms of modules and classes
using .extend, a class can access a modules internals
154
in ruby, alternative to using extend to access methods inside a module, what can we do
include the module inside the class
155
what is the main difference between a symbol and string in ruby
a symbol only exists once in memory for a session
156
in ruby how can we override default methods for instance to_S
within a classs, you can create a to_s method