# Uncut words (string)

**URL:** <https://www.ruby-forum.com/t/uncut-words-string/168900>\
**Category:** Rails\
**Created:** [May 27, 2009, 9:15am UTC](https://www.ruby-forum.com/t/uncut-words-string/168900 "2009-05-27T09:15:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [May 27, 2009, 9:15am UTC](https://www.ruby-forum.com/t/uncut-words-string/168900/1 "2009-05-27T09:15:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [May 27, 2009, 12:59pm UTC](https://www.ruby-forum.com/t/uncut-words-string/168900/2 "2009-05-27T12:59:50Z")

</div>

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/](http://sensei.zenunit.com/)  
Last updated 20-May-09 (Rails, Basic Unix)  
Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [May 27, 2009, 1:53pm UTC](https://www.ruby-forum.com/t/uncut-words-string/168900/3 "2009-05-27T13:53:32Z")

</div>

Julian L. wrote:

> 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/](http://sensei.zenunit.com/)  
> Last updated 20-May-09 (Rails, Basic Unix)  
> Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
> Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)

Thanks julian for your help almost at the end, little help more.

i used your regular expression

line\_length = 40  
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.”

words = words.gsub(/(.{1,#{line\_length}})(\s+|\Z)/, “\<=\1\n”)  
words = words.sub(‘\<=’, ‘’)  
abort words

output

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.

need to append “=\>” at end of each line to like in startin.

waiting for you last help

Regards

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [May 27, 2009, 2:00pm UTC](https://www.ruby-forum.com/t/uncut-words-string/168900/4 "2009-05-27T14:00:20Z")

</div>

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.”

word\_arry = words.split(" ")

count=0  
line\_text=“\<=”  
word\_arry.each {|word|  
count+=word.length  
if count \> 40  
count=0  
line\_text+=“=\>\n\<=”+word+" "  
else  
end\_txt = (count \> 38) ? word : word+" "  
line\_text+=end\_txt  
end  
}  
line\_text+=“=\>”

puts line\_text

On Wed, May 27, 2009 at 5:23 PM, Ruby O.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > def word\_wrap(line\_length=80)  
> > their abilities and learn new skills with interactive and fun computer  
> > argument:  
> > Last updated 20-May-09 (Rails, Basic Unix)  
> > ages  
> > abort words  
> > \<=logic, memory, vocabulary,
> 
> waiting for you last help
> 
> ## Regards
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
With regards,  
Arunkumar B.  
9789980534.  
[http://thinkingrails.blogspot.com](http://thinkingrails.blogspot.com)

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [May 27, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/uncut-words-string/168900/5 "2009-05-27T14:15:37Z")

</div>

On 27/05/2009, at 9:53 PM, Ruby O. wrote:

> 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."
> 
> words = words.gsub(/(.{1,#{line\_length}})(\s+|\Z)/, “\<=\1\n”)  
> words = words.sub(‘\<=’, ‘’)  
> abort words

Hi.

Is it really too much work for you to work out what you need to do?

class String  
def word\_wrap(line\_length=80)  
self.gsub(/(.{1,#{line\_length}})(\s+|\Z)/, “\<=\1=\>\n”)  
end  
end

Perhaps you should join [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

Julian.

* * *

Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
Last updated 20-May-09 (Rails, Basic Unix)  
Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [May 27, 2009, 2:11pm UTC](https://www.ruby-forum.com/t/uncut-words-string/168900/6 "2009-05-27T14:11:30Z")

</div>

On 27/05/2009, at 9:59 PM, Arunkumar B wrote:

> end  
> }  
> line\_text+=“=\>”
> 
> puts line\_text

This goes to show very easily that you can write really bad code in  
any language.

You have a beautiful set of iterators and other idiomatic ruby  
available to you. Why on earth would you denigrate ruby to working  
like a procedural language?

Julian.

* * *

Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
Last updated 20-May-09 (Rails, Basic Unix)  
Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [May 27, 2009, 2:47pm UTC](https://www.ruby-forum.com/t/uncut-words-string/168900/7 "2009-05-27T14:47:09Z")

</div>

Julian L. wrote:

> On 27/05/2009, at 9:53 PM, Ruby O. wrote:
> 
> > 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."
> > 
> > words = words.gsub(/(.{1,#{line\_length}})(\s+|\Z)/, “\<=\1\n”)  
> > words = words.sub(‘\<=’, ‘’)  
> > abort words
> 
> Hi.
> 
> Is it really too much work for you to work out what you need to do?
> 
> class String  
> def word\_wrap(line\_length=80)  
> self.gsub(/(.{1,#{line\_length}})(\s+|\Z)/, “\<=\1=\>\n”)  
> end  
> end
> 
> Perhaps you should join [http://sensei.zenunit.com/](http://sensei.zenunit.com/)
> 
> Julian.
> 
> * * *
> 
> Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
> Last updated 20-May-09 (Rails, Basic Unix)  
> Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
> Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)

Great it worked for me !!

is there any solution to traverse a string character by charcater in  
ruby on rails.

Regards

amit

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [May 28, 2009, 12:07am UTC](https://www.ruby-forum.com/t/uncut-words-string/168900/8 "2009-05-28T00:07:01Z")

</div>

Split your string to words like described. Then create an empty array  
of lines and an empty temporary string. Loop over your words. In the  
loop, pop a word off the words array and append it to the temp.  
String. Check for the length of it. If it is more than line\_length  
push the temp. String to the lines array and break the loop, else do  
nothing. Repeat this until your words array is empty (this means  
another loop around the described one).

While this is not as elegant as the solution posted before, it might  
be understandable by a novice programmer.

2009/5/27, Ruby O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> to parents, teachers, and kids, is to connect earning and skill  
> text.join(“\n”)  
> =\>\<= tion,
> 
> >

–  
Von meinen Mobilgerät aus gesendet

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [May 27, 2009, 3:32pm UTC](https://www.ruby-forum.com/t/uncut-words-string/168900/9 "2009-05-27T15:32:10Z")

</div>

2009/5/27 Ruby O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > > words = words.sub(‘\<=’, ‘’)  
> > > self.gsub(/(.{1,#{line\_length}})(\s+|\Z)/, “\<=\1=\>\n”)  
> > > Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
> > > Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)
> 
> Great it worked for me !!
> 
> is there any solution to traverse a string character by charcater in  
> ruby on rails.

Have you tried looking through the documentation for the String class?  
See  
[http://www.ruby-doc.org/core/classes/String.html](http://www.ruby-doc.org/core/classes/String.html)

Colin
