Skip to content

Commit

Permalink
Pretty up #to_s for DEFN'd procs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlen Christian Mart Cuss committed Oct 26, 2012
1 parent a6509f3 commit a8756da
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
3 changes: 2 additions & 1 deletion lib/boot.rg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
elements))

(defmacro defn [name args & body]
`(def ~name (fn ~args ~@body)))
(let [fn-name (.intern (.join [(.name (.ns (context))) (.name name)] "/"))]
`(def ~name (fn ~(ruby/Rouge.Symbol. fn-name) ~args ~@body))))

(defmacro when [cond & body]
`(if ~cond
Expand Down
4 changes: 2 additions & 2 deletions lib/rouge/builtins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _compile_quote(ns, lexicals, form)

def fn(context, *args)
if args[0].is_a? Rouge::Symbol
name = args.shift.name
name = args.shift.nice_name
end

argv, *body = args
Expand Down Expand Up @@ -102,7 +102,7 @@ def fn(context, *args)
}

if name
fn.define_singleton_method(:name) { name }
fn.define_singleton_method(:to_s) { name }
end

fn
Expand Down
6 changes: 5 additions & 1 deletion lib/rouge/symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ def self.[](inner)
c
end

def nice_name
:"#{@ns ? "#@ns/" : ""}#@name"
end

def inspect
"Rouge::Symbol[#{:"#{@ns ? "#@ns/" : ""}#@name".inspect}]"
"Rouge::Symbol[#{nice_name.inspect}]"
end

def to_s; inspect; end
Expand Down
2 changes: 1 addition & 1 deletion spec/builtins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
describe "storing its own name" do
let(:fn) { context.readeval('(fn lmnop [])') }

it { fn.name.should eq :lmnop }
it { fn.to_s.should eq :lmnop }
end

it "should compile with names bound" do
Expand Down
45 changes: 22 additions & 23 deletions spec/symbol_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,32 @@
require 'rouge'

describe Rouge::Symbol do
describe "the lookup" do
it "should return true, false and nil" do
Rouge::Symbol[:true].should be true
Rouge::Symbol[:false].should be false
Rouge::Symbol[:nil].should be nil
end
describe "lookup" do
it { Rouge::Symbol[:true].should be true }
it { Rouge::Symbol[:false].should be false }
it { Rouge::Symbol[:nil].should be nil }
end

describe "the constructor" do
it "should return new objects every time" do
Rouge::Symbol[:a].should_not be Rouge::Symbol[:a]
# but:
Rouge::Symbol[:a].should eq Rouge::Symbol[:a]
end
describe ".[]" do
it { Rouge::Symbol[:a].should_not be Rouge::Symbol[:a] }
# but:
it { Rouge::Symbol[:a].should eq Rouge::Symbol[:a] }
end

describe "the name and ns methods" do
it "should return the parts of the symbol" do
Rouge::Symbol[:abc].ns.should be_nil
Rouge::Symbol[:abc].name.should eq :abc
Rouge::Symbol[:"abc/def"].ns.should eq :abc
Rouge::Symbol[:"abc/def"].name.should eq :def
Rouge::Symbol[:/].ns.should be_nil
Rouge::Symbol[:/].name.should eq :/
Rouge::Symbol[:"rouge.core//"].ns.should eq :"rouge.core"
Rouge::Symbol[:"rouge.core//"].name.should eq :/
end
describe "#ns, #name" do
it { Rouge::Symbol[:abc].ns.should be_nil }
it { Rouge::Symbol[:abc].name.should eq :abc }
it { Rouge::Symbol[:"abc/def"].ns.should eq :abc }
it { Rouge::Symbol[:"abc/def"].name.should eq :def }
it { Rouge::Symbol[:/].ns.should be_nil }
it { Rouge::Symbol[:/].name.should eq :/ }
it { Rouge::Symbol[:"rouge.core//"].ns.should eq :"rouge.core" }
it { Rouge::Symbol[:"rouge.core//"].name.should eq :/ }
end

describe "#nice_name" do
it { Rouge::Symbol[:boo].nice_name.should eq :boo }
it { Rouge::Symbol[:"what/nice"].nice_name.should eq :"what/nice" }
end
end

Expand Down

0 comments on commit a8756da

Please sign in to comment.