Skip to content

Instantly share code, notes, and snippets.

@campeterson
Forked from coderberry/notes.rb
Created October 24, 2012 04:17
Show Gist options
  • Save campeterson/3943779 to your computer and use it in GitHub Desktop.
Save campeterson/3943779 to your computer and use it in GitHub Desktop.
urug notes 10/23/2012
[1, 2, 3] * ',' # => "1,2,3"
[1, 2, 3] + [3, 4, 5] # => [1, 2, 3, 3, 4, 5]
[1, 2, 3] << "a" << "b" #=> [1, 2, 3, "a", "b"]
[ "a", "a", "c" ] <=> [ "a", "b", "c" ]
a = Array.new
a[4] = "hello" => [nil, nil, nil, nil, "hello"]
a = [ "a", "b", "c", "d" ]
a.collect {|x| x + "!" }
a
a.collect! {|x| x + "!" }
a
[ "a", nil, "b", nil, "c", nil ].compact
[ "a", nil, "b", nil, "c", nil ].compact!
ary = [1, 2, 4, 2]
ary.count #=> 4
ary.count(2) #=> 2
ary.count{|x|x%2==0} #=> 3
a = [ "a", "b", "c" ]
a.delete_if {|x| x >= "b" } #=> ["a"]
a = %w{ a b c d e f }
a.keep_if {|v| v =~ /[aeiou]/} #=> ["a", "e"]
a = [ "a", "b", "c" ]
a.each_index {|x| print x, " -- " }
a.first
a.last
a = [ "a", "b", "c", "d" ]
a.pop #=> "d"
a.pop(2) #=> ["b", "c"]
a #=> ["a"]
a = [ "a", "b", "c" ]
a.reverse_each {|x| print x, " " }
a = [ "a", "b", "c", "d" ]
a.rotate #=> ["b", "c", "d", "a"]
a #=> ["a", "b", "c", "d"]
a.rotate(2) #=> ["c", "d", "a", "b"]
a.rotate(-3) #=> ["b", "c", "d", "a"]
a.sample
a = %w{ a b c d e f }
a.select {|v| v =~ /[aeiou]/} #=> ["a", "e"]
a = [ 1, 2, 3 ] #=> [1, 2, 3]
a.shuffle #=> [2, 3, 1]
a = [1, 2, 3, 4, 5, 0]
a.take(3) #=> [1, 2, 3]
a1 = ["a", "b", "e"]
a2 = ["b", "e", "g"]
a1 & a2 #=> => ["b", "e"]
a1 | a2 #=> ["a", "b", "e", "g"]
[ "a", "b", "c" ] | [ "c", "d", "a" ] #=> [ "a", "b", "c", "d" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment