Hi.
I think what you want to do is word-wrapping across lines depending on
line length.
Here's a pretty nice algorithm using a regular expression:
class String
def word_wrap(line_length=80)
self.gsub(/(.{1,#{line_length}})(\s+|\Z)/, "\\1\n")
end
end
What this does is put the word_wrap method onto the string class.
So, you can do this:
string_to_wrap = "Kid games need to be both fun and educational. Aimed
at ages pre-K through middle school, safe environment to discover
their abilities and learn new skills with interactive and fun computer
games. Our games build skills in math, logic, memory, vocabulary,
alphabet, spelling, geography, computer skills, color identification,
shape identification and other various problem solving. Our commitment
to parents, teachers, and kids, is to connect earning and skill
building with a sense of challenge, fun, and self esteem."
puts string_to_wrap.wrap
If you want to change the number of characters that it will wrap at
(ie line length), which defaults to 80... then specify it as an
argument:
puts string_to_wrap.wrap(10)
Good luck, and remember that when posting questions to this list, it's
really good to carefully prepare your question.
Julian.
----------------------------------------------
Learn: http://sensei.zenunit.com/
Last updated 20-May-09 (Rails, Basic Unix)
Blog: http://random8.zenunit.com/
Twitter: http://twitter.com/random8r
On 27/05/2009, at 5:15 PM, Ruby One wrote:
>
> I have this paragraph as one srting , apllied below code as
> suggested by
> one of the member.
>
> words = "Kid games need to be both fun and educational. Aimed at ages
> pre-K through middle school, safe environment to discover their
> abilities and learn new skills with interactive and fun computer
> games. Our games build skills in math, logic, memory, vocabulary,
> alphabet, spelling, geography, computer skills, color identification,
> shape identification and other various problem solving. Our commitment
> to parents, teachers, and kids, is to connect earning and skill
> building with a sense of challenge, fun, and self esteem."
>
> line_length = 10
>
> word_arry = words.split(" ")
>
> text = (0..(words.length / line_length)).inject([]) {|v,num| start =
> num * line_length; v << %Q{<= #{words[(start)...(start +
> line_length)].join(" ")} =>}}
>
> text.join("\n")
>
> It cuts the string after every 10 characters into like this
>
> <= Kid games need to be both fun and educat =><= ional. Aimed at ages
> pre-K through middl =><= e school, safe environment to discover t =><=
> heir
> abilities and learn new skills with =><= interactive and fun computer
> games. Our =><= games build skills in math, logic, memo =><= ry,
> vocabulary,
> alphabet, spelling, geog =><= raphy, computer skills, color identifica
> =><= tion,
> shape identification and other var =><= ious problem solving. Our
> commitment
> to =><= parents, teachers, and kids, is to conne =><= ct earning and
> skill
> building with a sen =><= se of challenge, fun, and self esteem. =>
>
>
> What i need is the words remain uncut if it comes in the range like in
> first line i want eductaional to be complete if it is not in range
> than
> it wil show upto "and"
>
>
>
> any kind of help highly appreciated,its a urgent need.Please provide
> any
> way to it.
>
> Regards & thanks
>
> amit
> --
> Posted via http://www.ruby-forum.com/.
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---