Browse files

stop subclassing string

  • Loading branch information...
1 parent e76ced0 commit 188cc90af9b29d5520564af7bd7bbcdc647953ca @tenderlove tenderlove committed Jun 21, 2012
@@ -7,7 +7,7 @@ module ActionView
# = Action View Resolver
class Resolver
# Keeps all information about view path and builds virtual path.
- class Path < String
+ class Path
attr_reader :name, :prefix, :partial, :virtual
alias_method :partial?, :partial
@@ -19,9 +19,16 @@ def, prefix, partial)
def initialize(name, prefix, partial, virtual)
- @name, @prefix, @partial = name, prefix, partial
- super(virtual)
+ @name = name
+ @prefix = prefix
+ @partial = partial
+ @virtual = virtual
+ def to_str
+ @virtual
+ end
+ alias :to_s :to_str
andmej Jun 21, 2012

Just out of curiosity, why not define the to_s method directly instead of this alias?

tenderlove Jun 21, 2012 Ruby on Rails member

We probably could. I defined to_str because we need that in order to coerce to a string. e.g.:

irb(main):001:0> class Foo; def to_str; 'hello'; end end
=> nil
irb(main):002:0> class Bar; def to_s; 'hello'; end end
=> nil
irb(main):003:0> File.join('/',
=> "/hello"
irb(main):004:0> File.join('/',
TypeError: can't convert Bar into String
    from (irb):4:in `join'
    from (irb):4
    from /Users/aaron/.local/bin/irb:12:in `<main>'

I actually would rather not implement to_s.

andmej Jun 22, 2012

Got it. I didn't know about to_str and coercion. Thanks for the explanation!

tenderlove Jun 22, 2012 Ruby on Rails member

No problem! :-D

cattr_accessor :caching
@@ -6,7 +6,7 @@ def test_should_return_template_for_any_path
templates = resolver.find_all("path.erb", "arbitrary", false, {:locale => [], :formats => [:html], :handlers => []})
assert_equal 1, templates.size, "expected one template"
assert_equal "Template generated by Null Resolver", templates.first.source
- assert_equal "arbitrary/path.erb", templates.first.virtual_path
+ assert_equal "arbitrary/path.erb", templates.first.virtual_path.to_s
assert_equal [:html], templates.first.formats

0 comments on commit 188cc90

Please sign in to comment.