Skip to content

Instantly share code, notes, and snippets.

@AlexVPopov
Created December 11, 2013 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexVPopov/7910165 to your computer and use it in GitHub Desktop.
Save AlexVPopov/7910165 to your computer and use it in GitHub Desktop.
Ruby cheatsheet
__foo__ = "This foo sentence contains many foo instances, for example: foo, foo, foo."
* Match a single symbol: `foo.match /./ # => #<MatchData "T">`
* Match the first-found word character symbol: `foo.match /\w/ => #<MatchData "T">`
* Extract all symbols of a string: `foo.scan /./ # => ["T",
"h",
"i",
"s",
" ",
"f",
"o",
"o",
" ",
"s",
"e",
"n",
"t",
"e",
"n",
"c",
"e",
" ",
"c",
"o",
"n",
"t",
"a",
"i",
"n",
"s",
" ",
"m",
"a",
"n",
"y",
" ",
"f",
"o",
"o",
" ",
"i",
"n",
"s",
"t",
"a",
"n",
"c",
"e",
"s",
",",
" ",
"f",
"o",
"r",
" ",
"e",
"x",
"a",
"m",
"p",
"l",
"e",
":",
" ",
"f",
"o",
"o",
",",
" ",
"f",
"o",
"o",
",",
" ",
"f",
"o",
"o",
"."] `
* Extract n-member group of symbols - e.g. groups of 3 symbols `foo.scan /.../ # => ["Thi",
"s f",
"oo ",
"sen",
"ten",
"ce ",
"con",
"tai",
"ns ",
"man",
"y f",
"oo ",
"ins",
"tan",
"ces",
", f",
"or ",
"exa",
"mpl",
"e: ",
"foo",
", f",
"oo,",
" fo"]`
* Match the whole string: `foo.match /.+/ # => #<MatchData "This foo sentence contains many foo instances, for example: foo, foo, foo.">`
`foo.match /.*/ # => #<MatchData "This foo sentence contains many foo instances, for example: foo, foo, foo.">`
> Written with [StackEdit](https://stackedit.io/).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment