Ruby things

190 views
144 views

Published on

Collection of ruby (and ruby related) tricks

Published in: Technology, Sports
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
190
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Ruby things

    1. 1. rubyoff the beaten path github.com/julio
    2. 2. ruby things
    3. 3. 01string concatenation
    4. 4. 1.8.7-p358 :001 > s = “this” “is” “a” “thing”
    5. 5. 1.8.7-p358 :001 > s = “this” “is” “a” “thing” => “thisisathing”1.8.7-p358 :002 >
    6. 6. 02print json
    7. 7. 1.8.7-p358 :001 > require “rubygems” => true1.8.7-p358 :002 > require “json” => true1.8.7-p358 :003 > h = {:one => 1, :two => 2} => {:one=>1, :two=>2}1.8.7-p358 :004 > j h
    8. 8. 1.8.7-p358 :001 > require “rubygems” => true1.8.7-p358 :002 > require “json” => true1.8.7-p358 :003 > h = {:one => 1, :two => 2} => {:one=>1, :two=>2}1.8.7-p358 :004 > j h{"one":1,"two":2}
    9. 9. 1.8.7-p358 :001 > require “rubygems” => true1.8.7-p358 :002 > require “json” => true1.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
    10. 10. 1.8.7-p358 :001 > require “rubygems” => true1.8.7-p358 :002 > require “json” => true1.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 >
    11. 11. 03interpolation
    12. 12. 1.8.7-p358 :001 > @name = "bob" => “bob”1.8.7-p358 :002 > puts "hello #@name"
    13. 13. 1.8.7-p358 :001 > @name = "bob" => “bob”1.8.7-p358 :002 > puts "hello #@name" “hello bob” => nil
    14. 14. 04.. and ...
    15. 15. 1.8.7-p358 :001 > a = (1..2)=> 1..2
    16. 16. 1.8.7-p358 :001 > a = (1..2) => 1..21.8.7-p358 :002 > a.to_a
    17. 17. 1.8.7-p358 :001 > a = (1..2) => 1..21.8.7-p358 :002 > a.to_a => [1, 2]
    18. 18. 1.8.7-p358 :001 > a = (1..2) => 1..21.8.7-p358 :002 > a.to_a => [1, 2]1.8.7-p358 :003 > b = (1...2)
    19. 19. 1.8.7-p358 :001 > a = (1..2) => 1..21.8.7-p358 :002 > a.to_a => [1, 2]1.8.7-p358 :003 > b = (1...2) => 1...2
    20. 20. 1.8.7-p358 :001 > a = (1..2) => 1..21.8.7-p358 :002 > a.to_a => [1, 2]1.8.7-p358 :003 > b = (1...2) => 1...21.8.7-p358 :004 > b.to_a
    21. 21. 1.8.7-p358 :001 > a = (1..2) => 1..21.8.7-p358 :002 > a.to_a => [1, 2]1.8.7-p358 :003 > b = (1...2) => 1...21.8.7-p358 :004 > b.to_a => [1]
    22. 22. 05string contains?
    23. 23. 1.8.7-p358 :001 > s = "this is a string" => “this is a string”1.8.7-p358 :002 > s[“is a”]
    24. 24. 1.8.7-p358 :001 > s = "this is a string" => “this is a string”1.8.7-p358 :002 > s[“is a”] => “is a”
    25. 25. 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"]
    26. 26. 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
    27. 27. 06expand array
    28. 28. 1.8.7-p358 :001 > [*10..20]
    29. 29. 1.8.7-p358 :001 > [*10..20]=> [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
    30. 30. 07hash from array
    31. 31. 1.8.7-p358 :001 > Hash[1,2,3,4]
    32. 32. 1.8.7-p358 :001 > Hash[1,2,3,4]=> {1=>2, 3=>4}
    33. 33. 1.8.7-p358 :001 > Hash[1,2,3,4] => {1=>2, 3=>4}1.8.7-p358 :002 > Hash[["one", 1], ["two", 2]]
    34. 34. 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]}
    35. 35. 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]
    36. 36. 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]]
    37. 37. 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]
    38. 38. 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]
    39. 39. 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]
    40. 40. 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]
    41. 41. 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}
    42. 42. 08zip
    43. 43. 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)
    44. 44. 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]]
    45. 45. 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]
    46. 46. 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}
    47. 47. 09shuffle
    48. 48. 1.8.7-p358 :001 > a = [1,2,3] => [1, 2, 3]1.8.7-p358 :002 > a.shuffle
    49. 49. 1.8.7-p358 :001 > a = [1,2,3] => [1, 2, 3]1.8.7-p358 :002 > a.shuffle => [1, 3, 2]
    50. 50. 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
    51. 51. 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]
    52. 52. 10cons
    53. 53. 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}
    54. 54. 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
    55. 55. 11set
    56. 56. 1.8.7-p358 :001 > require set => true1.8.7-p358 :002 > s = Set.new => #<Set: {}>1.8.7-p358 :003 > s << 1
    57. 57. 1.8.7-p358 :001 > require set => true1.8.7-p358 :002 > s = Set.new => #<Set: {}>1.8.7-p358 :003 > s << 1 => #<Set: {1}>
    58. 58. 1.8.7-p358 :001 > require set => true1.8.7-p358 :002 > s = Set.new => #<Set: {}>1.8.7-p358 :003 > s << 1 => #<Set: {1}>1.8.7-p358 :004 > s << 1
    59. 59. 1.8.7-p358 :001 > require set => true1.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}>
    60. 60. 1.8.7-p358 :001 > require set => true1.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
    61. 61. 1.8.7-p358 :001 > require set => true1.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}>
    62. 62. 12include A, B, C
    63. 63. 1.8.7-p358 :001 > module A; def foo; "A"; end; end=> nil
    64. 64. 1.8.7-p358 :001 > module A; def foo; "A"; end; end => nil1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil
    65. 65. 1.8.7-p358 :001 > module A; def foo; "A"; end; end => nil1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil1.8.7-p358 :003 > class C; include A; include B; end => C
    66. 66. 1.8.7-p358 :001 > module A; def foo; "A"; end; end => nil1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil1.8.7-p358 :003 > class C; include A; include B; end => C1.8.7-p358 :004 > C.new.foo
    67. 67. 1.8.7-p358 :001 > module A; def foo; "A"; end; end => nil1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil1.8.7-p358 :003 > class C; include A; include B; end => C1.8.7-p358 :004 > C.new.foo => “B”
    68. 68. 1.8.7-p358 :001 > module A; def foo; "A"; end; end => nil1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil1.8.7-p358 :003 > class C; include A; include B; end => C1.8.7-p358 :004 > C.new.foo => “B”1.8.7-p358 :005 > class D; include A, B; end
    69. 69. 1.8.7-p358 :001 > module A; def foo; "A"; end; end => nil1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil1.8.7-p358 :003 > class C; include A; include B; end => C1.8.7-p358 :004 > C.new.foo => “B”1.8.7-p358 :005 > class D; include A, B; end => D
    70. 70. 1.8.7-p358 :001 > module A; def foo; "A"; end; end => nil1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil1.8.7-p358 :003 > class C; include A; include B; end => C1.8.7-p358 :004 > C.new.foo => “B”1.8.7-p358 :005 > class D; include A, B; end => D1.8.7-p358 :006 > D.new.foo
    71. 71. 1.8.7-p358 :001 > module A; def foo; "A"; end; end => nil1.8.7-p358 :002 > module B; def foo; "B"; end; end => nil1.8.7-p358 :003 > class C; include A; include B; end => C1.8.7-p358 :004 > C.new.foo => “B”1.8.7-p358 :005 > class D; include A, B; end => D1.8.7-p358 :006 > D.new.foo => “A”
    72. 72. 13default args
    73. 73. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y; end => nil1.8.7-p358 :002 > foo 1
    74. 74. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y; end => nil1.8.7-p358 :002 > foo 1 12 => nil
    75. 75. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y; end => nil1.8.7-p358 :002 > foo 1 12 => nil1.8.7-p358 :003 > def bar(x, y=x); print x; print y; end => nil
    76. 76. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y; end => nil1.8.7-p358 :002 > foo 1 12 => nil1.8.7-p358 :003 > def bar(x, y=x); print x; print y; end => nil1.8.7-p358 :004 > bar 1
    77. 77. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y; end => nil1.8.7-p358 :002 > foo 1 12 => nil1.8.7-p358 :003 > def bar(x, y=x); print x; print y; end => nil1.8.7-p358 :004 > bar 1 11 => nil
    78. 78. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y; end => nil1.8.7-p358 :002 > foo 1 12 => nil1.8.7-p358 :003 > def bar(x, y=x); print x; print y; end => nil1.8.7-p358 :004 > bar 1 11 => nil1.8.7-p358 :005 > bar 1,2
    79. 79. 1.8.7-p358 :001 > def foo(x, y=2); print x; print y; end => nil1.8.7-p358 :002 > foo 1 12 => nil1.8.7-p358 :003 > def bar(x, y=x); print x; print y; end => nil1.8.7-p358 :004 > bar 1 11 => nil1.8.7-p358 :005 > bar 1,2 12 => nil
    80. 80. 14caller
    81. 81. 1.8.7-p358 :001 > def foo; puts caller; "foo"; end=> nil
    82. 82. 1.8.7-p358 :001 > def foo; puts caller; "foo"; end => nil1.8.7-p358 :002 > def bar; foo; end => nil
    83. 83. 1.8.7-p358 :001 > def foo; puts caller; "foo"; end => nil1.8.7-p358 :002 > def bar; foo; end => nil1.8.7-p358 :003 > bar
    84. 84. 1.8.7-p358 :001 > def foo; puts caller; "foo"; end => nil1.8.7-p358 :002 > def bar; foo; end => nil1.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"
    85. 85. 15car and cdr
    86. 86. 1.8.7-p358 :001 > a = [1,2,3,4,5]=> [1, 2, 3, 4, 5]
    87. 87. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5]1.8.7-p358 :002 > car, *cdr = a
    88. 88. 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]
    89. 89. 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
    90. 90. 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
    91. 91. 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 => 11.8.7-p358 :004 > cdr
    92. 92. 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 => 11.8.7-p358 :004 > cdr => [2, 3, 4, 5]
    93. 93. 16group_by
    94. 94. 1.8.7-p358 :001 > words = %w{ foo bar yo stuff } => ["foo", "bar", "yo", "stuff"]
    95. 95. 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}
    96. 96. 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"]}
    97. 97. 17inject and reduce
    98. 98. 1.8.7-p358 :001 > a = [*1..100] # or (1..100).to_a=> [1, 2, 3, 4, ..., 99, 100]
    99. 99. 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}
    100. 100. 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
    101. 101. 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} => 50501.8.7-p358 :003 > t = [*1..100].inject {|a,e| a += e} => 5050
    102. 102. 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} => 50501.8.7-p358 :003 > t = [*1..100].inject {|a,e| a += e} => 50501.8.7-p358 :004 > t = [*1..100].inject(&:+) => 5050
    103. 103. 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} => 50501.8.7-p358 :003 > t = [*1..100].inject {|a,e| a += e} => 50501.8.7-p358 :004 > t = [*1..100].inject(&:+) => 50501.8.7-p358 :005 > t = [*1..100].inject(:+) => 5050
    104. 104. 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} => 50501.8.7-p358 :003 > t = [*1..100].inject {|a,e| a += e} => 50501.8.7-p358 :004 > t = [*1..100].inject(&:+) => 50501.8.7-p358 :005 > t = [*1..100].inject(:+) => 50501.8.7-p358 :006 > t = [*1..100].reduce(:+) => 5050
    105. 105. 18the other join
    106. 106. 1.8.7-p358 :001 > a = [1,2,3,4]=> [1, 2, 3, 4]
    107. 107. 1.8.7-p358 :001 > a = [1,2,3,4] => [1, 2, 3, 4]1.8.7-p358 :002 > a *= ","
    108. 108. 1.8.7-p358 :001 > a = [1,2,3,4] => [1, 2, 3, 4]1.8.7-p358 :002 > a *= "," => "1,2,3,4"
    109. 109. 19add method to instance
    110. 110. 1.8.7-p358 :001 > a = [1,2,3,4,5]=> [1, 2, 3, 4, 5]
    111. 111. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5]1.8.7-p358 :002 > module Summable; def mul; self.reduce(:*); end;end => nil
    112. 112. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5]1.8.7-p358 :002 > module Summable; def mul; self.reduce(:*); end;end => nil1.8.7-p358 :003 > a.extend Summable => [1, 2, 3, 4, 5]
    113. 113. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5]1.8.7-p358 :002 > module Summable; def mul; self.reduce(:*); end;end => nil1.8.7-p358 :003 > a.extend Summable => [1, 2, 3, 4, 5]1.8.7-p358 :004 > a.mul
    114. 114. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5]1.8.7-p358 :002 > module Summable; def mul; self.reduce(:*); end;end => nil1.8.7-p358 :003 > a.extend Summable => [1, 2, 3, 4, 5]1.8.7-p358 :004 > a.mul => 120
    115. 115. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5]1.8.7-p358 :002 > module Summable; def mul; self.reduce(:*); end;end => nil1.8.7-p358 :003 > a.extend Summable => [1, 2, 3, 4, 5]1.8.7-p358 :004 > a.mul => 1201.8.7-p358 :005 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5]
    116. 116. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5]1.8.7-p358 :002 > module Summable; def mul; self.reduce(:*); end;end => nil1.8.7-p358 :003 > a.extend Summable => [1, 2, 3, 4, 5]1.8.7-p358 :004 > a.mul => 1201.8.7-p358 :005 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5]1.8.7-p358 :006 > a.mul
    117. 117. 1.8.7-p358 :001 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5]1.8.7-p358 :002 > module Summable; def mul; self.reduce(:*); end;end => nil1.8.7-p358 :003 > a.extend Summable => [1, 2, 3, 4, 5]1.8.7-p358 :004 > a.mul => 1201.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
    118. 118. 20methods.grep
    119. 119. 1.8.7-p358 :001 > class C; def foo; "foo"; end; end => nil1.8.7-p358 :002 > c = C.new => #<C:0x10b3e0b28>
    120. 120. 1.8.7-p358 :001 > class C; def foo; "foo"; end; end => nil1.8.7-p358 :002 > c = C.new => #<C:0x10b3e0b28>1.8.7-p358 :003 > c.methods.grep /foo/
    121. 121. 1.8.7-p358 :001 > class C; def foo; "foo"; end; end => nil1.8.7-p358 :002 > c = C.new => #<C:0x10b3e0b28>1.8.7-p358 :003 > c.methods.grep /foo/ => ["foo"]
    122. 122. 21class variables
    123. 123. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end=> nil
    124. 124. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil
    125. 125. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil1.8.7-p358 :003 > A.foo
    126. 126. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil1.8.7-p358 :003 > A.foo => “A”
    127. 127. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil1.8.7-p358 :003 > A.foo => “A”1.8.7-p358 :003 > C.foo
    128. 128. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil1.8.7-p358 :003 > A.foo => “A”1.8.7-p358 :003 > C.foo => “A”
    129. 129. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil1.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
    130. 130. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil1.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
    131. 131. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil1.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 => nil1.8.7-p358 :003 > B.foo
    132. 132. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil1.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 => nil1.8.7-p358 :003 > B.foo => “B”
    133. 133. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil1.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 => nil1.8.7-p358 :003 > B.foo => “B”1.8.7-p358 :003 > A.foo
    134. 134. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil1.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 => nil1.8.7-p358 :003 > B.foo => “B”1.8.7-p358 :003 > A.foo => “B”
    135. 135. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil1.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 => nil1.8.7-p358 :003 > B.foo => “B”1.8.7-p358 :003 > A.foo => “B”1.8.7-p358 :003 > C.foo
    136. 136. 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end => nil1.8.7-p358 :002 > class C < A; nil; end => nil1.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 => nil1.8.7-p358 :003 > B.foo => “B”1.8.7-p358 :003 > A.foo => “B”1.8.7-p358 :003 > C.foo => “B”
    137. 137. 22int to hex to int
    138. 138. 1.8.7-p358 :001 > 3405691582.to_s(16)
    139. 139. 1.8.7-p358 :001 > 3405691582.to_s(16)=> “cafebabe”
    140. 140. 1.8.7-p358 :001 > 3405691582.to_s(16) => “cafebabe”1.8.7-p358 :002 > "cafebabe".to_i(16)
    141. 141. 1.8.7-p358 :001 > 3405691582.to_s(16) => “cafebabe”1.8.7-p358 :002 > "cafebabe".to_i(16) => 3405691582
    142. 142. 23flip-flop
    143. 143. 1.8.7-p358 :001 > a = [*1..100]=> [1, 2, 3, 4, 5, 6, .., 99, 100]
    144. 144. 1.8.7-p358 :001 > a = [*1..100] => [1, 2, 3, 4, 5, 6, .., 99, 100]1.8.7-p358 :002 > a.each_with_index {|x,i| p x if (i==20..i==34)}
    145. 145. 1.8.7-p358 :001 > a = [*1..100] => [1, 2, 3, 4, 5, 6, .., 99, 100]1.8.7-p358 :002 > a.each_with_index {|x,i| p x if (i==20..i==34)}2122...3435=> [1, 2, 3, 4, 5, 6, .., 99, 100]
    146. 146. 240b
    147. 147. 1.8.7-p358 :001 > val = 0b11100100
    148. 148. 1.8.7-p358 :001 > val = 0b11100100=> 228
    149. 149. 1.8.7-p358 :001 > val = 0b11100100 => 2281.8.7-p358 :002 > val.class
    150. 150. 1.8.7-p358 :001 > val = 0b11100100 => 2281.8.7-p358 :002 > val.class => Fixnum
    151. 151. 1.8.7-p358 :001 > val = 0b11100100 => 2281.8.7-p358 :002 > val.class => Fixnum1.8.7-p358 :003 > val[4]
    152. 152. 1.8.7-p358 :001 > val = 0b11100100 => 2281.8.7-p358 :002 > val.class => Fixnum1.8.7-p358 :003 > val[4] => 0
    153. 153. 1.8.7-p358 :001 > val = 0b11100100 => 2281.8.7-p358 :002 > val.class => Fixnum1.8.7-p358 :003 > val[4] => 01.8.7-p358 :004 > val[5]
    154. 154. 1.8.7-p358 :001 > val = 0b11100100 => 2281.8.7-p358 :002 > val.class => Fixnum1.8.7-p358 :003 > val[4] => 01.8.7-p358 :004 > val[5] => 1
    155. 155. 25ensure and return
    156. 156. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end1.8.7-p358 :002 > foo
    157. 157. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end1.8.7-p358 :002 > foo => 0
    158. 158. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end1.8.7-p358 :002 > foo => 01.8.7-p358 :003 > def foo; 0; ensure; return 1; end1.8.7-p358 :004 > foo
    159. 159. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end1.8.7-p358 :002 > foo => 01.8.7-p358 :003 > def foo; 0; ensure; return 1; end1.8.7-p358 :004 > foo => 1
    160. 160. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end1.8.7-p358 :002 > foo => 01.8.7-p358 :003 > def foo; 0; ensure; return 1; end1.8.7-p358 :004 > foo => 11.8.7-p358 :005 > def foo; return 0; ensure; 1; end1.8.7-p358 :006 > foo
    161. 161. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end1.8.7-p358 :002 > foo => 01.8.7-p358 :003 > def foo; 0; ensure; return 1; end1.8.7-p358 :004 > foo => 11.8.7-p358 :005 > def foo; return 0; ensure; 1; end1.8.7-p358 :006 > foo => 0
    162. 162. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end1.8.7-p358 :002 > foo => 01.8.7-p358 :003 > def foo; 0; ensure; return 1; end1.8.7-p358 :004 > foo => 11.8.7-p358 :005 > def foo; return 0; ensure; 1; end1.8.7-p358 :006 > foo => 01.8.7-p358 :007 > def foo; return 0; ensure; return 1; end1.8.7-p358 :006 > foo
    163. 163. 1.8.7-p358 :001 > def foo; 0; ensure; 1; end1.8.7-p358 :002 > foo => 01.8.7-p358 :003 > def foo; 0; ensure; return 1; end1.8.7-p358 :004 > foo => 11.8.7-p358 :005 > def foo; return 0; ensure; 1; end1.8.7-p358 :006 > foo => 01.8.7-p358 :007 > def foo; return 0; ensure; return 1; end1.8.7-p358 :006 > foo => 1
    164. 164. 26benchmark
    165. 165. 1.8.7-p358 :001 > require ‘benchmark’=> true
    166. 166. 1.8.7-p358 :001 > require ‘benchmark’ => true1.8.7-p358 :002 > n = 50000003 > 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
    167. 167. 1.8.7-p358 :001 > require ‘benchmark’ => true1.8.7-p358 :002 > n = 50000003 > 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 realfor: 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
    168. 168. 27classes are objects
    169. 169. 1.8.7-p358 :001 > class ExceptionOne < Exception; end=> nil
    170. 170. 1.8.7-p358 :001 > class ExceptionOne < Exception; end => nil1.8.7-p358 :002 > ExceptionTwo = Class.new(Exception) => ExceptionTwo
    171. 171. 1.8.7-p358 :001 > class ExceptionOne < Exception; end => nil1.8.7-p358 :002 > ExceptionTwo = Class.new(Exception) => ExceptionTwo1.8.7-p358 :003 > raise ExceptionOne
    172. 172. 1.8.7-p358 :001 > class ExceptionOne < Exception; end => nil1.8.7-p358 :002 > ExceptionTwo = Class.new(Exception) => ExceptionTwo1.8.7-p358 :003 > raise ExceptionOneExceptionOne: ExceptionOne! from (irb):3
    173. 173. 1.8.7-p358 :001 > class ExceptionOne < Exception; end => nil1.8.7-p358 :002 > ExceptionTwo = Class.new(Exception) => ExceptionTwo1.8.7-p358 :003 > raise ExceptionOneExceptionOne: ExceptionOne! from (irb):31.8.7-p358 :004 > raise ExceptionTwo
    174. 174. 1.8.7-p358 :001 > class ExceptionOne < Exception; end => nil1.8.7-p358 :002 > ExceptionTwo = Class.new(Exception) => ExceptionTwo1.8.7-p358 :003 > raise ExceptionOneExceptionOne: ExceptionOne! from (irb):31.8.7-p358 :004 > raise ExceptionTwoExceptionTwo: ExceptionTwo! from (irb):4
    175. 175. 28[].sample
    176. 176. Loading development environment (Rails 2.3.10)>> a = [1,2,3,4,5]=> [1,2,3,4,5]>> a.sample
    177. 177. Loading development environment (Rails 2.3.10)>> a = [1,2,3,4,5]=> [1,2,3,4,5]>> a.sample=> 3
    178. 178. Loading development environment (Rails 2.3.10)>> a = [1,2,3,4,5]=> [1,2,3,4,5]>> a.sample=> 3>> a.sample=> 4
    179. 179. 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)
    180. 180. 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]
    181. 181. rails things
    182. 182. 29helper
    183. 183. Loading development environment (Rails 2.3.10)>> helper.number_to_currency(100)
    184. 184. Loading development environment (Rails 2.3.10)>> helper.number_to_currency(100)=> "$100.00"
    185. 185. Loading development environment (Rails 2.3.10)>> helper.number_to_currency(100)=> "$100.00">> helper.number_to_human_size(1000000000000000)
    186. 186. Loading development environment (Rails 2.3.10)>> helper.number_to_currency(100)=> "$100.00">> helper.number_to_human_size(1000000000000000)=> “909.5 TB”
    187. 187. 30group...count
    188. 188. Loading development environment (Rails 3.0.7)jruby-1.5.6 :001 > DataSource.group(:beta).count
    189. 189. Loading development environment (Rails 3.0.7)jruby-1.5.6 :001 > DataSource.group(:beta).count => #<OrderedHash {false=>4951, true=>233}>
    190. 190. command line
    191. 191. 31sandbox
    192. 192. $ script/console --sandbox
    193. 193. $ script/console --sandboxLoading development environment in sandbox (Rails 2.3.10)Any modifications you make will be rolled back on exitNOTE: Gem.source_index is deprecated, use Specification...
    194. 194. 32rake stats
    195. 195. $ rake stats
    196. 196. $ 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
    197. 197. 33rake notes
    198. 198. $ rake notes
    199. 199. $ rake notesapp/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 classand then we can get the date ranges from the controller.app/models/axe/outright/aggregation/aggregation_director.rb: * [ 33] [TODO] remove this if possible - its here b/c you get the expected * [131] [TODO] why is reload necessary?...
    200. 200. 34rake routes
    201. 201. $ rake routes
    202. 202. $ 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"}...
    203. 203. 35syntax check
    204. 204. $ ruby -c app/models/email_campaign.rb
    205. 205. $ ruby -c app/models/email_campaign.rbSyntax OK
    206. 206. $ ruby -c app/models/email_campaign.rbSyntax OK$ ruby -c app/models/axe/outright/filter_chain.rb
    207. 207. $ ruby -c app/models/email_campaign.rbSyntax OK$ ruby -c app/models/axe/outright/filter_chain.rbapp/models/axe/outright/filter_chain.rb:36: syntax error, unexpected$end, expecting kEND
    208. 208. 36.irbrc
    209. 209. $ vim ~/.irbrc
    210. 210. $ vim ~/.irbrcrequire "rubygems"require "wirble"Wirble.initWirble.colorizecolors = Wirble::Colorize.colors.merge({ :comma => :green, :refers => :green,})Wirble::Colorize.colors = colorsclass Object def own_methods (self.methods - Object.new.methods).sort endend

    ×