Flashcards


String
chomp

"hello".chomp #=> "hello"
"hello\n".chomp #=> "hello"
"hello\r\n".chomp #=> "hello"

  • Methodchomp
  • Aliaschomp
String
partition

"hello".partition("l") #=> ["he", "l", "lo"]
"hello".partition("x") #=> ["hello", "", ""]
"hello".partition(/.l/) #=> ["h", "el", "lo"]

  • Methodpartition
  • Aliaspartition
String
rindex

"hello".rindex('e') #=> 1
"hello".rindex(?e) #=> 1
"hello".rindex(/[aeiou]/, -2) #=> 1

  • Methodrindex
  • Aliasrindex
Array
delete

a.delete("b") #=> "b"
a.delete("z") #=> nil
a.delete("z") { "not found" } #=> "not found"

  • Methodchomp
  • Aliaschomp
Array
map

a.collect { |x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
a.map.with_index{ |x, i| x * i } #=> ["", "b", "cc", "ddd"]
a #=> ["a", "b", "c", "d"]

  • Methodmap
  • Aliascollect
Hash
has_key?

h = { "a" => 100, "b" => 200 }
h.has_key?("a") #=> true
h.has_key?("z") #=> false

  • Methodhas_key?
  • Aliasinclude?, key?, member?
Hash
fetch

h.fetch("a") #=> 100
h.fetch("z", "go fish") #=> "go fish"
h.fetch("z") { |el| "go, #{el}"} #=> "go fish, z"

  • Methodfetch
  • Aliasfetch
Hash
flatten

a = {1=> "one", 2 => [2,"two"], 3 => "three"}
a.flatten # => [1, "one", 2, [2, "two"], 3, "three"]
a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]

  • Methodflatten
  • Aliasflatten
Fixnum
next

1.next #=> 2
(-1).next #=> 0

  • Methodnext
  • Aliassucc