|
Lists and Maps
| Question | Answer |
| merge 2 lists | l + myOtherList |
| subtract lists | l - myOtherList |
| grep a list | l.grep( /a/ ) |
| find largest value (in a list) | l.max() |
| create a map | m = {"color" => "Red", "b" => "Bar"} |
| access item in a map | m["color"] |
| define a property (in a class) | @myProp |
start learning | start learning |
Files / IO
| Question | Answer |
| create a file | f = File.new( "f.txt" ) |
| read text from file | IO.readlines("file.txt") |
| write text to file | f << "hi" |
| get text from a url | require 'net/http' Net::HTTP.get_print URI.parse('http://www.google.com/') |
start learning | start learning |
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 " ) |
start learning | start learning |
Pages linking here (main versions)
Ruby
|
|
|
| |