Ruby things
Upcoming SlideShare
Loading in...5
×
 

Ruby things

on

  • 146 views

Collection of ruby (and ruby related) tricks

Collection of ruby (and ruby related) tricks

Statistics

Views

Total Views
146
Views on SlideShare
144
Embed Views
2

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 2

http://www.hanrss.com 2

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \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 Ruby things Presentation Transcript

  • rubyoff the beaten path github.com/julio
  • ruby things
  • 01string concatenation
  • 1.8.7-p358 :001 > s = “this” “is” “a” “thing”
  • 1.8.7-p358 :001 > s = “this” “is” “a” “thing” => “thisisathing”1.8.7-p358 :002 >
  • 02print json
  • 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
  • 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 :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
  • 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 >
  • 03interpolation
  • 1.8.7-p358 :001 > @name = "bob" => “bob”1.8.7-p358 :002 > puts "hello #@name"
  • 1.8.7-p358 :001 > @name = "bob" => “bob”1.8.7-p358 :002 > puts "hello #@name" “hello bob” => nil
  • 04.. and ...
  • 1.8.7-p358 :001 > a = (1..2)=> 1..2
  • 1.8.7-p358 :001 > a = (1..2) => 1..21.8.7-p358 :002 > a.to_a
  • 1.8.7-p358 :001 > a = (1..2) => 1..21.8.7-p358 :002 > a.to_a => [1, 2]
  • 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.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
  • 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.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]
  • 05string contains?
  • 1.8.7-p358 :001 > s = "this is a string" => “this is a string”1.8.7-p358 :002 > s[“is a”]
  • 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 :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"]
  • 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
  • 06expand array
  • 1.8.7-p358 :001 > [*10..20]
  • 1.8.7-p358 :001 > [*10..20]=> [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
  • 07hash from array
  • 1.8.7-p358 :001 > Hash[1,2,3,4]
  • 1.8.7-p358 :001 > Hash[1,2,3,4]=> {1=>2, 3=>4}
  • 1.8.7-p358 :001 > Hash[1,2,3,4] => {1=>2, 3=>4}1.8.7-p358 :002 > Hash[["one", 1], ["two", 2]]
  • 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 :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]
  • 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 :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]
  • 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.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 :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.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}
  • 08zip
  • 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)
  • 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 :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]
  • 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}
  • 09shuffle
  • 1.8.7-p358 :001 > a = [1,2,3] => [1, 2, 3]1.8.7-p358 :002 > a.shuffle
  • 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 :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
  • 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]
  • 10cons
  • 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.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
  • 11set
  • 1.8.7-p358 :001 > require set => true1.8.7-p358 :002 > s = Set.new => #<Set: {}>1.8.7-p358 :003 > s << 1
  • 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 :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
  • 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 :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
  • 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}>
  • 12include A, B, C
  • 1.8.7-p358 :001 > module A; def foo; "A"; end; end=> nil
  • 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
  • 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
  • 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
  • 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 :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
  • 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
  • 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
  • 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”
  • 13default args
  • 1.8.7-p358 :001 > def foo(x, y=2); print x; print y; end => nil1.8.7-p358 :002 > foo 1
  • 1.8.7-p358 :001 > def foo(x, y=2); print x; print y; end => nil1.8.7-p358 :002 > foo 1 12 => nil
  • 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
  • 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
  • 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
  • 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
  • 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
  • 14caller
  • 1.8.7-p358 :001 > def foo; puts caller; "foo"; end=> nil
  • 1.8.7-p358 :001 > def foo; puts caller; "foo"; end => nil1.8.7-p358 :002 > def bar; foo; end => nil
  • 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
  • 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"
  • 15car and cdr
  • 1.8.7-p358 :001 > a = [1,2,3,4,5]=> [1, 2, 3, 4, 5]
  • 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.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 :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.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 :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
  • 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]
  • 16group_by
  • 1.8.7-p358 :001 > words = %w{ foo bar yo stuff } => ["foo", "bar", "yo", "stuff"]
  • 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}
  • 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"]}
  • 17inject and reduce
  • 1.8.7-p358 :001 > a = [*1..100] # or (1..100).to_a=> [1, 2, 3, 4, ..., 99, 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}
  • 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 :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
  • 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
  • 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
  • 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
  • 18the other join
  • 1.8.7-p358 :001 > a = [1,2,3,4]=> [1, 2, 3, 4]
  • 1.8.7-p358 :001 > a = [1,2,3,4] => [1, 2, 3, 4]1.8.7-p358 :002 > a *= ","
  • 1.8.7-p358 :001 > a = [1,2,3,4] => [1, 2, 3, 4]1.8.7-p358 :002 > a *= "," => "1,2,3,4"
  • 19add method to instance
  • 1.8.7-p358 :001 > a = [1,2,3,4,5]=> [1, 2, 3, 4, 5]
  • 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
  • 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 :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
  • 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
  • 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 :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
  • 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
  • 20methods.grep
  • 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 :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/
  • 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"]
  • 21class variables
  • 1.8.7-p358 :001 > class A; @@foo="A"; def self.foo; @@foo; end; end=> nil
  • 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
  • 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
  • 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 :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
  • 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 :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
  • 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
  • 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
  • 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 :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
  • 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 :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
  • 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”
  • 22int to hex to int
  • 1.8.7-p358 :001 > 3405691582.to_s(16)
  • 1.8.7-p358 :001 > 3405691582.to_s(16)=> “cafebabe”
  • 1.8.7-p358 :001 > 3405691582.to_s(16) => “cafebabe”1.8.7-p358 :002 > "cafebabe".to_i(16)
  • 1.8.7-p358 :001 > 3405691582.to_s(16) => “cafebabe”1.8.7-p358 :002 > "cafebabe".to_i(16) => 3405691582
  • 23flip-flop
  • 1.8.7-p358 :001 > a = [*1..100]=> [1, 2, 3, 4, 5, 6, .., 99, 100]
  • 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)}
  • 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]
  • 240b
  • 1.8.7-p358 :001 > val = 0b11100100
  • 1.8.7-p358 :001 > val = 0b11100100=> 228
  • 1.8.7-p358 :001 > val = 0b11100100 => 2281.8.7-p358 :002 > val.class
  • 1.8.7-p358 :001 > val = 0b11100100 => 2281.8.7-p358 :002 > val.class => Fixnum
  • 1.8.7-p358 :001 > val = 0b11100100 => 2281.8.7-p358 :002 > val.class => Fixnum1.8.7-p358 :003 > val[4]
  • 1.8.7-p358 :001 > val = 0b11100100 => 2281.8.7-p358 :002 > val.class => Fixnum1.8.7-p358 :003 > val[4] => 0
  • 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.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
  • 25ensure and return
  • 1.8.7-p358 :001 > def foo; 0; ensure; 1; end1.8.7-p358 :002 > foo
  • 1.8.7-p358 :001 > def foo; 0; ensure; 1; end1.8.7-p358 :002 > foo => 0
  • 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.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
  • 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
  • 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
  • 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.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
  • 26benchmark
  • 1.8.7-p358 :001 > require ‘benchmark’=> true
  • 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
  • 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
  • 27classes are objects
  • 1.8.7-p358 :001 > class ExceptionOne < Exception; end=> nil
  • 1.8.7-p358 :001 > class ExceptionOne < Exception; end => nil1.8.7-p358 :002 > ExceptionTwo = Class.new(Exception) => ExceptionTwo
  • 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
  • 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
  • 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
  • 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
  • 28[].sample
  • Loading development environment (Rails 2.3.10)>> a = [1,2,3,4,5]=> [1,2,3,4,5]>> a.sample
  • Loading development environment (Rails 2.3.10)>> a = [1,2,3,4,5]=> [1,2,3,4,5]>> a.sample=> 3
  • Loading development environment (Rails 2.3.10)>> a = [1,2,3,4,5]=> [1,2,3,4,5]>> a.sample=> 3>> a.sample=> 4
  • 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)
  • 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]
  • rails things
  • 29helper
  • Loading development environment (Rails 2.3.10)>> helper.number_to_currency(100)
  • Loading development environment (Rails 2.3.10)>> helper.number_to_currency(100)=> "$100.00"
  • Loading development environment (Rails 2.3.10)>> helper.number_to_currency(100)=> "$100.00">> helper.number_to_human_size(1000000000000000)
  • Loading development environment (Rails 2.3.10)>> helper.number_to_currency(100)=> "$100.00">> helper.number_to_human_size(1000000000000000)=> “909.5 TB”
  • 30group...count
  • Loading development environment (Rails 3.0.7)jruby-1.5.6 :001 > DataSource.group(:beta).count
  • Loading development environment (Rails 3.0.7)jruby-1.5.6 :001 > DataSource.group(:beta).count => #<OrderedHash {false=>4951, true=>233}>
  • command line
  • 31sandbox
  • $ script/console --sandbox
  • $ 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...
  • 32rake stats
  • $ rake stats
  • $ 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
  • 33rake notes
  • $ rake notes
  • $ 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?...
  • 34rake routes
  • $ rake routes
  • $ 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"}...
  • 35syntax check
  • $ ruby -c app/models/email_campaign.rb
  • $ ruby -c app/models/email_campaign.rbSyntax OK
  • $ ruby -c app/models/email_campaign.rbSyntax OK$ ruby -c app/models/axe/outright/filter_chain.rb
  • $ 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
  • 36.irbrc
  • $ vim ~/.irbrc
  • $ 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