Permalink
Browse files

reduce object creation

  • Loading branch information...
1 parent bb40322 commit a421f595d7c88ee052922de87aa0e7e5aede3787 @tenderlove committed Jan 16, 2013
Showing with 6 additions and 10 deletions.
  1. +6 −10 lib/hana.rb
View
16 lib/hana.rb
@@ -8,28 +8,24 @@ def initialize path
@path = Pointer.parse path
end
- def each
- @path.each { |x| yield x }
- end
-
- def to_a; @path.dup; end
+ def each(&block); @path.each(&block); end
+ def to_a; @path.dup; end
def eval object
Pointer.eval @path, object
end
+ ESC = {'^/' => '/', '^^' => '^', '~0' => '~', '~1' => '/'} # :nodoc:
+
def self.eval list, object
list.inject(object) { |o, part| o[(Array === o ? part.to_i : part)] }
end
def self.parse path
return [''] if path == '/'
- path.sub(/^\//, '').split(/(?<!\^)\//).map { |part|
- part.gsub!(/\^([\/^])/, '\1')
- part.gsub!(/~1/, '/')
- part.gsub!(/~0/, '~')
- part
+ path.sub(/^\//, '').split(/(?<!\^)\//).map! { |part|
+ part.gsub!(/\^[\/^]|~[01]/) { |m| ESC[m] }; part
}
end
end

0 comments on commit a421f59

Please sign in to comment.