Skip to content

Commit

Permalink
rename Letters::CoreExt#p to #o
Browse files Browse the repository at this point in the history
  • Loading branch information
davejacobs committed Oct 2, 2012
1 parent 34bb489 commit f9e0696
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ With Letters installed, you have a suite of methods available wherever you want


There are almost 20 Letters methods so far. You can find them [in the documentation](http://lettersrb.com/api). There are almost 20 Letters methods so far. You can find them [in the documentation](http://lettersrb.com/api).


Let's use with the `p` method as an example. It is one of the most familiar methods. Calling it prints the receiver to STDOUT and returns the receiver: Let's use with the `o` method as an example. It is one of the most familiar methods. Calling it prints the receiver to STDOUT and returns the receiver:


```ruby ```ruby
{ foo: "bar" }.p { foo: "bar" }.o
# => { foo: "bar" } # => { foo: "bar" }
# prints { foo: "bar" } # prints { foo: "bar" }
``` ```
Expand All @@ -37,20 +37,20 @@ words.grep(/interesting/).
join(", ") join(", ")
``` ```


If I want to know the state of your code after lines 3 and 5, all I have to do is add `.p` to each one: If I want to know the state of your code after lines 3 and 5, all I have to do is add `.o` to each one:


```ruby ```ruby
words.grep(/interesting/). words.grep(/interesting/).
map(&:downcase). map(&:downcase).
group_by(&:length).p. group_by(&:length).o.
values_at(5, 10). values_at(5, 10).
slice(0..2).p. slice(0..2).o.
join(", ") join(", ")
``` ```


Because the `p` method (and nearly every Letters method) returns the original object, introducing it is only ever for side effects -- it won't change the output of your code. Because the `o` method (and nearly every Letters method) returns the original object, introducing it is only ever for side effects -- it won't change the output of your code.


This is significantly easier than breaking apart the pipeline using variable assignment or a hefty `tap` block. This is significantly easier than breaking apart the pipeline using variable assignment or a hefty `tap` block.


The `p` method takes options, too, so you can add a prefix message to the output or choose another output format -- like [YAML]() or [pretty print](). The `o` method takes options, too, so you can add a prefix message to the output or choose another output format -- like [YAML]() or [pretty print]().


2 changes: 1 addition & 1 deletion lib/letters/core_ext.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def n(opts={})
end end


# Print to STDOUT # Print to STDOUT
def p(opts={}, &block) def o(opts={}, &block)
opts = { format: "ap", stream: $stdout }.merge opts opts = { format: "ap", stream: $stdout }.merge opts
tap do |o| tap do |o|
Helpers.message opts Helpers.message opts
Expand Down
10 changes: 5 additions & 5 deletions spec/letters/core_ext_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Letters


# Methods that can take a block # Methods that can take a block
hash.j { nil }.should == hash hash.j { nil }.should == hash
hash.p { nil }.should == hash hash.o { nil }.should == hash
end end


describe "#a (assert)" do describe "#a (assert)" do
Expand Down Expand Up @@ -182,25 +182,25 @@ module Letters
end end
end end


describe "#p (print)" do describe "#o (print)" do
describe "when no format is given" do describe "when no format is given" do
it "writes the object as awesome_print to STDOUT" do it "writes the object as awesome_print to STDOUT" do
$stdout.should_receive(:puts).with(hash.awesome_inspect) $stdout.should_receive(:puts).with(hash.awesome_inspect)
hash.p hash.o
end end
end end


describe "when a format is given" do describe "when a format is given" do
it "writes the object as that format to STDOUT" do it "writes the object as that format to STDOUT" do
$stdout.should_receive(:puts).with(hash.to_yaml) $stdout.should_receive(:puts).with(hash.to_yaml)
hash.p(:format => :yaml) hash.o(:format => :yaml)
end end
end end


describe "when a block is given" do describe "when a block is given" do
it "write the result of the block, executed in the object's context" do it "write the result of the block, executed in the object's context" do
$stdout.should_receive(:puts).with(hash.length.awesome_inspect) $stdout.should_receive(:puts).with(hash.length.awesome_inspect)
hash.p { length }.should == hash hash.o { length }.should == hash
end end
end end
end end
Expand Down
4 changes: 2 additions & 2 deletions spec/letters/patch_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ module Letters
Klass = Class.new Klass = Class.new
Letters.patch! Klass Letters.patch! Klass
k = Klass.new k = Klass.new
k.p.should == k k.o.should == k
end end


it "adds Letters::CoreExt to objects" do it "adds Letters::CoreExt to objects" do
obj = Object.new obj = Object.new
Letters.patch! obj Letters.patch! obj
obj.p.should == obj obj.o.should == obj
end end
end end
end end

0 comments on commit f9e0696

Please sign in to comment.