How to trim a string?


#1

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?


#2

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

mark


#3

“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(" ")


#4

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!


#5

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

Oups : [0…9]


#6

On 4/27/06, James W. removed_email_address@domain.invalid 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


#7

Or [0…10]