Skip to content

Instantly share code, notes, and snippets.

@Coolagin
Created June 25, 2012 18:33
Show Gist options
  • Save Coolagin/2990403 to your computer and use it in GitHub Desktop.
Save Coolagin/2990403 to your computer and use it in GitHub Desktop.
ruby cheat sheet

#####Strings

""               #expansion
String.new       #constructor
''               #no expansion
%{}              #keeps formatting
%q{}             #keeps formatting no expansion
%w{a b c}        #returns an array of strings no expansion
string = <<EOF   #here document

#####Regexes

/literal/
%r{literal}
Regexp.new 'pattern' #string or literal
Regexp#match 'string'
=~ returns an index or nil
if the pattern has groups, $1, $2, etc.. can be used to reference them after amatch

######Character classes

[:alnum:] Alphanumeric
[:alpha:] Uppercase or lowercase letter
[:blank:] Blank and tab
[:cntrl:] Control characters (at least 0x00?0x1f, 0x7f)
[:digit:] Digit
[:graph:] Printable character excluding space
[:lower:] Lowercase letter
[:print:] Any printable character (including space)
[:punct:] Printable character excluding space and alphanumeric
[:space:] Whitespace (same as \s)
[:upper:] Uppercase letter
[:xdigit:] Hex digit (0?9, a?f, A?F)

######OO regexes:

re = /(\d+):(\d+)/ 		        # match a time hh:mm
md = re.match("Time: 12:34am")
md.class ? MatchData
md[0] 					# == $& ? "12:34"
md[1] 					# == $1 ? "12"
md[2] 					# == $2 ? "34"
md.pre_match			        # == $` ? "Time: "
md.post_match 			        # == $' ? "am"

#####Inheritance and modules

#####Files and directories

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment