# How to use ruby script/console to debug methods in application.rb?

**URL:** <https://www.ruby-forum.com/t/how-to-use-ruby-script-console-to-debug-methods-in-application-rb/183145>\
**Category:** Rails\
**Created:** [February 8, 2010, 11:11pm UTC](https://www.ruby-forum.com/t/how-to-use-ruby-script-console-to-debug-methods-in-application-rb/183145 "2010-02-08T23:11:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lukas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9a140/32.png) [@lukas](https://www.ruby-forum.com/u/lukas)\
**Post date:** [February 8, 2010, 11:11pm UTC](https://www.ruby-forum.com/t/how-to-use-ruby-script-console-to-debug-methods-in-application-rb/183145/1 "2010-02-08T23:11:38Z")

</div>

Hello everyone,

newbie question:

I have a couple of methods that interact with a remote python server  
in application.rb.  
Is there a convenient way to debug them? e.g. run them from “ruby  
script/console” - debug-console and see the return values?

Example-method:

def addUserToServer?(category\_name, useremail\_address)  
socket = TCPSocket.open(host, port)  
cmd = “COMMAND:adduser;;CAT:” + (category\_name,  
cmd += “;;EMAIL:” + useremail\_address  
socket.puts(cmd)  
result = socket.rcv(1024)  
socket.close()  
end

I’d like to see contents of result f.ex. and then return true or false  
accordingly.

Rails is version 2.2.2

Any help on this or some other best practice to check those methods is  
appreciated.

Lukas

---

<div class="post-metadata">

**Author:** ![lukas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9a140/32.png) [@lukas](https://www.ruby-forum.com/u/lukas)\
**Post date:** [February 8, 2010, 11:23pm UTC](https://www.ruby-forum.com/t/how-to-use-ruby-script-console-to-debug-methods-in-application-rb/183145/2 "2010-02-08T23:23:11Z")

</div>

lukas wrote:

> Hello everyone,
> 
> newbie question:
> 
> I have a couple of methods that interact with a remote python server  
> in application.rb.  
> Is there a convenient way to debug them? e.g. run them from “ruby  
> script/console” - debug-console and see the return values?

Just set a breakpoint, run script/server -u, and play around in the  
debugger.

> Example-method:
> 
> def addUserToServer?(category\_name, useremail\_address)

That should be add\_user\_to\_server. camelCase is considered poor style  
in Ruby.

> ```
> socket = TCPSocket.open(host, port)
> cmd = "COMMAND:adduser;;CAT:" + (category_name,
> 
> ```

You know you’ve got mismatched parentheses, right? And string  
concatenation is more efficient with the “#{}” syntax.

> ```
> cmd += ";;EMAIL:" + useremail_address
> socket.puts(cmd)
> result = socket.rcv(1024)
> socket.close()
> 
> ```

You don’t need empty parentheses when a function doesn’t take arguments.  
Frankly, your code looks more like Java than Ruby.

> end
> 
> I’d like to see contents of result f.ex. and then return true or false  
> accordingly.
> 
> Rails is version 2.2.2
> 
> Any help on this or some other best practice to check those methods is  
> appreciated.

Test-first development will reduce the need for debugging.

> Lukas

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lukas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9a140/32.png) [@lukas](https://www.ruby-forum.com/u/lukas)\
**Post date:** [February 9, 2010, 7:01pm UTC](https://www.ruby-forum.com/t/how-to-use-ruby-script-console-to-debug-methods-in-application-rb/183145/3 "2010-02-09T19:01:07Z")

</div>

Marnen,

thanks for your feedback.  
Indeed, I’m a newbie ruby programer. I’ll keep your advice in mind.

I was just hoping I could run the method from the debugging console  
and check the output directly.

---

<div class="post-metadata">

**Author:** ![lukas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9a140/32.png) [@lukas](https://www.ruby-forum.com/u/lukas)\
**Post date:** [February 9, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/how-to-use-ruby-script-console-to-debug-methods-in-application-rb/183145/4 "2010-02-09T20:27:44Z")

</div>

Hi Lukas,

You definitely can, by adding some temporary debug puts calls  
interspersed in your method, and then calling your method either  
using ./script/runner or from within a ./script/console session.

To make that method easier to test, I’d relocate that method outside  
of ./app/controllers/application.rb and put it somewhere else, maybe  
something like ./app/models/remote\_socket\_server.rb.

Possibly lame example, but:

###### add tmp debug calls to meth you want to test:

$ cat ./app/models/remote\_socket\_server.rb  
class RemoteSocketServer

HOST = …  
PORT = …

def RemoteSocketServer.add\_user(category, email)  
puts(“add\_user: category=#{category} email=#{email}”) #DEBUG  
socket = TCPSocket.open(HOST, PORT)  
…  
end  
…  
end

###### start console and call the meth:

$ ./script/console  
…

> > RemoteSocketServer.add\_user(“Stuff”, “removed\_email\_address@domain.invalid”)  
> > add\_user: category=Stuff email=removed\_email\_address@domain.invalid  
> > …

###### or run the meth via runner:

$ ./script/runner ‘RemoteSocketServer.add\_user(“Stuff”,  
“removed\_email\_address@domain.invalid”)’  
add\_user: category=Stuff email=removed\_email\_address@domain.invalid  
…

Also note that instead of using puts to stdout, you could change those  
debug puts calls to use the environment’s logger instead:

```
...
 ActionController::Base.logger.debug("add_user:

```

category=#{category} email=#{email}") #DEBUG  
…

such that when that meth is called (via console, runner, or in the web  
app), you can just look at the env log:

$ tail -100 ./log/development.log | grep “^add\_user”  
add\_user: category=Stuff email=removed\_email\_address@domain.invalid  
…

Once you’re done with your temp testing of the meth, just wipe out  
those debug puts/logger calls.

Jeff

---

<div class="post-metadata">

**Author:** ![lukas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9a140/32.png) [@lukas](https://www.ruby-forum.com/u/lukas)\
**Post date:** [February 10, 2010, 12:33am UTC](https://www.ruby-forum.com/t/how-to-use-ruby-script-console-to-debug-methods-in-application-rb/183145/5 "2010-02-10T00:33:36Z")

</div>

Thanks Jeff,

it worked. We’ve been able to test the whole stack of methods. 🙂

And I’ve learned something today.

Regards  
Lukas

---

<div class="post-metadata">

**Author:** ![lukas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9a140/32.png) [@lukas](https://www.ruby-forum.com/u/lukas)\
**Post date:** [February 9, 2010, 8:44pm UTC](https://www.ruby-forum.com/t/how-to-use-ruby-script-console-to-debug-methods-in-application-rb/183145/6 "2010-02-09T20:44:20Z")

</div>

lukas wrote:

> Marnen,
> 
> thanks for your feedback.  
> Indeed, I’m a newbie ruby programer. I’ll keep your advice in mind.

OK. You’re most welcome.

> I was just hoping I could run the method from the debugging console  
> and check the output directly.

You can. And I explained how. script/console is not a “debugging  
console”.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
