Create
Learn
Share

Ruby 2

rename
Updated 2007-01-14 00:50

Lists and Maps

Question Answer
merge 2 listsl + myOtherList
subtract listsl - myOtherList
grep a listl.grep( /a/ )
find largest value (in a list)l.max()
create a mapm = {"color" => "Red", "b" => "Bar"}
access item in a mapm["color"]
define a property (in a class)@myProp
memorize

Files / IO

Question Answer
create a filef = File.new( "f.txt" )
read text from fileIO.readlines("file.txt")
write text to filef << "hi"
get text from a url
require 'net/http'
Net::HTTP.get_print URI.parse('http://www.google.com/')
memorize

Searching

Question Answer
check for regex match/c/ =~ "abc"
check for regex match (whole string)"ccc" =~ /^c+$/
find and display match
matcher = /(b+)/.match("abbc")
println matcher[1]
search and replace"abc".gsub( /b/, "bee" )
search and replace (case insensitive)"bBb".gsub( /B/i, "bee" )
search and replace (using groups)"abbc".gsub( /(b+)/, " \\1 " )
memorize