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

!=

A

Not!

24
Q

||

A

or

25
Q

&&

A

and

26
Q

if condition
do something
end

-or-

expression if boolean

A

if conditional

27
Q

unless test
do something
end

-or-

do something unless boolean

A

unless statement

28
Q

string.include? “value”

A

returns true if value is in string

29
Q

string.gsub!(/value/, “new_value”

A

global substitution

updates string to replace value with new_value

30
Q
counter 
while counter not some value
  do something
  increment counter
end
A

while loop syntax

31
Q

counter
until counter evaluation
do something
end

A

Until loop syntax

32
Q

+=, -=, *=, /=

A

reassignment operators. return the calculated result to the variable
example: counter += 1, will increment counter variable by 1

33
Q

for counter in range
do something
end

A

for loop syntax

34
Q
counter
loop do
  execute something
  increment counter
  break
end
A

loop syntax.

do… end can be replaced with {..}

35
Q

break
or
break if test

A

ends loop iteration at break statement

36
Q

next if test

A

skips to following iteration

37
Q
object.each { |item| 
  # Do something 
}
A

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
Q

num.times {execute something}

A

times loop

39
Q

string.split(“separator”)

A

converts string to array with designated separator

40
Q

hash = {“key”=> “value”, “key”=>”value, …}

A

Like an object in javascript

made up of keys and values

41
Q

Hash.new(default value)

A

creates empty hash
same as:
my_hash = {}
default value is optional. With no default, {} = nil

42
Q

hash.sort_by do |key,value|
count
end

A

sorts hash smallest to largest
cant be used with bang “sort_by!”
has to be multiline, not “ hash.sort_by { |key,value| count }

43
Q

Reusable section of code written to perform a specific task in a program

A

Method

similar to a function in other languages

44
Q

<=>

A

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
Q

string. to_sym

string. intern

A

converts string to symbol

46
Q

hash.select { |key,value| test}

A

Selects

47
Q
case string
  when x
    do x
  when y
    do y
end
-or-
case string
  when x then x 
  when y then y
end
A

case statement is effectively multiple if elsif statements

48
Q

boolean ? Do this if true: Do this if false

A

Ternary conditional expression

takes 3 arguments: boolean, expression to eval if true, and expression to eval if false

49
Q

||=

A

Conditional assignment operator.

Only assigns value if the variable != nil

50
Q

implicit return

A

If a method has no return statement, the last evaluated expression will be returned

51
Q

object.respond_to?(symbol)

A

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
Q

<

A

“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
Q

x.floor

A

rounds down a number to closest whole below it

floor can also be achieved by x.to_i

54
Q

Proc.new { |x| execute someething }

A

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
Q

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

A

proc + symbol applied to convert array of integers to array of strings