Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
11 Cards in this Set
- Front
- Back
Escaping quotes in a string
|
"\"Pigs\" is double quoted, and this is a backslash \\"
will print: "Pigs" is double quoted, and this is a backslash \ |
|
How to use string interpolation and to prefer it to concatenation.
|
Interpolation
It is very common to build up strings from other strings. worst_day = "Monday" "#{worst_day}s are the hardest." # => "Mondays are the hardest. Interpolation is usually the idiomatic, clean way to build up strings. Sometimes you'll want more power. Strings have methods that correspond to array's << and + methods: For example: likes = "race cars, lasers, aeroplanes" dislikes = "harmonicas" "I like " + likes + ". I don't like " + dislikes + "." # => "I like race cars, lasers, aeroplanes. I don't like harmonicas." You'd probably prefer string interpolation here, but concatenation is an option. And not true: harmonicas are cool. It is preferred to use the shovel (<<) operator for this; you'll look cooler: count_in = "" count_in << "One, " count_in << "two, " # ... |
|
Accessing a substring
|
"this is my sentence"[5..6]
# => "is" |
|
Split a string into parts
|
ice_creams = "Bi-Rite, Humphrey Slocum, Mitchell's"
ice_creams.split(", ") #=> ["Bi-Rite", "Humphrey Slocum", "Mitchell's"] motto = "We all scream for ice cream!" motto.split(" ") ["We", "all", "scream", "for", "ice", "cream!"] |
|
Chomp
|
Returns a new String with the given record separator removed from the end of str (if present). If $/ has not been changed from the default Ruby record separator, then chomp also removes carriage return characters (that is it will remove \n, \r, and \r\n).
"hello".chomp #=> "hello" "hello\n".chomp #=> "hello" "hello\r\n".chomp #=> "hello" "hello\n\r".chomp #=> "hello\n" "hello\r".chomp #=> "hello" "hello \n there".chomp #=> "hello \n there" "hello".chomp("llo") #=> "he" |
|
Strip
|
Returns a copy of str with leading and trailing whitespace removed.
" hello ".strip #=> "hello" "\tgoodbye\r\n".strip #=> "goodbye" |
|
.to_i
|
"12345".to_i #=> 12345
"99 red balloons".to_i #=> 99 "0a".to_i #=> 0 "0a".to_i(16) #=> 10 "hello".to_i #=> 0 "1100101".to_i(2) #=> 101 "1100101".to_i(8) #=> 294977 "1100101".to_i(10) #=> 1100101 "1100101".to_i(16) #=> 17826049 |
|
.to_sym
|
"Koala".intern #=> :Koala
s = 'cat'.to_sym #=> :cat s == :cat #=> true s = '@cat'.to_sym #=> :@cat s == :@cat #=> true |
|
*
|
Copy—Returns a new String containing integer copies of the receiver.
"Ho! " * 3 #=> "Ho! Ho! Ho! " |
|
.each_byte
|
Passes each byte in str to the given block, or returns an enumerator if no block is given.
"hello".each_byte {|c| print c, ' ' } produces: 104 101 108 108 111 |
|
gsub
|
Returns a copy of str with the all occurrences of pattern substituted for the second argument. The pattern is typically a Regexp;
result.gsub(Regexp.union(digits_values.keys), digits_values) |