Problem with putting a String in a String

Hi, I wanted to write a function, which gets a text and will automaticly put an \n in the text if the text is bigger than the screen.

Here is the code:

def draw_text_with_n(text, x, y) t = text.split(" ") n_text = "" t.each do |word| line = "" if line.length + word.length < self.width line << "#{word} " else n_text = line + "\n" line.clear end end draw_text_ex(x, y, n_text) endNow i have the Problem that this line: 

line << "#{word} "Makes this:

"This "

"is "

"an "

"example"

But it should look like this: "This is an example"

So my code dont work.

Could someone help me pls?
Assuming your input is valid the only problem I can see is this condition not doing what you think it's doing.

Code:
if line.length + word.length < self.width
You should check that your logic/math is correct.
I believe that line = "" is placed wrong. You clear the line variable for every word.

Also the return draw_text_ex(x, y, n_text) will bug if you only have 1 line. n_text is "".

Shorter version (doesn't take words into account):

def wrap_long_string(text,max_width = 10)(text.length < max_width) ?text :text.scan(/.{1,#{max_width}}/).join("\n")endhttps://gist.github.com/Amitesh/1160287

Better sample:

Code:
TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec placerat, lacus et lacinia congue, mi ipsum tempus mauris, id mollis ligula nulla ut ligula. Curabitur vestibulum enim sed ante bibendum sagittis. Donec blandit a mi quis blandit. Sed ultrices, lectus ac tincidunt vestibulum, libero dolor rutrum lectus, vel feugiat felis augue volutpat tortor. Fusce orci velit, malesuada eget quam quis, malesuada ullamcorper lorem. Pellentesque aliquam magna lorem, eu eleifend libero adipiscing quis. Donec posuere laoreet libero, vitae pretium dolor fermentum pulvinar. Praesent bibendum, nunc et auctor porta, velit magna egestas orci, ac varius tellus libero in metus. Pellentesque ac sem at mauris facilisis interdum. Aenean non metus vitae orci sodales aliquam. Aliquam tincidunt turpis a mi congue, ac condimentum libero lacinia. Morbi laoreet rhoncus ipsum, in cursus urna viverra at."def wrap_text(txt, col = 80)  txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,    "\\1\\3\n") end  p wrap_text TEXT, 40p wrap_text 'Hello world. This is a sample line.', 10
There is one other thing which I found out. If the text is too small and never reaches the self.width. It won't be drawn at all^^ Hmm, must look how to fix this.

I have made this to test the condition:

def draw_text_with_n(text, x, y) t = text.split(" ") n_text = "" t.each do |word| line = "" temp = line + "#{word} " p(temp.length) if temp.length < self.width line << "#{word} " else n_text = line + "\n" line.clear end end draw_text_ex(x, y, n_text) endThe console window looks like this(For the word: "The" and "hello": 

4

6

But it should look like this: 10

The text is put verticaly into the variable and I dont understand why..
Can we assume self.width is greater than those values? Just from looking at the code, it should only be possible to have line breaks if the length of your temp line is greater than the width.
Yes self.width is much greater then a word in the array. I think you dont understand what i mean. The problem is not in the condition, the problem is because this

temp = line + "#{word} "

and

line << "#{word} "

dont put the words horizontally in the variables but vertically and I dont know why... 

Haha I found the problem. I have defined line = "" in the loop.
Remember that String#length returns a number of characters while Bitmap#width and Window#width (I assume you put your code in) return a number of pixels.
 

As Napoleon mentioned, you reset "line" every time you begin with a new word, so line << "#{word} " does not have any effect.

In addition, "n_text" cannot store more than one line since a new line replaces the previous content.

Considering that, I am surprised your code works so far anyway... ^^
Yes self.width is much greater then a word in the array. I think you dont understand what i mean. The problem is not in the condition, the problem is because this


temp = line + "#{word} "


and


line << "#{word} "


dont put the words horizontally in the variables but vertically and I dont know why...
Are you sure that's what the problem is? Because you can't store strings "vertically".


The reason why your words are coming up like that is either


1: your x,y values are acting funny


2: you have line-breaks after every line


3: possibly something else I am not seeing


Try printing out each text before you draw them.
I didnt seen that napoleon have edited his post(There was only "Never mind"). But this are the problems I have. But the first was solved already I had edited my post before.

edit*

I will use this text_size().width instead of .length
I feared that you might overlook it. But forum rules... require me to edit it... But the first post that I made I quickly edited because right after I posted I saw how 'dumb' my original post was haha.
Ok, thanks all for the help, this is the new code and it works without problems(I haven't seen any atm):

Code:
  def draw_text_with_n(text, x, y)    t = text.split(" ")    n_text = ""    line = ""    t.each do |word|      if text_size(line + word).width < (self.width - 32)        line << "#{word} "      else        n_text += line + "\n"        line = "#{word} "      end    end       n_text = line if n_text == ""     draw_text_ex(x, y, n_text)  end
line << "#{word} " will add one space too much at the end of a line, for each line. Not a biggie but still not 'proper' 😉 . You need an extra check.

Word Wrap alternative (added it to my first post, but prints out one \n too much at the end):

Code:
def wrap_text(txt, col = 80)  txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,    "\\1\\3\n") end
Ohh, yes. Thanks
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/problem-with-putting-a-string-in-a-string.17735/