Show Menu
Cheatography

Ruby strings Cheat Sheet (DRAFT) by

common string methods in ruby

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Repres­ent­ation

'string literal in single quote'
"­escaped string \t \n"
%q[a string]
single quoted string
%Q[escaped \t]
double quoted string
<<EOF {{multiline string}} EOF
double quoted string
<<'EOF' {{multiline string}} EOF
single quoted string
<<-EOF {{multiline string}} EOF
preserve line indent­ation
<<E­OF.#­{m­ethod} {{multiline string}} EOF
method is defined as a String instance method
<<eof str1 #{eof2} str2 eof
here doc can be nested

Length

s.length, s.size

Iterate Line, Byte, Char

each_char
{|char| puts char}
each_line
{|line| puts line}
each_byte
{|byte| puts byte}
each_l­ine.wi­th_­index
{|line, index| puts line, index}
s.scan­(/./) {|char| puts char}
. matches a char

Specia­lized string comparison

alias old_co­mpare <=>
* use the old_co­mpare in the new <=> method
redefine == method
Redefine <=> method under String class

Tokenize

s1 = “It was a dark”; s1.split
# ["It­", "­was­", "­dar­k"]
s2.spl­it(“, “)
use a string
s3.split(/ and /)
use regex
s = “alpha­,be­ta,­gam­ma,,”
s.spli­t(“­,”,4)
#["a­lph­a", "­bet­a", "­gam­ma", "­,"] - rest of the string
s.spli­t(“­,”,-1)
["al­pha­", "­bet­a", "­gam­ma", "­", "­"] - empty matches

Format a string

 
 

Scan a string

s ="a,­a,a­"; s.scan­("a")
=> ["a", "­a", "­a"]
s.scan­(/\w+/)
regex or string
str.sc­an(­/\w+/) {|x| puts x }
each scan result is passed to the block
String­Scanner
require 'strscan'
ss = String­Sca­nne­r.n­ew(str)
str = “Watch how I soar!”
ss.bol?
beginning of line
ss.eos?
end of string
ss.pos, ss.pos=
query or set current position
ss.match, ss.mat­ched?, ss.matched
check if a match exists at pos
ss.pre­_match, ss.pos­t_match
check a pre or post match - only if matched?
ss.rest, ss.rest?
get the rest of the string
ss.reset, ss.ter­minate
move to beginning or end
ss.scan, ss.unscan
scan and unscan
ss.exist?
check if a substring exits in the rest
String­Scanner acts like a file pointer - it forwards the pointer when there is a match, thus allowing for multiple scan capabi­lities on the same string.
* ususally methods ending with ? returns boolean

Sub strings

s[7,4]
start index, length
s[-3,3]
index from end (end is -1), length
s[8..13]
index range
s[/foo/]; s["f­oo"]
regex or string
s[4]
single index
s[8..13]=
All above sub string look ups can also be used for assignment

Substitute / replace a string

s.sub(­pat­tern, replac­ement)
pattern - regex or string
s.sub(­pat­tern) {block}
each match of the pattern is passed to the block
special symbols \1, \2 can be used in replac­ement string; gsub is a method which replaces all matches while sub does it only for first match
sub!, gsub! - inplace replac­ements
 

Search a string

s.inde­x(s­tri­ng|­regex) => int
s.rind­ex(­str­ing­|regex) => int
s.incl­ude­?(s­tri­ng|­regex) =>bool
s.scan­(st­r|reg) => array

ASCII and Char conversion

"­A".ord => 65
65.chr => "­A"
233.ch­r('­UTF-8') => "­é"