`
yangzb
  • 浏览: 3491703 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

undefined method `length' for Enumerable when calling truncate method

    博客分类:
  • Ruby
阅读更多

With rails 2.0.2 and ruby 1.8.7 you can find this error using rails truncate helper

To solve this problem, paste this code in your enviroment.rb (eof).

module ActionView
  module Helpers
    module TextHelper
      def truncate(text, length = 30, truncate_string = "...")
        if text.nil? then return end
        l = length - truncate_string.chars.to_a.size
        (text.chars.to_a.size > length ? text.chars.to_a[0...l].join + truncate_string : text).to_s
      end
    end
  end
end

Have a nice day!

Incompatible with Ruby 1.8.7

If using Rails < 2.2 with Ruby 1.8.7, calling truncate will result in the following error:

  undefined

 method

 `length

'

 for #<Enumerable::Enumerator:0xb74f952c>




The workaround (other than upgrading to Rails 2.2 or higher), is to overwrite the truncate method, by inserting the following at the end of environment.rb (or where it will be called on startup):

分享到:
评论

相关推荐

    前端开源库-node-enumerable

    "node-enumerable" 就是一个这样的开源库,专门为前端开发设计,旨在为Node.js环境提供类似C# LINQ(Language Integrated Query)的功能,并且完全适配ES6语法,同时使用TypeScript进行编写,确保了代码的类型安全...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: The TFlexPanel.FindControlAtPoint method maked virtual to realize RealTime-capability when on mouse cursor moving the flex-object search not occurs. - FIX: After deleting the selected points ...

    Prototype Enumerable对象 学习第1/2页

    【Prototype Enumerable对象】是Prototype框架的核心组成部分,它提供了一系列用于枚举操作的实用方法,即处理集合对象的方法。Enumerable本身并不是一个独立使用的对象,而是一个模块,设计用于与其他对象混合...

    Recursively Enumerable Sets and Degrees(Soare)

    Recursively Enumerable Sets and Degrees by Rovert I. Soare。递归论的书籍,Soare的,从djvu转过来的,有可能有误。经典的教材

    通过Enumerable实现线程控制(源码 更新)

    Enumerable扩展的线程控制是一种优化并发执行的方式,它基于AsyncEnumerator的概念,允许开发者更精细地管理和监控线程的运行状态。 AsyncEnumerator是一个异步枚举器,通常用于异步编程,它可以将一个枚举序列转化...

    Prototype源码浅析 Enumerable部分(二)

    在JavaScript的Prototype库中,Enumerable模块提供了一系列用于处理集合(如数组)的方法,极大地扩展了JavaScript的迭代功能。本文将重点解析Enumerable中的`$break`和`$continue`概念,以及`grep`方法的设计思想。...

    LINQ Method Syntax_linq_源码

    1. **Enumerable类和Queryable类**:LINQ的方法语法基于两个关键接口——`System.Linq.Enumerable`和`System.Linq.Queryable`。前者用于非数据库源,后者用于数据库源。这两个接口定义了一系列的扩展方法,如`Select...

    Prototype使用指南之enumerable.js

    在JavaScript的世界里,`Prototype`库提供了一个名为`Enumerable`的抽象对象,它不直接用于实例化,而是作为基础框架,被多种枚举类型如`Hash`、`Array`、`Range`等继承。在JavaScript中,由于没有真正的类概念,类...

    Prototype源码浅析 Enumerable部分之each方法

    for (var i = 0, len = this.length; i ; i++) { iterator.call(context, this[i], i, this); } }; ``` 为了使`Enumerable`能够应用于不同的对象,Prototype库中`Enumerable`的`each`方法实际上会调用对象自身的...

    Enumerable.js:适用于所有类型集合的实用函数

    这个库很大程度上受到 Ruby 的 Enumerable 模块的启发,并借用了它的大量功能。 注意:这并不意味着是一个独立的库,而是要与具有.each方法的另一种数据类型结合使用。安装作为 NPM 模块npm install enumerable-js ...

    C#使用LINQ中Enumerable类方法的延迟与立即执行的控制

    本文将深入探讨如何使用LINQ中的Enumerable类方法来控制查询的延迟执行和立即执行,这对于优化代码性能至关重要。 Enumerable类是.NET Framework 3.5及以上版本中引入的,它包含在System.Core.dll程序集中,属于...

    enumerable.lua:一个lua集合库

    local Enumerable = require ( ' enumerable ' ) collectionInstance = Enumerable. create ({ 1 , 2 , 3 , 4 , 5 }) secondElement = collectionInstance[ 2 ] collectionInstance: each ( function ( value , ...

    enumerable-methods

    该项目展示了Ruby Enumerable模块中方法的重建列表。 重写的Enumerable方法是: 每个-&gt; my_each each_with_index-&gt;​​ my_each_with_index 选择-&gt; my_select 全部? -&gt; my_all? 任何? -&gt; my_any? 没有...

    CSharp 4.0 .Net Framework V4.0 Enumerable 类

    Enumerable 类在 C# 中是 LINQ (Language Integrated Query) 的一部分,它位于 .NET Framework 4.0 的 System.Linq 命名空间中。这个类提供了大量静态方法,用于查询实现了 IEnumerable&lt;T&gt; 接口的对象,如数组、列表...

    javascript中undefined的本质解析

    - `enumerable`: `false`,因此`for...in`循环不会包含`undefined`。 - `configurable`: `false`,这意味着无法通过`Object.defineProperty`重新定义或删除`undefined`。 正是因为这些特性,即使尝试给`undefined`...

    enumerable:一个简单的 JavaScript 可枚举库

    在JavaScript中,`enumerable`属性是一个对象的属性特性,决定了该属性是否可以通过`for...in`循环或者`Object.keys()`等方法枚举。默认情况下,对象的自定义属性是可枚举的,而原型链上的属性(除非显式指定)通常...

    06-for-in和for-of的区别.md

    # for-in和for-of的区别 在JavaScript编程中,循环结构是一种常用的控制流语句,用于对数组或对象的元素进行迭代。`for-in`和`for-of`是两种用于迭代的循环结构,它们在语法和使用上存在一些差异。了解它们之间的...

    mongoid_enumerable:Mongoid 可枚举

    gem 'mongoid_enumerable' 然后执行: $ bundle 或者自己安装: $ gem install mongoid_enumerable 用法 只需在您的文档中包含MongoidEnumerable 。 添加enumerable后: 字段名称 具有可能值的数组 选项( ...

    enumerable-statistics

    Enumerable::Statistics 提供了一些方法来计算数组和枚举中的统计汇总。 安装 将此行添加到应用程序的 Gemfile 中: gem 'enumerable-statistics' 然后执行: $ bundle 或者自己安装: $ gem install ...

Global site tag (gtag.js) - Google Analytics