Ruby Flashcards

1
Q

*.rb

A

Ruby code file extension

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

irb

A

built-in Ruby function for running ruby code outside a ruby code file.

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

#

A

ruby comment line

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

snake_case

A

defining or initializing variables, methods, or files, should always be named using snake_case:
lower case lettering and separated words using underscore.

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

constants

A

denoted by all uppercase letters

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

CamelCase

A
convention for class names
Capitalize every word, no spaces
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Max line length

A

80 chars

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

spaces around operators

A

use spaces around operators

    • except exponent (4**2), the slash in rational literals (1/48r), safe navigation (foo&.bar)
    • no spaces after ( and [, or before ] and )
    • use spaces around { and before }, except interpolated expressions have no spaces
    • no space after bang (!)
    • no space inside range literals (‘a’…‘z’)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

case when indents

A
case
when....
  puts....
when...
  puts...
else
  ....
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

puts

A

display result to console, adding line return at the end
puts “This is test to display”
puts my_variable

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

print

A

display result to console, with no trailing spaces or line feed
print “This is test to display”
print my_variable

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

text

A

Single line comment

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

=begin
text
more text
=end

A

multiline comment

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

Float

Integer

A

Numeric data types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
\+
-
*
/
**
%
A

Common arithmetic operations

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

variable.method

A

all variables in ruby are objects, and have methods available to them

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

strings

A

delineated by single or double quotation marks

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

true, false

A

Boolean values

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

.upcase
.downcase
.

A

converts string to all uppercase or lowercase

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

“your result is #{result}, congratulations”

A

string interpolation with variable

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

gets.chomp

A

method for getting user input
gets is the method for retrieving input
chomp removes the new line that ruby automatically adds to a gets call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
if *test*
  do something
elsif *test*
  do something else
else
  do a third thing
end
A

if else conditional
else is required, any number of elsif statements allowed
requires end to stop

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

!=

24
Q

||

25
&&
and
26
if condition do something end -or- expression if boolean
if conditional
27
unless test do something end -or- do something unless boolean
unless statement
28
string.include? "value"
returns true if value is in string
29
string.gsub!(/value/, "new_value"
global substitution | updates string to replace value with new_value
30
``` counter while counter not some value do something increment counter end ```
while loop syntax
31
counter until counter evaluation do something end
Until loop syntax
32
+=, -=, *=, /=
reassignment operators. return the calculated result to the variable example: counter += 1, will increment counter variable by 1
33
for counter in range do something end
for loop syntax
34
``` counter loop do execute something increment counter break end ```
loop syntax. | do... end can be replaced with {..}
35
break or break if test
ends loop iteration at break statement
36
next if test
skips to following iteration
37
``` object.each { |item| # Do something } ```
each operator executes on each item in an array | {...} can be replaced with do...end, which is preferred with longer than 1 line statement
38
num.times {execute something}
times loop
39
string.split("separator")
converts string to array with designated separator
40
hash = {"key"=> "value", "key"=>"value, ...}
Like an object in javascript | made up of keys and values
41
Hash.new(default value)
creates empty hash same as: my_hash = {} default value is optional. With no default, {} = nil
42
hash.sort_by do |key,value| count end
sorts hash smallest to largest cant be used with bang "sort_by!" has to be multiline, not " hash.sort_by { |key,value| count }
43
Reusable section of code written to perform a specific task in a program
Method | similar to a function in other languages
44
<=>
Combined comparison operator compares two items. returns 1 if first > second, 0 if equal, -1 if second > first array. sort! { |x,y| x<=>y } sorts descending array. sort! { |x,y| y<=>x } sorts ascending
45
string. to_sym | string. intern
converts string to symbol
46
hash.select { |key,value| test}
Selects
47
``` case string when x do x when y do y end -or- case string when x then x when y then y end ```
case statement is effectively multiple if elsif statements
48
boolean ? Do this if true: Do this if false
Ternary conditional expression | takes 3 arguments: boolean, expression to eval if true, and expression to eval if false
49
||=
Conditional assignment operator. | Only assigns value if the variable != nil
50
implicit return
If a method has no return statement, the last evaluated expression will be returned
51
object.respond_to?(symbol)
tests whether an object will receive a method e.g. [1, 2, 3].respond_to?(:push) returns true becasue an array can have push [1, 2, 3].respond_to?(:to_sym) returns false because an array can't be converted to symbol
52
<
"The Shovel", or the concatenation operator, can be used to add an element to the end of an array, text ``` [1,2,3] << 4 # ==> [1,2,3,4] ```
53
x.floor
rounds down a number to closest whole below it floor can also be achieved by x.to_i
54
Proc.new { |x| execute someething }
a Proc is a block that can be reused it's called using the (&procname) syntax proc's can be called via argument [function(&procname)], or using procname.call
55
numbers_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] strings_array = numbers_array.map(&:to_s) puts strings_array --> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
proc + symbol applied to convert array of integers to array of strings