lukas
2010-Feb-08 22:08 UTC
How to use ruby script/console to debug methods in application.rb?
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Feb-08 22:23 UTC
Re: How to use ruby script/console to debug methods in appl
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.> > LukasBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. On Feb 8, 11:23 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jeff Lewis
2010-Feb-09 19:26 UTC
Re: How to use ruby script/console to debug methods in appl
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", "foo-rfuReZR+ibo@public.gmane.org")add_user: category=Stuff email=foo-rfuReZR+ibo@public.gmane.org ... ###### or run the meth via runner: $ ./script/runner ''RemoteSocketServer.add_user("Stuff", "foo-rfuReZR+ibo@public.gmane.org")'' add_user: category=Stuff email=foo-rfuReZR+ibo@public.gmane.org ... 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=foo-rfuReZR+ibo@public.gmane.org ... Once you''re done with your temp testing of the meth, just wipe out those debug puts/logger calls. Jeff On Feb 9, 9:59 am, lukas <lukas.zielin...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 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. > > On Feb 8, 11:23 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > > > 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-Koserhttp://www.marnen.org > > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Feb-09 19:44 UTC
Re: How to use ruby script/console to debug methods in appl
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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Jeff, it worked. We''ve been able to test the whole stack of methods. :) And I''ve learned something today. Regards Lukas On Feb 9, 8:44 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.