Ruby On Rails Flashcards
Studying (42 cards)
Ask name & print “Hello, _____!”
print “What’s your name ?”
name = gets.chomp
p “Hello, #{name}!”
\t
\s
tab in Ruby. ie. “Bob\tDoe” prints
“Bob Doe”
space in Ruby ie. “Bob\sDoe” prints
“Bob Doe”
.split
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”, “”, “”]
«HERE
(HERE document)
Great for adding large sets of text.
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.
.include?
Does something include?
Ie. letters = ‘a’..’z’
letters.include?(‘h’)
=> true
.squeeze
Removes trailing spaces.
Ie. name = “Jane “
name.squeeze returns “Jane “
.each
Will return each piece of data within object called. Ie. range = (0..4) range.each {|n| puts n} 0 1 2 3 4
.to_a
Turns data into an array.
Ie. digits = 0..3
num_array = digits.to_a
=> [0, 1, 2, 3]
$ variables
$ makes variable global
Ie. $salary = 40000
hash = {}
Holds keys with values Ie. nums = { 'Dave' => '1234', 'Bill' => '2345' } nums['Dave'] => "1234" OR new way is Dave: 1234, Bill: 2345 Nums[:Dave] => 1234
Constants
Variable in all uppercase:
PI = 3.14
.to_s
Converts data to string
Ie. age = Integer(gets)
puts “Being “ + age.to_s + “ feels just like “ + (age-1).to_s + “!”
.rand
Randomizer Ie. nums = [] i = 0 while i < 5 nums[i] = .rand(101) i += 1 End
Parallel assignment
a, b = b, a
This will swap the values of the variables. Nice to use with data sorting w/o introducing new variable.
defined?
Defines data:
Ie. a = 1
defined? a
=> “local-variable”
.step
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
case (when)
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)
break
use cautiously
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
redo
Restarts program if activated. Ie: if data/input == 0 then redo end
next
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
exit
Exits program. Ie: elsif tries == 3 puts "Sorry answer is apple." exit else puts "Try again."
require
Includes or “requires” the use of the named file:
require ‘./tempconvert’
puts ftoc(212) puts ctof(0)
rescue
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
Debug
(In terminal)
ruby -r debug file.rb