Ruby On Rails Flashcards

Studying

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

Ask name & print “Hello, _____!”

A

print “What’s your name ?”
name = gets.chomp
p “Hello, #{name}!”

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

\t

\s

A

tab in Ruby. ie. “Bob\tDoe” prints
“Bob Doe”

space in Ruby ie. “Bob\sDoe” prints
“Bob Doe”

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

.split

A

divides string into substrings, returning an array of these substrings. ie:
“ now’s the time”.split #=> [“now’s”, “the”, “time”]
“ now’s the time”.split(‘ ‘) #=> [“now’s”, “the”, “time”]
“ now’s the time”.split(/ /) #=> [””, “now’s”, “”, “the”, “time”]
“1, 2.34,56, 7”.split(%r{,\s}) #=> [“1”, “2.34”, “56”, “7”]
“hello”.split(//) #=> [“h”, “e”, “l”, “l”, “o”]
“hello”.split(//, 3) #=> [“h”, “e”, “llo”]
“hi mom”.split(%r{\s
}) #=> [“h”, “i”, “m”, “o”, “m”]

“mellow yellow”.split(“ello”) #=> [“m”, “w y”, “w”]
“1,2,,3,4,,”.split(‘,’) #=> [“1”, “2”, “”, “3”, “4”]
“1,2,,3,4,,”.split(‘,’, 4) #=> [“1”, “2”, “”, “3,4,,”]
“1,2,,3,4,,”.split(‘,’, -4) #=> [“1”, “2”, “”, “3”, “4”, “”, “”]

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

«HERE
(HERE document)
Great for adding large sets of text.

A
Ie. words = <<HERE
     Now is the time
     for all people
     to come together.
HERE

print words
Now is the time
for all people
to come together.

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

.include?

A

Does something include?
Ie. letters = ‘a’..’z’
letters.include?(‘h’)
=> true

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

.squeeze

A

Removes trailing spaces.
Ie. name = “Jane “
name.squeeze returns “Jane “

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

.each

A
Will return each piece of data within object called.
Ie. range = (0..4)
range.each {|n| puts n}
0
1
2
3
4
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

.to_a

A

Turns data into an array.
Ie. digits = 0..3
num_array = digits.to_a
=> [0, 1, 2, 3]

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

$ variables

A

$ makes variable global

Ie. $salary = 40000

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

hash = {}

A
Holds keys with values
Ie. nums = {
'Dave' => '1234',
'Bill' => '2345'
}
nums['Dave']
=> "1234"
OR new way is
Dave: 1234,
Bill: 2345
Nums[:Dave]
=> 1234
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Constants

A

Variable in all uppercase:

PI = 3.14

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

.to_s

A

Converts data to string
Ie. age = Integer(gets)
puts “Being “ + age.to_s + “ feels just like “ + (age-1).to_s + “!”

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

.rand

A
Randomizer
Ie. nums = []
i = 0
while i < 5
nums[i] = .rand(101)
i += 1
End
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Parallel assignment

A

a, b = b, a

This will swap the values of the variables. Nice to use with data sorting w/o introducing new variable.

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

defined?

A

Defines data:
Ie. a = 1
defined? a
=> “local-variable”

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

.step

A

Use to “step” values. Ie. if step = 5, values will be incremented by 5.

0.step(15,5) {|i| print i, “ “ }
=> 0 5 10 15

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

case (when)

A
Useful "when" there's lots of data. Otherwise if/else statement is better.
Ie:
puts("Enter a grade: ")
grade = gets
grade = Integer(grade)
case grade
   when 90..100
      letterGrade = "A"
   when 80..89
      letterGrade = "B"
   when 70..79
      letterGrade = "C"
   when 60..69
      letterGrade = "D"
   else 
      letterGrade = "F"
end
puts("The letter grade is " + letterGrade)
17
Q

break

use cautiously

A
Ends program if activated and/or will go to next line program after final end if there is one.
Ie:
if data/input == 0 then
     break
end
#next line
18
Q

redo

A
Restarts program if activated.
Ie:
if data/input == 0 then
     redo
end
19
Q

next

A

Jumps to next iteration of the most internal loop. Terminates execution of a block if called within a block (with yield or call returning nil).

for i in 0..5

if i < 2 then

  next    end    puts "Value of local variable is #{i}" end
20
Q

exit

A
Exits program. Ie:
elsif tries == 3
puts "Sorry answer is apple."
exit
else 
puts "Try again."
21
Q

require

A

Includes or “requires” the use of the named file:

require ‘./tempconvert’

puts ftoc(212)
puts ctof(0)
22
Q

rescue

A

Provides solution to an exception “error.” Ie. if you try and divide by zero.
Needs “begin”, “rescue” & “end”

begin
   print("Enter numerator: ")
   num = Integer(gets)
   print("Enter denominator: ")
   denom = Integer(gets)
   ratio = num / denom
   print(ratio)
rescue
   print $! #this stores the exception
   puts
   print("Enter a denominator other than 0: ")
   denom = Integer(gets)
   ratio = num / denom
   print(ratio)
end
23
Q

Debug

A

(In terminal)

ruby -r debug file.rb

24
Q

variable

A

storage location/s that hold data used by program

25
Q

.slice

A
If passed a single index, returns a substring of one character at that index. If passed a start index and a length, returns a substring containing length characters starting at the index. If passed a range, its beginning and end are interpreted as offsets delimiting the substring to be returned. ie:
a = "hello there"
a[1]                   #=> "e"
a[2, 3]                #=> "llo"
a[2..3]                #=> "ll"
a[-3, 2]               #=> "er"
a[/[aeiou](.)\1/, 0]   #=> "ell"
a[/(?[aeiou])(?[^aeiou])/, "non_vowel"] #=> "l"
26
Q

.count

A
used to count characters. ie:
a = "hello world"
a.count "lo"                   #=> 5
a.count "lo", "o"              #=> 2
"hello^world".count "\\^aeiou" #=> 4

c = “hello world\r\n”
c.count “\” #=> 2

27
Q

.sub

A

substitutes ONLY the first occurrence of the pattern specified, whereas gsub is “global”. (see gsub card for examples)

28
Q

.gsub

A

global substitution method. ie:
“hello”.gsub(/[aeiou]/, ‘’) #=> “hll
“hello”.gsub(/([aeiou])/, ‘’) #=> “hll”
“hello”.gsub(/./) {|s| s.ord.to_s + ‘ ‘} #=> “104 101 108 108 111 “
“hello”.gsub(/(?[aeiou])/, ‘{\k}’) #=> “h{e}ll{o}”
‘hello’.gsub(/[eo]/, ‘e’ => 3, ‘o’ => ‘
’) #=> “h3ll*”

“Super guper bag”.gsub “g”, “d” # => “Super duper bad”
“Ruby is 13%”.gsub( ‘%’ ) {|c| c.ord.to_s + ‘ ‘ } # => “Ruby is 1337”

29
Q

.replace

A

Replaces the contents and taintedness of str with the corresponding values in other_str.

s = “hello” #=> “hello”
s.replace “world” #=> “world”

30
Q

.scan (array or block)

A

Array:
a = “cruel world”
a.scan(/\w+/) #=> [“cruel”, “world”]
a.scan(/…/) #=> [“cru”, “el “, “wor”]
a.scan(/(…)/) #=> [[“cru”], [“el “], [“wor”]]

Block:
a = "cruel world"
a.scan(/\w+/) {|w| print "<> " }
print "\n"
=> <> <>
a.scan(/(.)(.)/) {|x,y| print y, x }
print "\n"
=> rceu lowlr
31
Q

.strip

A

strips whitespace. ie:
“ hello “.strip #=> “hello”
“\tgoodbye\r\n”.strip #=> “goodbye”

32
Q

.floor

A

The .floor method rounds a float (a number with a decimal) down to the nearest integer.

33
Q

.upto

A

iterates thru successive values, returning those values. ie:
3.upto(10) {|x| print x, “ “} => 3 4 5 6 7 8 9 10
“9”.upto(“11”).to_a #=> [“9”, “10”, “11”]

34
Q
  • ( *args) or splat argument
A
  • tells Ruby that there could be more than 1 argument. ie:
    #in this example *args takes 3 arguments:
    def what_up(greeting, *args)
    args.each { |bro| puts “#{greeting}, #{bro}?” }
    end
    what_up(“What up”, “Justin”, “Ben”, “Kevin”)

=> What up, Justin?
What up, Ben?
What up, Kevin?

35
Q

proc (procedure)

A

http://ruby-doc.org/core-2.0/Proc.html

36
Q

yield

A
used to "invoke" a block. useful for passing arguments within a method. ie:
def double(number)
    yield(4)           => 8
    yield(8)           => 16
    yield(12)          => 24
    yield(number) => 12
end
double(6) { |n| puts "#{n * 2}"  }
37
Q

:: (scope resolution operator)

A

Tells Ruby where to look for a specific bit of code. If we say Math::PI, Ruby knows to look inside the Math module to get that PI, not any other PI.

38
Q

Domain

A

A domain is a field of study that defines a set of common requirements, terminology, and functionality for any software program constructed to solve a problem in the area of computer programming, known as domain engineering.

39
Q

inject

A
Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.
# Same using a block and inject
(1..3).inject { |sum, n| sum + n }            #=> 6
# Same using a block
(5..10).inject(1) { |product, n| product * n } #=> 151200
40
Q

map

A

The map method can be used to create a new array based on the original array, but with the values modified by the supplied block:
arr = [1, 2, 3, 4, 5]
arr.map { |a| 2*a } #=> [2, 4, 6, 8, 10]
arr #=> [1, 2, 3, 4, 5]
#use of “!” is destructive
arr.map! { |a| a**2 } #=> [1, 4, 9, 16, 25]
arr #=> [1, 4, 9, 16, 25]

41
Q

CRUD

A

Create, Retrieve, Update, Delete