Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert activesupport string first met...

Replytotopic

activesupport string first method error

Posted in Forums : Ask a Rails expert

 
Profile

Authority 12
Posting Rating 0
Sign in to rate this post

Hi,

i’ve come across this weird error calling value.first

value = “2008/05/31 00:00:00”

value.class
String

value.first(10)
NoMethodError: undefined method `[]’ for #<enumerable::enumerator:0x652e760> from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_suppo
rt/core_ext/string/access.rb:43:in `first’

41 def first(limit = 1)
42 chars[0..(limit – 1)].to_s
43 end

value[0..9] = “2008/05/31”

I am at a loss (any help appreciated)

 
Profile

Authority 12
Posting Rating 0
Sign in to rate this post

OK, This occurs because i’ve upgraded to ruby 1.8.7 (had to do this to get mislavs will_paginate to install on windows)

from a mailing list

Ruby 1.8.7 has String#chars.
It returns an Enumerator object
but Rails 2.0.2 expects an ActiveSupport::Multibyte::Chars object.

You can avoid this conflict by putting
the following code in config/initializers/ directory.

----------------
unless ‘1.9’.respond_to?(:force_encoding) String.class_eval do begin remove_method :chars rescue NameError # OK end end
end
----------------

Note: activesupport/lib/active_support/core_ext/string/unicode.rb
of Rails 2.1.0 removes “chars” method from String class of Ruby 1.8.7.

 
Profile

Authority 75
Posting Rating 95
Sign in to rate this post

Make a test to prove this, and then submit a patch to the core!

Replytotopic

Other Recent Topics

Ask a Rails expert : Ruby Builder

Ask a Rails expert : Better programming skills

Ask a Rails expert : Select Option

Ask a Rails expert : Updating ruby version on windows

Ask a Rails expert : Periodicall Ajax Calls and Session Expiry

Ask a Rails expert : Sub queries in RoR

Ask a Rails expert : Remembering associations

Ask a Rails expert : Keeping the databse clean

Ask a Rails expert : How to redirect single app to 2 different URLs

Ask a Rails expert : Dealing with nested models

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel