method
first
v1.1.6 -
Show latest stable
-
4 notes -
Class: ActiveSupport::CoreExtensions::String::Access
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (0)
- 2.0.3 (0)
- 2.1.0 (0)
- 2.2.1 (-38)
- 2.3.8 (38)
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
first(limit = 1)
public
Register or
log in
to add new notes.
insane-dreamer -
February 26, 2009 - (v1.0.0 - v2.1.0)
0 thanks
Conflicts with Ruby 1.8.7
Using this with Rails < 2.2.x and Ruby 1.8.7 will create a conflict between ActiveSupport and Ruby, generating the following error:
>> '/'.first NoMethodError: undefined method `[]' for #<Enumerable::Enumerator:0x176b974> from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/string/access.rb:43:in `first'
So if using an older version of Rails with Ruby 1.8.7, use String to instead of String.first
insane-dreamer -
March 12, 2009
0 thanks
insane-dreamer -
March 12, 2009 - (<= v2.1.0)
0 thanks
Another fix
Another way around this problem, with code that already employs String.first, is to change the ActiveSupport definition as follows (in environment.rb)
module ActiveSupport module CoreExtensions module String module Access def first(limit = 1) chars.to_a[0..(limit - 1)].to_s end end end end end