On Sunday 24 October 2004 15:37, ts wrote:
> >>>>> "S" == Simon Strandgaard <neoneye / adslhome.dk> writes:
>
> S>     raise TypeError, "cannot convert #{n.class} to Integer" unless
> n.kind_of? S> (Integer)
>
> svg% ruby -e 'class A; def to_int() 2 end; end; p [1,2,3].first(A.new)'
> [1, 2]
> svg%
>
>
> Guy Decoux


Ok.. I think I have fixed this issue now.


irb(main):001:0> 'abc'.last(3)
=> "abc"
irb(main):002:0> class A; def to_int; 2 end; end
=> nil
irb(main):003:0> 'abc'.last(A.new)
=> "bc"
irb(main):004:0> class B; def to_int; 'a' end; end
=> nil
irb(main):005:0> 'abc'.last(B.new)
TypeError: B#to_int should return Integer
        from ./a.rb:22:in `last'
        from (irb):5
irb(main):006:0>



New implementation is here:

class String
  def first(length=nil)
    length ||= 1
    unless length.respond_to?(:to_int)
      raise TypeError, "cannot convert #{length.class} to Integer"
    end
    n = length.to_int
    unless n.kind_of?(Integer)
      raise TypeError, "#{length.class}#to_int should return Integer"
    end
    raise ArgumentError, "negative string size" if n < 0
    n = [n, self.size].min
    self[0, n]
  end
  def last(length=nil)
    length ||= 1
    unless length.respond_to?(:to_int)
      raise TypeError, "cannot convert #{length.class} to Integer"
    end
    n = length.to_int
    unless n.kind_of?(Integer)
      raise TypeError, "#{length.class}#to_int should return Integer"
    end
    raise ArgumentError, "negative string size" if n < 0
    n = [n, self.size].min
    self[-n, n]
  end
end




btw: Is this better?

--
Simon Strandgaard