Forum: Ruby on Rails How to trim a string?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
James W. (Guest)
on 2006-04-27 18:20
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?
Mark Van H. (Guest)
on 2006-04-27 18:33
(Received via mailing list)
There may be a better way, but "dassdfasdfs"[0..10]

mark
Mathieu C. (Guest)
on 2006-04-27 18:33
(Received via mailing list)
"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(" ")
James W. (Guest)
on 2006-04-27 18:37
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!
James L. (Guest)
on 2006-04-27 18:37
(Received via mailing list)
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
Mathieu C. (Guest)
on 2006-04-27 18:40
(Received via mailing list)
> and a web server.".split[1..10].join(" ")

Oups : [0..9]
Carl-Johan K. (Guest)
on 2006-04-27 19:04
(Received via mailing list)
Or [0...10]
This topic is locked and can not be replied to.