uncut words (string)

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.

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

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

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.

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/

Julian.

Julian Leviston 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/

Julian.


Learn: http://sensei.zenunit.com/

Last updated 20-May-09 (Rails, Basic Unix)

Blog: http://random8.zenunit.com/

Twitter: 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

Colin