Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby things

Julio Santos
September 19, 2012

Ruby things

Some ruby surprises, tricks, oddities. There should be something surprising to all rubyists.

Julio Santos

September 19, 2012
Tweet

Other Decks in Programming

Transcript

  1. 1.8.7-p358 :001 > s = “this” “is” “a” “thing” =>

    “thisisathing” 1.8.7-p358 :002 > Wednesday, September 19, 12
  2. 1.8.7-p358 :001 > s = % qwerty\ azerty .size =>

    13 Wednesday, September 19, 12
  3. 1.8.7-p358 :001 > require “rubygems” => true 1.8.7-p358 :002 >

    require “json” => true 1.8.7-p358 :003 > h = {:one => 1, :two => 2} => {:one=>1, :two=>2} 1.8.7-p358 :004 > j h Wednesday, September 19, 12
  4. 1.8.7-p358 :001 > require “rubygems” => true 1.8.7-p358 :002 >

    require “json” => true 1.8.7-p358 :003 > h = {:one => 1, :two => 2} => {:one=>1, :two=>2} 1.8.7-p358 :004 > j h {"one":1,"two":2} Wednesday, September 19, 12
  5. 1.8.7-p358 :001 > require “rubygems” => true 1.8.7-p358 :002 >

    require “json” => true 1.8.7-p358 :003 > h = {:one => 1, :two => 2} => {:one=>1, :two=>2} 1.8.7-p358 :004 > j h {"one":1,"two":2} 1.8.7-p358 :005 > jj h Wednesday, September 19, 12
  6. 1.8.7-p358 :001 > require “rubygems” => true 1.8.7-p358 :002 >

    require “json” => true 1.8.7-p358 :003 > h = {:one => 1, :two => 2} => {:one=>1, :two=>2} 1.8.7-p358 :004 > j h {"one":1,"two":2} 1.8.7-p358 :005 > jj h { "one": 1, "two": 2 } 1.8.7-p358 :006 > Wednesday, September 19, 12
  7. 1.8.7-p358 :001 > @name = "bob" => “bob” 1.8.7-p358 :002

    > puts "hello #@name" Wednesday, September 19, 12
  8. 1.8.7-p358 :001 > @name = "bob" => “bob” 1.8.7-p358 :002

    > puts "hello #@name" “hello bob” => nil Wednesday, September 19, 12
  9. 1.8.7-p358 :001 > a = (1..2) => 1..2 1.8.7-p358 :002

    > a.to_a Wednesday, September 19, 12
  10. 1.8.7-p358 :001 > a = (1..2) => 1..2 1.8.7-p358 :002

    > a.to_a => [1, 2] Wednesday, September 19, 12
  11. 1.8.7-p358 :001 > a = (1..2) => 1..2 1.8.7-p358 :002

    > a.to_a => [1, 2] 1.8.7-p358 :003 > b = (1...2) Wednesday, September 19, 12
  12. 1.8.7-p358 :001 > a = (1..2) => 1..2 1.8.7-p358 :002

    > a.to_a => [1, 2] 1.8.7-p358 :003 > b = (1...2) => 1...2 Wednesday, September 19, 12
  13. 1.8.7-p358 :001 > a = (1..2) => 1..2 1.8.7-p358 :002

    > a.to_a => [1, 2] 1.8.7-p358 :003 > b = (1...2) => 1...2 1.8.7-p358 :004 > b.to_a Wednesday, September 19, 12
  14. 1.8.7-p358 :001 > a = (1..2) => 1..2 1.8.7-p358 :002

    > a.to_a => [1, 2] 1.8.7-p358 :003 > b = (1...2) => 1...2 1.8.7-p358 :004 > b.to_a => [1] Wednesday, September 19, 12
  15. 1.8.7-p358 :001 > s = "this is a string" =>

    “this is a string” 1.8.7-p358 :002 > s[“is a”] Wednesday, September 19, 12
  16. 1.8.7-p358 :001 > s = "this is a string" =>

    “this is a string” 1.8.7-p358 :002 > s[“is a”] => “is a” Wednesday, September 19, 12
  17. 1.8.7-p358 :001 > s = "this is a string" =>

    “this is a string” 1.8.7-p358 :002 > s[“is a”] => “is a” 1.8.7-p358 :003 > s["is not a"] Wednesday, September 19, 12
  18. 1.8.7-p358 :001 > s = "this is a string" =>

    “this is a string” 1.8.7-p358 :002 > s[“is a”] => “is a” 1.8.7-p358 :003 > s["is not a"] => nil Wednesday, September 19, 12
  19. 1.8.7-p358 :001 > [*10..20] => [10, 11, 12, 13, 14,

    15, 16, 17, 18, 19, 20] Wednesday, September 19, 12
  20. 1.8.7-p358 :001 > Hash[1,2,3,4] => {1=>2, 3=>4} 1.8.7-p358 :002 >

    Hash[["one", 1], ["two", 2]] Wednesday, September 19, 12
  21. 1.8.7-p358 :001 > Hash[1,2,3,4] => {1=>2, 3=>4} 1.8.7-p358 :002 >

    Hash[["one", 1], ["two", 2]] => {["one", 1]=>["two", 2]} Wednesday, September 19, 12
  22. 1.8.7-p358 :001 > Hash[1,2,3,4] => {1=>2, 3=>4} 1.8.7-p358 :002 >

    Hash[["one", 1], ["two", 2]] => {["one", 1]=>["two", 2]} 1.8.7-p358 :003 > a=["one", 1], ["two", 2] Wednesday, September 19, 12
  23. 1.8.7-p358 :001 > Hash[1,2,3,4] => {1=>2, 3=>4} 1.8.7-p358 :002 >

    Hash[["one", 1], ["two", 2]] => {["one", 1]=>["two", 2]} 1.8.7-p358 :003 > a=["one", 1], ["two", 2] => [["one", 1], ["two", 2]] Wednesday, September 19, 12
  24. 1.8.7-p358 :001 > Hash[1,2,3,4] => {1=>2, 3=>4} 1.8.7-p358 :002 >

    Hash[["one", 1], ["two", 2]] => {["one", 1]=>["two", 2]} 1.8.7-p358 :003 > a=["one", 1], ["two", 2] => [["one", 1], ["two", 2]] 1.8.7-p358 :004 > Hash[a] Wednesday, September 19, 12
  25. 1.8.7-p358 :001 > Hash[1,2,3,4] => {1=>2, 3=>4} 1.8.7-p358 :002 >

    Hash[["one", 1], ["two", 2]] => {["one", 1]=>["two", 2]} 1.8.7-p358 :003 > a=["one", 1], ["two", 2] => [["one", 1], ["two", 2]] 1.8.7-p358 :004 > Hash[a] => {"two"=>2, "one"=>1} 1.8.7-p358 :005 > b = [1,2,3,4] Wednesday, September 19, 12
  26. 1.8.7-p358 :001 > Hash[1,2,3,4] => {1=>2, 3=>4} 1.8.7-p358 :002 >

    Hash[["one", 1], ["two", 2]] => {["one", 1]=>["two", 2]} 1.8.7-p358 :003 > a=["one", 1], ["two", 2] => [["one", 1], ["two", 2]] 1.8.7-p358 :004 > Hash[a] => {"two"=>2, "one"=>1} 1.8.7-p358 :005 > b = [1,2,3,4] => [1,2,3,4] Wednesday, September 19, 12
  27. 1.8.7-p358 :001 > Hash[1,2,3,4] => {1=>2, 3=>4} 1.8.7-p358 :002 >

    Hash[["one", 1], ["two", 2]] => {["one", 1]=>["two", 2]} 1.8.7-p358 :003 > a=["one", 1], ["two", 2] => [["one", 1], ["two", 2]] 1.8.7-p358 :004 > Hash[a] => {"two"=>2, "one"=>1} 1.8.7-p358 :005 > b = [1,2,3,4] => [1,2,3,4] 1.8.7-p358 :006 > Hash[*b] Wednesday, September 19, 12
  28. 1.8.7-p358 :001 > Hash[1,2,3,4] => {1=>2, 3=>4} 1.8.7-p358 :002 >

    Hash[["one", 1], ["two", 2]] => {["one", 1]=>["two", 2]} 1.8.7-p358 :003 > a=["one", 1], ["two", 2] => [["one", 1], ["two", 2]] 1.8.7-p358 :004 > Hash[a] => {"two"=>2, "one"=>1} 1.8.7-p358 :005 > b = [1,2,3,4] => [1,2,3,4] 1.8.7-p358 :006 > Hash[*b] => {1=>2, 3=>4} Wednesday, September 19, 12
  29. 1.8.7-p358 :001 > keys = [:one, :two, :three] => [:one,

    :two, :three] 1.8.7-p358 :002 > values = [1,2,3] => [1, 2, 3] 1.8.7-p358 :003 > zip = keys.zip(values) Wednesday, September 19, 12
  30. 1.8.7-p358 :001 > keys = [:one, :two, :three] => [:one,

    :two, :three] 1.8.7-p358 :002 > values = [1,2,3] => [1, 2, 3] 1.8.7-p358 :003 > zip = keys.zip(values) => [[:one, 1], [:two, 2], [:three, 3]] Wednesday, September 19, 12
  31. 1.8.7-p358 :001 > keys = [:one, :two, :three] => [:one,

    :two, :three] 1.8.7-p358 :002 > values = [1,2,3] => [1, 2, 3] 1.8.7-p358 :003 > zip = keys.zip(values) => [[:one, 1], [:two, 2], [:three, 3]] 1.8.7-p358 :004 > Hash[zip] Wednesday, September 19, 12
  32. 1.8.7-p358 :001 > keys = [:one, :two, :three] => [:one,

    :two, :three] 1.8.7-p358 :002 > values = [1,2,3] => [1, 2, 3] 1.8.7-p358 :003 > zip = keys.zip(values) => [[:one, 1], [:two, 2], [:three, 3]] 1.8.7-p358 :004 > Hash[zip] => {:two=>2, :three=>3, :one=>1} Wednesday, September 19, 12
  33. 1.8.7-p358 :001 > a = [1,2,3] => [1, 2, 3]

    1.8.7-p358 :002 > a.shuffle Wednesday, September 19, 12
  34. 1.8.7-p358 :001 > a = [1,2,3] => [1, 2, 3]

    1.8.7-p358 :002 > a.shuffle => [1, 3, 2] Wednesday, September 19, 12
  35. 1.8.7-p358 :001 > a = [1,2,3] => [1, 2, 3]

    1.8.7-p358 :002 > a.shuffle => [1, 3, 2] 1.8.7-p358 :003 > a.shuffle Wednesday, September 19, 12
  36. 1.8.7-p358 :001 > a = [1,2,3] => [1, 2, 3]

    1.8.7-p358 :002 > a.shuffle => [1, 3, 2] 1.8.7-p358 :003 > a.shuffle => [3, 2, 1] Wednesday, September 19, 12
  37. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > a.each_cons(2) {|pair| p pair} Wednesday, September 19, 12
  38. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > a.each_cons(2) {|pair| p pair} [1, 2] [2, 3] [3, 4] [4, 5] => nil Wednesday, September 19, 12
  39. 1.8.7-p358 :001 > require 'set' => true 1.8.7-p358 :002 >

    s = Set.new => #<Set: {}> 1.8.7-p358 :003 > s << 1 Wednesday, September 19, 12
  40. 1.8.7-p358 :001 > require 'set' => true 1.8.7-p358 :002 >

    s = Set.new => #<Set: {}> 1.8.7-p358 :003 > s << 1 => #<Set: {1}> Wednesday, September 19, 12
  41. 1.8.7-p358 :001 > require 'set' => true 1.8.7-p358 :002 >

    s = Set.new => #<Set: {}> 1.8.7-p358 :003 > s << 1 => #<Set: {1}> 1.8.7-p358 :004 > s << 1 Wednesday, September 19, 12
  42. 1.8.7-p358 :001 > require 'set' => true 1.8.7-p358 :002 >

    s = Set.new => #<Set: {}> 1.8.7-p358 :003 > s << 1 => #<Set: {1}> 1.8.7-p358 :004 > s << 1 => #<Set: {1}> Wednesday, September 19, 12
  43. 1.8.7-p358 :001 > require 'set' => true 1.8.7-p358 :002 >

    s = Set.new => #<Set: {}> 1.8.7-p358 :003 > s << 1 => #<Set: {1}> 1.8.7-p358 :004 > s << 1 => #<Set: {1}> 1.8.7-p358 :005 > s << 2 Wednesday, September 19, 12
  44. 1.8.7-p358 :001 > require 'set' => true 1.8.7-p358 :002 >

    s = Set.new => #<Set: {}> 1.8.7-p358 :003 > s << 1 => #<Set: {1}> 1.8.7-p358 :004 > s << 1 => #<Set: {1}> 1.8.7-p358 :005 > s << 2 => #<Set: {1, 2}> Wednesday, September 19, 12
  45. 1.8.7-p358 :001 > module A; def foo; "A"; end; end

    => nil Wednesday, September 19, 12
  46. 1.8.7-p358 :001 > module A; def foo; "A"; end; end

    => nil 1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil Wednesday, September 19, 12
  47. 1.8.7-p358 :001 > module A; def foo; "A"; end; end

    => nil 1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil 1.8.7-p358 :003 > class C; include A; include B; end => C Wednesday, September 19, 12
  48. 1.8.7-p358 :001 > module A; def foo; "A"; end; end

    => nil 1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil 1.8.7-p358 :003 > class C; include A; include B; end => C 1.8.7-p358 :004 > C.new.foo Wednesday, September 19, 12
  49. 1.8.7-p358 :001 > module A; def foo; "A"; end; end

    => nil 1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil 1.8.7-p358 :003 > class C; include A; include B; end => C 1.8.7-p358 :004 > C.new.foo => “B” Wednesday, September 19, 12
  50. 1.8.7-p358 :001 > module A; def foo; "A"; end; end

    => nil 1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil 1.8.7-p358 :003 > class C; include A; include B; end => C 1.8.7-p358 :004 > C.new.foo => “B” 1.8.7-p358 :005 > class D; include A, B; end Wednesday, September 19, 12
  51. 1.8.7-p358 :001 > module A; def foo; "A"; end; end

    => nil 1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil 1.8.7-p358 :003 > class C; include A; include B; end => C 1.8.7-p358 :004 > C.new.foo => “B” 1.8.7-p358 :005 > class D; include A, B; end => D Wednesday, September 19, 12
  52. 1.8.7-p358 :001 > module A; def foo; "A"; end; end

    => nil 1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil 1.8.7-p358 :003 > class C; include A; include B; end => C 1.8.7-p358 :004 > C.new.foo => “B” 1.8.7-p358 :005 > class D; include A, B; end => D 1.8.7-p358 :006 > D.new.foo Wednesday, September 19, 12
  53. 1.8.7-p358 :001 > module A; def foo; "A"; end; end

    => nil 1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil 1.8.7-p358 :003 > class C; include A; include B; end => C 1.8.7-p358 :004 > C.new.foo => “B” 1.8.7-p358 :005 > class D; include A, B; end => D 1.8.7-p358 :006 > D.new.foo => “A” Wednesday, September 19, 12
  54. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y;

    end => nil 1.8.7-p358 :002 > foo 1 Wednesday, September 19, 12
  55. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y;

    end => nil 1.8.7-p358 :002 > foo 1 12 => nil Wednesday, September 19, 12
  56. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y;

    end => nil 1.8.7-p358 :002 > foo 1 12 => nil 1.8.7-p358 :003 > def bar(x, y=x); print x; print y; end => nil Wednesday, September 19, 12
  57. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y;

    end => nil 1.8.7-p358 :002 > foo 1 12 => nil 1.8.7-p358 :003 > def bar(x, y=x); print x; print y; end => nil 1.8.7-p358 :004 > bar 1 Wednesday, September 19, 12
  58. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y;

    end => nil 1.8.7-p358 :002 > foo 1 12 => nil 1.8.7-p358 :003 > def bar(x, y=x); print x; print y; end => nil 1.8.7-p358 :004 > bar 1 11 => nil Wednesday, September 19, 12
  59. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y;

    end => nil 1.8.7-p358 :002 > foo 1 12 => nil 1.8.7-p358 :003 > def bar(x, y=x); print x; print y; end => nil 1.8.7-p358 :004 > bar 1 11 => nil 1.8.7-p358 :005 > bar 1,2 Wednesday, September 19, 12
  60. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y;

    end => nil 1.8.7-p358 :002 > foo 1 12 => nil 1.8.7-p358 :003 > def bar(x, y=x); print x; print y; end => nil 1.8.7-p358 :004 > bar 1 11 => nil 1.8.7-p358 :005 > bar 1,2 12 => nil Wednesday, September 19, 12
  61. 1.8.7-p358 :001 > def foo; puts caller; "foo"; end =>

    nil 1.8.7-p358 :002 > def bar; foo; end => nil Wednesday, September 19, 12
  62. 1.8.7-p358 :001 > def foo; puts caller; "foo"; end =>

    nil 1.8.7-p358 :002 > def bar; foo; end => nil 1.8.7-p358 :003 > bar Wednesday, September 19, 12
  63. 1.8.7-p358 :001 > def foo; puts caller; "foo"; end =>

    nil 1.8.7-p358 :002 > def bar; foo; end => nil 1.8.7-p358 :003 > bar (irb):5:in `bar' (irb):6:in `irb_binding' /Users/julio/.rvm/rubies/ruby-1.8.7-p358/lib/ruby/1.8/irb/ workspace.rb:52:in `irb_binding' /Users/julio/.rvm/rubies/ruby-1.8.7-p358/lib/ruby/1.8/irb/ workspace.rb:52 => "foo" Wednesday, September 19, 12
  64. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] Wednesday, September 19, 12
  65. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > car, *cdr = a Wednesday, September 19, 12
  66. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > car, *cdr = a => [1, 2, 3, 4, 5] Wednesday, September 19, 12
  67. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > car, *cdr = a => [1, 2, 3, 4, 5] 1.8.7-p358 :003 > car Wednesday, September 19, 12
  68. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > car, *cdr = a => [1, 2, 3, 4, 5] 1.8.7-p358 :003 > car => 1 Wednesday, September 19, 12
  69. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > car, *cdr = a => [1, 2, 3, 4, 5] 1.8.7-p358 :003 > car => 1 1.8.7-p358 :004 > cdr Wednesday, September 19, 12
  70. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > car, *cdr = a => [1, 2, 3, 4, 5] 1.8.7-p358 :003 > car => 1 1.8.7-p358 :004 > cdr => [2, 3, 4, 5] Wednesday, September 19, 12
  71. 1.8.7-p358 :001 > words = %w{ foo bar yo stuff

    } => ["foo", "bar", "yo", "stuff"] Wednesday, September 19, 12
  72. 1.8.7-p358 :001 > words = %w{ foo bar yo stuff

    } => ["foo", "bar", "yo", "stuff"] 1.8.7-p358 :002 > words.group_by {|x| x.size} Wednesday, September 19, 12
  73. 1.8.7-p358 :001 > words = %w{ foo bar yo stuff

    } => ["foo", "bar", "yo", "stuff"] 1.8.7-p358 :002 > words.group_by {|x| x.size} => {5=>["stuff"], 2=>["yo"], 3=>["foo", "bar"]} Wednesday, September 19, 12
  74. 1.8.7-p358 :001 > a = [*1..100] # or (1..100).to_a =>

    [1, 2, 3, 4, ..., 99, 100] Wednesday, September 19, 12
  75. 1.8.7-p358 :001 > a = [*1..100] # or (1..100).to_a =>

    [1, 2, 3, 4, ..., 99, 100] 1.8.7-p358 :002 > t = [*1..100].inject(0) {|a,e| a + e} Wednesday, September 19, 12
  76. 1.8.7-p358 :001 > a = [*1..100] # or (1..100).to_a =>

    [1, 2, 3, 4, ..., 99, 100] 1.8.7-p358 :002 > t = [*1..100].inject(0) {|a,e| a + e} => 5050 Wednesday, September 19, 12
  77. 1.8.7-p358 :001 > a = [*1..100] # or (1..100).to_a =>

    [1, 2, 3, 4, ..., 99, 100] 1.8.7-p358 :002 > t = [*1..100].inject(0) {|a,e| a + e} => 5050 1.8.7-p358 :003 > t = [*1..100].inject {|a,e| a + e} => 5050 Wednesday, September 19, 12
  78. 1.8.7-p358 :001 > a = [*1..100] # or (1..100).to_a =>

    [1, 2, 3, 4, ..., 99, 100] 1.8.7-p358 :002 > t = [*1..100].inject(0) {|a,e| a + e} => 5050 1.8.7-p358 :003 > t = [*1..100].inject {|a,e| a + e} => 5050 1.8.7-p358 :004 > t = [*1..100].inject(&:+) => 5050 Wednesday, September 19, 12
  79. 1.8.7-p358 :001 > a = [*1..100] # or (1..100).to_a =>

    [1, 2, 3, 4, ..., 99, 100] 1.8.7-p358 :002 > t = [*1..100].inject(0) {|a,e| a + e} => 5050 1.8.7-p358 :003 > t = [*1..100].inject {|a,e| a + e} => 5050 1.8.7-p358 :004 > t = [*1..100].inject(&:+) => 5050 1.8.7-p358 :005 > t = [*1..100].inject(:+) => 5050 Wednesday, September 19, 12
  80. 1.8.7-p358 :001 > a = [*1..100] # or (1..100).to_a =>

    [1, 2, 3, 4, ..., 99, 100] 1.8.7-p358 :002 > t = [*1..100].inject(0) {|a,e| a + e} => 5050 1.8.7-p358 :003 > t = [*1..100].inject {|a,e| a + e} => 5050 1.8.7-p358 :004 > t = [*1..100].inject(&:+) => 5050 1.8.7-p358 :005 > t = [*1..100].inject(:+) => 5050 1.8.7-p358 :006 > t = [*1..100].reduce(:+) => 5050 Wednesday, September 19, 12
  81. 1.8.7-p358 :001 > a = [1,2,3,4] => [1, 2, 3,

    4] Wednesday, September 19, 12
  82. 1.8.7-p358 :001 > a = [1,2,3,4] => [1, 2, 3,

    4] 1.8.7-p358 :002 > a *= "," Wednesday, September 19, 12
  83. 1.8.7-p358 :001 > a = [1,2,3,4] => [1, 2, 3,

    4] 1.8.7-p358 :002 > a *= "," => "1,2,3,4" Wednesday, September 19, 12
  84. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] Wednesday, September 19, 12
  85. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > module Mult; def mul; self.reduce(:*); end;end => nil Wednesday, September 19, 12
  86. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > module Mult; def mul; self.reduce(:*); end;end => nil 1.8.7-p358 :003 > a.extend Mult => [1, 2, 3, 4, 5] Wednesday, September 19, 12
  87. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > module Mult; def mul; self.reduce(:*); end;end => nil 1.8.7-p358 :003 > a.extend Mult => [1, 2, 3, 4, 5] 1.8.7-p358 :004 > a.mul Wednesday, September 19, 12
  88. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > module Mult; def mul; self.reduce(:*); end;end => nil 1.8.7-p358 :003 > a.extend Mult => [1, 2, 3, 4, 5] 1.8.7-p358 :004 > a.mul => 120 Wednesday, September 19, 12
  89. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > module Mult; def mul; self.reduce(:*); end;end => nil 1.8.7-p358 :003 > a.extend Mult => [1, 2, 3, 4, 5] 1.8.7-p358 :004 > a.mul => 120 1.8.7-p358 :005 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5] Wednesday, September 19, 12
  90. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > module Mult; def mul; self.reduce(:*); end;end => nil 1.8.7-p358 :003 > a.extend Mult => [1, 2, 3, 4, 5] 1.8.7-p358 :004 > a.mul => 120 1.8.7-p358 :005 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5] 1.8.7-p358 :006 > a.mul Wednesday, September 19, 12
  91. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3,

    4, 5] 1.8.7-p358 :002 > module Mult; def mul; self.reduce(:*); end;end => nil 1.8.7-p358 :003 > a.extend Mult => [1, 2, 3, 4, 5] 1.8.7-p358 :004 > a.mul => 120 1.8.7-p358 :005 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5] 1.8.7-p358 :006 > a.mul => NoMethodError: undefined method `mul' for [1, 2, 3, 4, 5]:Array Wednesday, September 19, 12
  92. 1.8.7-p358 :001 > class C; def foo; "foo"; end; end

    => nil 1.8.7-p358 :002 > c = C.new => #<C:0x10b3e0b28> Wednesday, September 19, 12
  93. 1.8.7-p358 :001 > class C; def foo; "foo"; end; end

    => nil 1.8.7-p358 :002 > c = C.new => #<C:0x10b3e0b28> 1.8.7-p358 :003 > c.methods.grep /foo/ Wednesday, September 19, 12
  94. 1.8.7-p358 :001 > class C; def foo; "foo"; end; end

    => nil 1.8.7-p358 :002 > c = C.new => #<C:0x10b3e0b28> 1.8.7-p358 :003 > c.methods.grep /foo/ => ["foo"] Wednesday, September 19, 12
  95. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil Wednesday, September 19, 12
  96. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil 1.8.7-p358 :003 > A.foo Wednesday, September 19, 12
  97. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil 1.8.7-p358 :003 > A.foo => “A” Wednesday, September 19, 12
  98. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil 1.8.7-p358 :003 > A.foo => “A” 1.8.7-p358 :003 > C.foo Wednesday, September 19, 12
  99. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil 1.8.7-p358 :003 > A.foo => “A” 1.8.7-p358 :003 > C.foo => “A” Wednesday, September 19, 12
  100. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil 1.8.7-p358 :003 > A.foo => “A” 1.8.7-p358 :003 > C.foo => “A” 1.8.7-p358 :003 > class B < A; @@foo="B"; nil; end Wednesday, September 19, 12
  101. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil 1.8.7-p358 :003 > A.foo => “A” 1.8.7-p358 :003 > C.foo => “A” 1.8.7-p358 :003 > class B < A; @@foo="B"; nil; end => nil Wednesday, September 19, 12
  102. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil 1.8.7-p358 :003 > A.foo => “A” 1.8.7-p358 :003 > C.foo => “A” 1.8.7-p358 :003 > class B < A; @@foo="B"; nil; end => nil 1.8.7-p358 :003 > B.foo Wednesday, September 19, 12
  103. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil 1.8.7-p358 :003 > A.foo => “A” 1.8.7-p358 :003 > C.foo => “A” 1.8.7-p358 :003 > class B < A; @@foo="B"; nil; end => nil 1.8.7-p358 :003 > B.foo => “B” Wednesday, September 19, 12
  104. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil 1.8.7-p358 :003 > A.foo => “A” 1.8.7-p358 :003 > C.foo => “A” 1.8.7-p358 :003 > class B < A; @@foo="B"; nil; end => nil 1.8.7-p358 :003 > B.foo => “B” 1.8.7-p358 :003 > A.foo Wednesday, September 19, 12
  105. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil 1.8.7-p358 :003 > A.foo => “A” 1.8.7-p358 :003 > C.foo => “A” 1.8.7-p358 :003 > class B < A; @@foo="B"; nil; end => nil 1.8.7-p358 :003 > B.foo => “B” 1.8.7-p358 :003 > A.foo => “B” Wednesday, September 19, 12
  106. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil 1.8.7-p358 :003 > A.foo => “A” 1.8.7-p358 :003 > C.foo => “A” 1.8.7-p358 :003 > class B < A; @@foo="B"; nil; end => nil 1.8.7-p358 :003 > B.foo => “B” 1.8.7-p358 :003 > A.foo => “B” 1.8.7-p358 :003 > C.foo Wednesday, September 19, 12
  107. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end;

    end => nil 1.8.7-p358 :002 > class C < A; nil; end => nil 1.8.7-p358 :003 > A.foo => “A” 1.8.7-p358 :003 > C.foo => “A” 1.8.7-p358 :003 > class B < A; @@foo="B"; nil; end => nil 1.8.7-p358 :003 > B.foo => “B” 1.8.7-p358 :003 > A.foo => “B” 1.8.7-p358 :003 > C.foo => “B” Wednesday, September 19, 12
  108. 1.8.7-p358 :001 > a = [*1..100] => [1, 2, 3,

    4, 5, 6, .., 99, 100] Wednesday, September 19, 12
  109. 1.8.7-p358 :001 > a = [*1..100] => [1, 2, 3,

    4, 5, 6, .., 99, 100] 1.8.7-p358 :002 > a.each { |x| p x if (x==17..x==42) } Wednesday, September 19, 12
  110. 1.8.7-p358 :001 > a = [*1..100] => [1, 2, 3,

    4, 5, 6, .., 99, 100] 1.8.7-p358 :002 > a.each { |x| p x if (x==17..x==42) } 17 18 ... 41 42 => [1, 2, 3, 4, 5, 6, .., 99, 100] Wednesday, September 19, 12
  111. 1.8.7-p358 :001 > val = 0b11100100 => 228 1.8.7-p358 :002

    > val.class Wednesday, September 19, 12
  112. 1.8.7-p358 :001 > val = 0b11100100 => 228 1.8.7-p358 :002

    > val.class => Fixnum Wednesday, September 19, 12
  113. 1.8.7-p358 :001 > val = 0b11100100 => 228 1.8.7-p358 :002

    > val.class => Fixnum 1.8.7-p358 :003 > val[4] Wednesday, September 19, 12
  114. 1.8.7-p358 :001 > val = 0b11100100 => 228 1.8.7-p358 :002

    > val.class => Fixnum 1.8.7-p358 :003 > val[4] => 0 Wednesday, September 19, 12
  115. 1.8.7-p358 :001 > val = 0b11100100 => 228 1.8.7-p358 :002

    > val.class => Fixnum 1.8.7-p358 :003 > val[4] => 0 1.8.7-p358 :004 > val[5] Wednesday, September 19, 12
  116. 1.8.7-p358 :001 > val = 0b11100100 => 228 1.8.7-p358 :002

    > val.class => Fixnum 1.8.7-p358 :003 > val[4] => 0 1.8.7-p358 :004 > val[5] => 1 Wednesday, September 19, 12
  117. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end 1.8.7-p358

    :002 > foo Wednesday, September 19, 12
  118. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end 1.8.7-p358

    :002 > foo => 0 Wednesday, September 19, 12
  119. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end 1.8.7-p358

    :002 > foo => 0 1.8.7-p358 :003 > def foo; 0; ensure; return 1; end 1.8.7-p358 :004 > foo Wednesday, September 19, 12
  120. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end 1.8.7-p358

    :002 > foo => 0 1.8.7-p358 :003 > def foo; 0; ensure; return 1; end 1.8.7-p358 :004 > foo => 1 Wednesday, September 19, 12
  121. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end 1.8.7-p358

    :002 > foo => 0 1.8.7-p358 :003 > def foo; 0; ensure; return 1; end 1.8.7-p358 :004 > foo => 1 1.8.7-p358 :005 > def foo; return 0; ensure; 1; end 1.8.7-p358 :006 > foo Wednesday, September 19, 12
  122. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end 1.8.7-p358

    :002 > foo => 0 1.8.7-p358 :003 > def foo; 0; ensure; return 1; end 1.8.7-p358 :004 > foo => 1 1.8.7-p358 :005 > def foo; return 0; ensure; 1; end 1.8.7-p358 :006 > foo => 0 Wednesday, September 19, 12
  123. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end 1.8.7-p358

    :002 > foo => 0 1.8.7-p358 :003 > def foo; 0; ensure; return 1; end 1.8.7-p358 :004 > foo => 1 1.8.7-p358 :005 > def foo; return 0; ensure; 1; end 1.8.7-p358 :006 > foo => 0 1.8.7-p358 :007 > def foo; return 0; ensure; return 1; end 1.8.7-p358 :006 > foo Wednesday, September 19, 12
  124. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end 1.8.7-p358

    :002 > foo => 0 1.8.7-p358 :003 > def foo; 0; ensure; return 1; end 1.8.7-p358 :004 > foo => 1 1.8.7-p358 :005 > def foo; return 0; ensure; 1; end 1.8.7-p358 :006 > foo => 0 1.8.7-p358 :007 > def foo; return 0; ensure; return 1; end 1.8.7-p358 :006 > foo => 1 Wednesday, September 19, 12
  125. 1.8.7-p358 :001 > def foo; raise "0"; ensure; "ok"; end

    1.8.7-p358 :002 > foo Wednesday, September 19, 12
  126. 1.8.7-p358 :001 > def foo; raise "0"; ensure; "ok"; end

    1.8.7-p358 :002 > foo => RuntimeError: 0 ! from (irb):01:in `foo' ! from (irb):02 Wednesday, September 19, 12
  127. 1.8.7-p358 :001 > def foo; raise "0"; ensure; "ok"; end

    1.8.7-p358 :002 > foo => RuntimeError: 0 ! from (irb):01:in `foo' ! from (irb):02 1.8.7-p358 :003 > def foo; raise "0"; ensure; return "ok"; end 1.8.7-p358 :004 > foo Wednesday, September 19, 12
  128. 1.8.7-p358 :001 > def foo; raise "0"; ensure; "ok"; end

    1.8.7-p358 :002 > foo => RuntimeError: 0 ! from (irb):01:in `foo' ! from (irb):02 1.8.7-p358 :003 > def foo; raise "0"; ensure; return "ok"; end 1.8.7-p358 :004 > foo => “ok” Wednesday, September 19, 12
  129. 1.8.7-p358 :001 > require ‘benchmark’ => true 1.8.7-p358 :002 >

    n = 50000 003 > Benchmark.bm(7) do |x| 004 > x.report("for:") { for i in 1..n; a = "1"; end } 005 > x.report("times:") { n.times do ; a = "1"; end } 006 > x.report("upto:") { 1.upto(n) do ; a = "1"; end } 007 > end Wednesday, September 19, 12
  130. 1.8.7-p358 :001 > require ‘benchmark’ => true 1.8.7-p358 :002 >

    n = 50000 003 > Benchmark.bm(7) do |x| 004 > x.report("for:") { for i in 1..n; a = "1"; end } 005 > x.report("times:") { n.times do ; a = "1"; end } 006 > x.report("upto:") { 1.upto(n) do ; a = "1"; end } 007 > end user system total real for: 0.020000 0.000000 0.020000 ( 0.017378) times: 0.010000 0.000000 0.010000 ( 0.013688) upto: 0.020000 0.000000 0.020000 ( 0.014495) => true Wednesday, September 19, 12
  131. 1.8.7-p358 :001 > class ExceptionOne < Exception; end => nil

    1.8.7-p358 :002 > ExceptionTwo = Class.new(Exception) => ExceptionTwo Wednesday, September 19, 12
  132. 1.8.7-p358 :001 > class ExceptionOne < Exception; end => nil

    1.8.7-p358 :002 > ExceptionTwo = Class.new(Exception) => ExceptionTwo 1.8.7-p358 :003 > raise ExceptionOne Wednesday, September 19, 12
  133. 1.8.7-p358 :001 > class ExceptionOne < Exception; end => nil

    1.8.7-p358 :002 > ExceptionTwo = Class.new(Exception) => ExceptionTwo 1.8.7-p358 :003 > raise ExceptionOne ExceptionOne: ExceptionOne ! from (irb):3 Wednesday, September 19, 12
  134. 1.8.7-p358 :001 > class ExceptionOne < Exception; end => nil

    1.8.7-p358 :002 > ExceptionTwo = Class.new(Exception) => ExceptionTwo 1.8.7-p358 :003 > raise ExceptionOne ExceptionOne: ExceptionOne ! from (irb):3 1.8.7-p358 :004 > raise ExceptionTwo Wednesday, September 19, 12
  135. 1.8.7-p358 :001 > class ExceptionOne < Exception; end => nil

    1.8.7-p358 :002 > ExceptionTwo = Class.new(Exception) => ExceptionTwo 1.8.7-p358 :003 > raise ExceptionOne ExceptionOne: ExceptionOne ! from (irb):3 1.8.7-p358 :004 > raise ExceptionTwo ExceptionTwo: ExceptionTwo ! from (irb):4 Wednesday, September 19, 12
  136. Loading development environment (Rails 2.3.10) >> a = [1,2,3,4,5] =>

    [1,2,3,4,5] >> a.sample Wednesday, September 19, 12
  137. Loading development environment (Rails 2.3.10) >> a = [1,2,3,4,5] =>

    [1,2,3,4,5] >> a.sample => 3 Wednesday, September 19, 12
  138. Loading development environment (Rails 2.3.10) >> a = [1,2,3,4,5] =>

    [1,2,3,4,5] >> a.sample => 3 >> a.sample => 4 Wednesday, September 19, 12
  139. Loading development environment (Rails 2.3.10) >> a = [1,2,3,4,5] =>

    [1,2,3,4,5] >> a.sample => 3 >> a.sample => 4 >> a.sample(2) Wednesday, September 19, 12
  140. Loading development environment (Rails 2.3.10) >> a = [1,2,3,4,5] =>

    [1,2,3,4,5] >> a.sample => 3 >> a.sample => 4 >> a.sample(2) => [4, 2] Wednesday, September 19, 12
  141. Loading development environment (Rails 2.3.10) >> helper.number_to_currency(100) => "$100.00" >>

    helper.number_to_human_size(1000000000000000) Wednesday, September 19, 12
  142. Loading development environment (Rails 2.3.10) >> helper.number_to_currency(100) => "$100.00" >>

    helper.number_to_human_size(1000000000000000) => “909.5 TB” Wednesday, September 19, 12
  143. Loading development environment (Rails 3.0.7) jruby-1.5.6 :001 > DataSource.group(:beta).count =>

    #<OrderedHash {false=>4951, true=>233}> Wednesday, September 19, 12
  144. $ script/console --sandbox Loading development environment in sandbox (Rails 2.3.10)

    Any modifications you make will be rolled back on exit NOTE: Gem.source_index is deprecated, use Specification... Wednesday, September 19, 12
  145. $ rake stats +----------------------+-------+-------+---------+---------+-----+-------+ | Name | Lines | LOC

    | Classes | Methods | M/C | LOC/M | +----------------------+-------+-------+---------+---------+-----+-------+ | Controllers | 4941 | 4077 | 67 | 399 | 5 | 8 | | Helpers | 970 | 787 | 0 | 103 | 0 | 5 | | Models | 9779 | 7878 | 163 | 989 | 6 | 5 | | Libraries | 5932 | 4496 | 108 | 447 | 4 | 8 | | Integration tests | 1731 | 552 | 18 | 8 | 0 | 67 | | Functional tests | 9767 | 8248 | 52 | 90 | 1 | 89 | | Unit tests | 16470 | 13678 | 181 | 97 | 0 | 139 | +----------------------+-------+-------+---------+---------+-----+-------+ | Total | 49590 | 39716 | 589 | 2133 | 3 | 16 | +----------------------+-------+-------+---------+---------+-----+-------+ Code LOC: 17238 Test LOC: 22478 Code to Test Ratio: 1:1.3 Wednesday, September 19, 12
  146. $ rake notes app/controllers/company_importers_controller.rb: * [241] [TODO] refactor to raise

    ActiveRecord::RecordNotFound instead of nil. app/controllers/open_id_controller.rb: * [ 44] [TODO] refactor the below. app/controllers/open_social_controller.rb: * [134] [TODO] Remove this code after enough data was collected (trobrock) app/helpers/reports_helper.rb: * [ 12] [TODO] The core utility of these helpers could be moved into the BaseReport class and then we can get the date ranges from the controller. app/models/axe/outright/aggregation/aggregation_director.rb: * [ 33] [TODO] remove this if possible - it's here b/c you get the 'expected * [131] [TODO] why is reload necessary? ... Wednesday, September 19, 12
  147. $ rake routes => QueryTrace disabled; CTRL-\ to toggle **

    Erubis 2.6.6 emails GET /emails(.:format) {:controller=>"emails", :action=>"index"} POST /emails(.:format) {:controller=>"emails", :action=>"create"} new_email GET /emails/new(.:format) {:controller=>"emails", :action=>"new"} edit_email GET /emails/:id/edit(.:format) {:controller=>"emails", :action=>"edit"} email GET /emails/:id(.:format) {:controller=>"emails", :action=>"show"} PUT /emails/:id(.:format) {:controller=>"emails", :action=>"update"} DELETE /emails/:id(.:format) {:controller=>"emails", :action=>"destroy"} root / {:controller=>"dashboard", :action=>"root"} dashboard /dashboard {:controller=>"dashboard", :action=>"index"} self_identify /self_identify {:controller=>"dashboard", :action=>"self_identify"} undo_self_identify /undo_self_identify {:controller=>"dashboard", :action=>"undo_self_identify"} request_merchant /request_merchant {:controller=>"dashboard", :action=>"request_merchant"} welcome /welcome {:controller=>"dashboard", :action=>"welcome"} /companies/profit {:controller=>"companies", :action=>"profit"} address_import_invitations GET /invitations/address_import(.:format) {:controller=>"invitations", :action=>"address_import"} bookkeeper_client_invitations GET /invitations/bookkeeper_client(.:format) {:controller=>"invitations", :action=>"bookkeeper_client"} invitations GET /invitations(.:format) {:controller=>"invitations", :action=>"index"} POST /invitations(.:format) {:controller=>"invitations", :action=>"create"} new_invitation GET /invitations/new(.:format) {:controller=>"invitations", :action=>"new"} edit_invitation GET /invitations/:id/edit(.:format) {:controller=>"invitations", :action=>"edit"} invitation GET /invitations/:id(.:format) {:controller=>"invitations", :action=>"show"} PUT /invitations/:id(.:format) {:controller=>"invitations", :action=>"update"} DELETE /invitations/:id(.:format) {:controller=>"invitations", :action=>"destroy"} new_feedback GET /feedback/new(.:format) {:controller=>"feedback", :action=>"new"} edit_feedback GET /feedback/edit(.:format) {:controller=>"feedback", :action=>"edit"} feedback GET /feedback(.:format) {:controller=>"feedback", :action=>"show"} PUT /feedback(.:format) {:controller=>"feedback", :action=>"update"} DELETE /feedback(.:format) {:controller=>"feedback", :action=>"destroy"} POST /feedback(.:format) {:controller=>"feedback", :action=>"create"} visible_accounts GET /visible_accounts(.:format) {:controller=>"visible_accounts", :action=>"index"} /tax_calendar {:controller=>"redirect", :action=>"to_outright"} ... Wednesday, September 19, 12
  148. $ ruby -c app/models/email_campaign.rb Syntax OK $ ruby -c app/models/axe/outright/filter_chain.rb

    app/models/axe/outright/filter_chain.rb:36: syntax error, unexpected $end, expecting kEND Wednesday, September 19, 12
  149. $ vim ~/.irbrc require "rubygems" require "wirble" Wirble.init Wirble.colorize colors

    = Wirble::Colorize.colors.merge({ :comma => :green, :refers => :green, }) Wirble::Colorize.colors = colors class Object def own_methods (self.methods - Object.new.methods).sort end end Wednesday, September 19, 12
  150. $ gem server $ Server started at http://0.0.0.0:8808 Server started

    at http://[::]:8808 Wednesday, September 19, 12
  151. $ gem server $ Server started at http://0.0.0.0:8808 Server started

    at http://[::]:8808 Wednesday, September 19, 12