How to trim a string?

This must be really simple but I have looked everywhere.

I have a <%= item.description -% in my view and I want to trim it to 10
words.

The description can be long and I want to trim this in my preview?

There may be a better way, but “dassdfasdfs”[0…10]

mark

“Rails is a full-stack framework for developing database-backed web
applications according to the Model-View-Control pattern. From the
Ajax in the view, to the request and response in the controller, to
the domain model wrapping the database, Rails gives you a pure-Ruby
development environment. To go live, all you need to add is a database
and a web server.”.split[1…10].join(" ")

Mathieu C. wrote:

“Rails is a full-stack framework for developing database-backed web
applications according to the Model-View-Control pattern. From the
Ajax in the view, to the request and response in the controller, to
the domain model wrapping the database, Rails gives you a pure-Ruby
development environment. To go live, all you need to add is a database
and a web server.”.split[1…10].join(" ")

Thanks, I knew it would be simple! Brain hurts!

and a web server.".split[1…10].join(" ")

Oups : [0…9]

On 4/27/06, James W. [email protected] wrote:

This must be really simple but I have looked everywhere.

I have a <%= item.description -% in my view and I want to trim it to 10
words.

The description can be long and I want to trim this in my preview?

item.description.split[0…9].join " "

It blows away whitespace like extra spaces and carriage returns, but
that wouldn’t have mattered in a view anyway so it should be fine.

– James

Or [0…10]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs