# New problem: Newlines

**URL:** <https://www.ruby-forum.com/t/new-problem-newlines/157244>\
**Category:** wxRuby\
**Created:** [January 8, 2009, 8:47pm UTC](https://www.ruby-forum.com/t/new-problem-newlines/157244 "2009-01-08T20:47:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 8, 2009, 8:47pm UTC](https://www.ruby-forum.com/t/new-problem-newlines/157244/1 "2009-01-08T20:47:25Z")

</div>

I have this:

```
test2 = TextCtrl.new(frame, 10, "Output here!", Point.new(0, 501),

```

Size.new(500, 25), TE\_PROCESS\_ENTER)  
display.write\_text " \n You entered: #{str}"

But the ‘\n’ isn’t… calculated? Inserted? It reads that there is  
SOMETHING there, because the \n doesn’t show up in the text, but I can  
comment it out (\n) and the \n will then show. It looked like to me  
that the API documentation says that newline characters are the only  
control characters allowed in write\_text, and that they work. (Oh, and  
display is a variable for test2, in a different method)

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 8, 2009, 8:53pm UTC](https://www.ruby-forum.com/t/new-problem-newlines/157244/2 "2009-01-08T20:53:42Z")

</div>

Oh and also, is it possible/would it be ‘correct’ for me to add some  
stuff into the ‘main\_loop’? I would like to add something like:

def main\_loop  
Thread.new do ||  
inp = connection.recv(1000000)  
display.write\_text inp  
end  
end

would that screw with what is already in the main\_loop too much?

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 8, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/new-problem-newlines/157244/3 "2009-01-08T21:55:49Z")

</div>

Tim M. wrote:

> I have this:
> 
> ```
> test2 = TextCtrl.new(frame, 10, "Output here!", Point.new(0, 501),
> 
> ```
> 
> Size.new(500, 25), TE\_PROCESS\_ENTER)  
> display.write\_text " \n You entered: #{str}"
> 
> But the ‘\n’ isn’t… calculated? Inserted? It reads that there is  
> SOMETHING there, because the \n doesn’t show up in the text,

I’m sure that’ll be a newline; you could check by doing “p text2.value”  
in Ruby. I guess what’s happening is that Ruby is translating \n into a  
newline because it’s inside a double-quoted string. This is done before  
wxRuby gets the string, so wxRuby gets a literal newline character. But  
that works fine I think.

> but I can  
> comment it out (\n) and the \n will then show.  
> So - what’s the problem you’re trying to solve?

alex

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 8, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/new-problem-newlines/157244/4 "2009-01-08T23:29:39Z")

</div>

Alex F. wrote:

> Tim M. wrote:
> 
> > would that screw with what is already in the main\_loop too much?
> 
> It should work OK, but you’ll need to add something like
> 
> Wx::Timer.every(50) { Thread.pass }
> 
> to your main\_loop. This will ensure that on Ruby 1.8 your non-GUI ruby  
> threads get execution time, otherwise they won’t progress.
> 
> Have a look at the threaded.rb and chat client examples in the samples  
> directory as they demonstrate threads and network programming.
> 
> hth  
> alex

Thanks!

And the problem in the first post is this: When I run in, there is no  
linebreaks. I am trying to make it so that there ARE line breaks via the  
newline character.

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 9, 2009, 2:18am UTC](https://www.ruby-forum.com/t/new-problem-newlines/157244/5 "2009-01-09T02:18:47Z")

</div>

Sorry for even more noobiness, but; did I just find a new problem?

Code [  
require ‘rubygems’  
require ‘socket’  
require ‘wx’  
include Wx

$connection = TCPSocket.new(“[dark-legacy.com](http://dark-legacy.com)”, 9898)

class GuiMudClientApp \< App  
THREAD\_TIMER\_ID = ID\_HIGHEST + 1  
def on\_init  
#Timer.every(25) { Thread.pass }  
frame = Frame.new(nil, -1, “GMC: Incoming”, Point.new(100, 100),  
Size.new(500, 550))  
test = TextCtrl.new(frame, -1, “-----Mud stuff here-----”,  
Point.new(0, 0), Size.new(500, 500), TE\_READONLY)  
test.append\_text “Pfft!”  
test2 = TextCtrl.new(frame, 10, “Output here!”, Point.new(0, 501),  
Size.new(500, 25), TE\_PROCESS\_ENTER)  
frame.show  
evt\_text\_enter(10) do |event|  
cmdentered(event.get\_string, test2, test)  
end  
end

def cmdentered(str, textbox, display)  
display.write\_text “–(#{str})–”  
$connection.write(str)  
textbox.clear  
end

def main\_loop  
a = Thread.new do  
until connection.closed? == true  
input = $connection.recv(1000000)  
test.append\_text input  
end  
end  
Timer.every(25) { Thread.pass } ### LINE 35 \<----  
super  
end  
end

nowai = GuiMudClientApp.new

nowai.main\_loop  
]

Error:

/Library/Ruby/Gems/1.8/gems/wxruby-1.9.9-universal-darwin-9/lib/wx/classes/timer.rb:18:in  
`every': uninitialized constant Wxruby2::THE_APP (NameError) from guimc.rb:35:in `main\_loop’  
from guimc.rb:42

Line 35 is the marked line in the above code. What’s wrong?

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 8, 2009, 10:12pm UTC](https://www.ruby-forum.com/t/new-problem-newlines/157244/6 "2009-01-08T22:12:15Z")

</div>

Tim M. wrote:

> would that screw with what is already in the main\_loop too much?

It should work OK, but you’ll need to add something like

Wx::Timer.every(50) { Thread.pass }

to your main\_loop. This will ensure that on Ruby 1.8 your non-GUI ruby  
threads get execution time, otherwise they won’t progress.

Have a look at the threaded.rb and chat client examples in the samples  
directory as they demonstrate threads and network programming.

hth  
alex

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 9, 2009, 4:49am UTC](https://www.ruby-forum.com/t/new-problem-newlines/157244/7 "2009-01-09T04:49:44Z")

</div>

Mario S. wrote:

> You need to make sure that you pass the TE\_MULTILINE to the creation of  
> the  
> TextCtrl, EG:
> 
> test2 = TextCtrl.new(frame, 10, “Output here!”, Point.new(0,  
> 501),Size.new(500, 25), TE\_PROCESS\_ENTER|TE\_MULTILINE)
> 
> This will create a Multi-Line Text Control, which can display new lines,  
> if  
> you create a TextCtrl without the TE\_MULTILINE, it’ll create a Single  
> Line  
> Text Control, even though the sizer will resize it to look like it’s a  
> Multi-Line Text control.

Thats good, thanks for that.

Have any idea about the ‘THE\_APP’ error?

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 9, 2009, 5:42am UTC](https://www.ruby-forum.com/t/new-problem-newlines/157244/8 "2009-01-09T05:42:38Z")

</div>

Right now, I’m trying to get a compile of the latest Subversion that  
will be  
deemed 2.0 going, so that I can run my tests to see if there are any  
outstanding bugs that need to be covered. The WxRuby::THE\_APP error, is  
something that is kinda curious. I don’t have a Mac system in which to  
test  
out on, but from the looks of it, somehow WxRuby::THE\_APP (AKA  
Wx::THE\_APP),  
is not being set, when your code calls the GuiMudClientApp.new, which is  
suppose to be handled internally by wxRuby when you create the very  
first  
App class or App subclass.

Meaning, that there is only 1 App that can be in existance during the  
entire  
run of wxRuby. There are ways around it, but it’s only for thoes who  
are  
more experienced with the library, and know of the dangers of it.  
However,  
in this case, your problem occurs in main\_loop(), which is weird. My  
suggestion to you, would be to move the code out of main\_loop(), and  
throw  
it into on\_init(). Since you will have $connection setup by that time.  
Just to test and see if the problem actually does lie in main\_loop(), or  
not… And actually, I think on\_init() isn’t actually called till you  
execute main\_loop(), it’s part of the process it goes through once  
everything is initialized, it finalizes by running any on\_init() methods  
you  
have defined. So try moving the code there. That’s the best I can  
suggest,  
till I get wxWidgets and wxRuby compiled on Windows, to test your code,  
to  
see if it has the same problem.

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 9, 2009, 2:47am UTC](https://www.ruby-forum.com/t/new-problem-newlines/157244/9 "2009-01-09T02:47:04Z")

</div>

You need to make sure that you pass the TE\_MULTILINE to the creation of  
the  
TextCtrl, EG:

test2 = TextCtrl.new(frame, 10, “Output here!”, Point.new(0,  
501),Size.new(500, 25), TE\_PROCESS\_ENTER|TE\_MULTILINE)

This will create a Multi-Line Text Control, which can display new lines,  
if  
you create a TextCtrl without the TE\_MULTILINE, it’ll create a Single  
Line  
Text Control, even though the sizer will resize it to look like it’s a  
Multi-Line Text control.

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 9, 2009, 9:54pm UTC](https://www.ruby-forum.com/t/new-problem-newlines/157244/10 "2009-01-09T21:54:03Z")

</div>

Okay, thanks you two! 🙂

I’ll test it in a bit.

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 9, 2009, 9:35am UTC](https://www.ruby-forum.com/t/new-problem-newlines/157244/11 "2009-01-09T09:35:36Z")

</div>

Tim M. wrote:

> def on\_init  
> #Timer.every(25) { Thread.pass }

…

> def main\_loop  
> a = Thread.new do  
> until connection.closed? == true  
> input = $connection.recv(1000000)  
> test.append\_text input  
> end  
> end  
> …  
> nowai = GuiMudClientApp.new

Don’t override main\_loop unless you know what you’re doing. And if you  
do override it, you must call super() within it. This is the source of  
your error.

Basically, all of your start-up code - including your Thread  
initialisation - should go in the on\_init method. This goes for prety  
much any App.

a
