Posts tagged: ruby

Netbeans 6.9.1 and Ruby debugging

Some notes made while trying to get Netbeans 6.9.1 to debug Rails applications running on various Ruby stacks.

First and foremost, no success at all trying to use the Ruby debugging support internal to Netbeans with any combination of stacks based on Ruby MRI 1.8.6. Mind you, in every case, the code ran flawlessly under Netbeans, just not in debug mode.

When trying to run attaching the debugger to an external Ruby interpreter running in debug mode, success every time.

Combinations that were tried and succeeded. Others too numerous to mention failed somewhere along – compile, install, etc.

OS Ruby RubyGems ruby-debug-ide ruby-debug-base linecache
OS X 10.6.4 1.8.6 p111 1.0.1 0.4.4 0.10.3 0.3
OS X 10.6.4 1.8.6 p111 1.3.3 0.4.9 0.10.3 0.43
OS X 10.6.4 1.8.6 p111 1.3.5 0.4.9 0.10.3 0.43

No testing with Ruby debugging under jRuby in any combination. That’s yet to come.

A few conclusions (may be obvious to some!):

  • The combination of Ruby and RubyGems version is critical
  • It seems, after RubyGems 1.1 (more likely 1.2), the debug support stabilized
  • With RubyGems 1.3.x, the latest debug support is likely the best.

A final note, not particularly related to debugging, but rather to multiple Ruby platform support in Netbeans.

Using the same base version of Ruby for all the tests led to unmitigated user confusion when configuring the different stacks – Tools > Ruby Platforms.

An egregious hack that hasn’t appeared to hurt anything  is to manually edit the file ~/.netbeans/6.9/build.properties and add some text to the ruby_version tag for each different Ruby installation to differentiate one from another.

An example

In a console, run a command similar to:

rdebug-ide _0.4.9_ -p 7000 -d -- mongrel_rails start

Open the project in Netbeans, Debug > Attach Debugger and accept the defaults. Then use Netbeans debugging tools normally.

Ruby and Debugging

A small gathering of information about Ruby and debugging tools.

All debugging tools for Ruby ultimately exploit the same feature of Ruby – the ability of the Ruby interpreter to single-step through code and set breakpoints.  Usually, two pieces are involved – the copy of the Ruby interpreter running the Ruby code in debug mode and the program issuing debug commands (via an established interface).

Ruby comes with its own command-line debugger – rdebug. In this case, rdebug acts a shell that sends debug commands to the Ruby program executing in the same instance of Ruby. It involves a little trickery, but is no different in concept to any other debugging technique.

IDEs and other tools use essentially the same hooks, accessed through interfaces such as ruby-debug or ruby-debug-ide. In these cases, the IDE presents the source code and execution context of the program in a user-friendly fashion. When a user clicks on a breakpoint, or runs the program, the IDE sends an appropriate debug command to the Ruby interpreter running the application in debug mode.

When trying to understand why Ruby debugging sometimes doesn’t work, it helps to know that the following elements must all be in agreement:

  • The version of the OS
  • The Ruby interpreter and the debugging interface it makes available
  • The version of RubyGems
  • The library that provides ‘packaged’ access to the debugging interface (usually ruby-debug-ide or ruby-debug)
  • The implementation of the Ruby debug support in the IDE

There is no magic solution here – a lot of googling and trial and error.

An example

It is instructive to set up an IDE to debug Ruby externally, connect to a command-line instance of the program being debugged, and watch the debug console.

In a console window, in the directory containing the program, issue a command similar to:

rdebug-ide _0.4.9_ -p 7000 -d -- mongrel_rails start

In the IDE of choice,  open up the application and attach to the remote debugging session – in this case running on localhost on port 7000.

Issue breakpoints, run commands, variable inspections in the IDE and watch the activity in the console window.

Ruby on Rails startup errors

Ruby on Rails startup errors

If you see something similar to the following when starting Rails:

!!! Path to log file not valid: log/m

All you need to do is to create the ./log directory.

Ruby – internal configuration information

Ruby – to see Ruby internal configuration information

require 'rbconfig'
Config::CONFIG.inspect

More usefully, something like:

Config::CONFIG["prefix"]

List of properties:

  • DESTDIR
  • INSTALL
  • prefix
  • EXEEXT
  • ruby_install_name
  • RUBY_INSTALL_NAME
  • RUBY_SO_NAME
  • SHELL
  • PATH_SEPARATOR
  • PACKAGE_NAME
  • PACKAGE_TARNAME
  • PACKAGE_VERSION
  • PACKAGE_STRING
  • PACKAGE_BUGREPORT
  • exec_prefix
  • bindir
  • sbindir
  • libexecdir
  • datarootdir
  • datadir
  • sysconfdir
  • sharedstatedir
  • localstatedir
  • includedir
  • oldincludedir
  • docdir
  • infodir
  • htmldir
  • dvidir
  • pdfdir
  • psdir
  • libdir
  • localedir
  • mandir
  • ECHO_C
  • ECHO_N
  • ECHO_T
  • LIBS
  • build_alias
  • host_alias
  • target_alias
  • MAJOR
  • MINOR
  • TEENY
  • build
  • build_cpu
  • build_vendor
  • build_os
  • host
  • host_cpu
  • host_vendor
  • host_os
  • target
  • target_cpu
  • target_vendor
  • target_os
  • CC
  • CFLAGS
  • LDFLAGS
  • CPPFLAGS
  • OBJEXT
  • CPP
  • GREP
  • EGREP
  • GNU_LD
  • CPPOUTFILE
  • OUTFLAG
  • YACC
  • YFLAGS
  • RANLIB
  • AR
  • AS
  • ASFLAGS
  • NM
  • WINDRES
  • DLLWRAP
  • OBJDUMP
  • LN_S
  • SET_MAKE
  • INSTALL_PROGRAM
  • INSTALL_SCRIPT
  • INSTALL_DATA
  • RM
  • CP
  • MAKEDIRS
  • ALLOCA
  • DLDFLAGS
  • ARCH_FLAG
  • STATIC
  • CCDLFLAGS
  • LDSHARED
  • DLEXT
  • DLEXT2
  • LIBEXT
  • LINK_SO
  • LIBPATHFLAG
  • RPATHFLAG
  • LIBPATHENV
  • TRY_LINK
  • STRIP
  • EXTSTATIC
  • setup
  • MINIRUBY
  • PREP
  • RUNRUBY
  • EXTOUT
  • ARCHFILE
  • RDOCTARGET
  • XCFLAGS
  • XLDFLAGS
  • LIBRUBY_LDSHARED
  • LIBRUBY_DLDFLAGS
  • rubyw_install_name
  • RUBYW_INSTALL_NAME
  • LIBRUBY_A
  • LIBRUBY_SO
  • LIBRUBY_ALIASES
  • LIBRUBY
  • LIBRUBYARG
  • LIBRUBYARG_STATIC
  • LIBRUBYARG_SHARED
  • SOLIBS
  • DLDLIBS
  • ENABLE_SHARED
  • MAINLIBS
  • COMMON_LIBS
  • COMMON_MACROS
  • COMMON_HEADERS
  • EXPORT_PREFIX
  • MAKEFILES
  • arch
  • sitearch
  • sitedir
  • configure_args
  • NROFF
  • MANTYPE
  • ruby_version
  • rubylibdir
  • archdir
  • sitelibdir
  • sitearchdir
  • topdir
  • CONFIG.each{|k,v| MAKEFILE_CONFIG[k] = v.dup}

Ruby 1.8.6 and Unicode

Using Ruby 1.8.6 (p111)

In irb

$KCODE='u'
'aébvHögtåwHÅFuG'.scan(/./u)

nicely yields

 ["a", "é", "b", "v", "H", "ö", "g", "t", "å", "w", "H", "Å", "F", "u", "G"]

and

 'aébvHögtåwHÅFuG'.scan(/[\x00-\x7F]/u).join('')

neatly removes all non-ASCII characters.

RubyGems – Stickler

Stickler – Manage gem repositories

http://copiousfreetime.rubyforge.org/stickler

Google Analytics and Ruby

See http://github.com/robmckinnon/rugalytics

Intellij Idea, Ruby and Ruby on Rails

To set up an existing Ruby on Rails project in Intellij Idea 8:

  • Install the Intellij Ruby plugin
    • Preferences > Plugins > Available > Ruby
  • File > New Project
    • Select ‘Create Java Project from existing sources’ [Next]
    • Navigate  to the directory containing the source code [Next]
    • Select ‘Create source directory’. Use the default. (This is one of the vital steps)  [Next]
    • Accept anything on the next screen relating to libraries [Next]
    • Accept anything on the next screen relating to modules [Next]
    • Accept the facets (this doesn’t appear to have any immediate impact) [Finish]
  • File > Project Structure > [Platform Settings] > JDKs
    • In the second panel, click the ‘+’ button to add a new SDK. Select ‘Ruby SDK’
    • Navigate to the Ruby binary you want to use (usually /usr/bin/ruby or /usr/local/bin/ruby)
    • Change the entry name if you want to
    • [Apply], [OK]
  • File > Project > [Project Settings] > Project
    • Set Project SDK to the Ruby SDK you just created
    • [Apply], [OK]
  • Run > Edit Configurations
    • Click the ‘+’ button to add a new configuration. Select ‘Rails’
    • Name your configuration – e.g. ‘mongrel’
    • Select a server – ‘mongrel’
    • Select ‘Choose SDK from module’. Pick the module you’re in the process of configuring
    • [Apply]

You should now be able to debug and run your project using Intellij.

Ruby 1.8.7, Snow Leopard, readline and openssl

Had a lot of trouble getting Ruby 1.8.7 (p160), readline and openssl to combine to let ruby compile on Snow Leopard.

Solution:

readline-6.0
./configure –prefix=/opt/ruby187rubygems135
make
sudo make install

For ruby:

./configure –prefix=/opt/ruby187rubygems135 –disable-pthread –enable-shared –with-readline-dir=/opt/ruby187rubygems135
make
sudo make install

Note that specifying something like ‘-with-openssl=/usr’ breaks the compile. Leaving the SSL option off works.

Ruby 1.8.6 and Rubygems 1.0.1 – “can’t activate xxx, already activated yyy’

Using ruby 1.8.6 and rubygems 1.0.1

If you get the error “can’t activate xxx, already activated yyy”, it can be hard to find the problem.

This version of custom_require.rb has some debug information that prints to the console to make this job easier.

This file updates the file of the same name in the directory

$RUBY_LIB_INSTALL_DIR/site_ruby/1.8/rubygems/custom_require.rb

where $RUBY_LIB_INSTALL_DIR is the base directory where internal Ruby libraries are stored for your particular installation. This is almost always either /usr/lib/ruby or /usr/local/lib/ruby.

In addition, in the following in the file:

$RUBY_LIB_INSTALL_DIR/site_ruby/1.8/rubygems.rb

Add an additional line so the code starting near line 12 looks like this fragment

  module Gem
    class LoadError < ::LoadError
      attr_accessor :name, :version_requirement
    end

    DEBUG_ME = true unless defined?(Gem::DEBUG_ME)
  end

Then, insert a statement similar to this at the start of the method ‘activate’ (near line 300).

puts "rubylib.rubygems activate gem #{gem.inspect} autorequire #{autorequire.inspect} version_requirements #{version_requirements.inspect}" if Gem::DEBUG_ME

WordPress Themes