Ruby 1.9, TextMate and Segmentation fault errors

While tinkering with Ruby 1.9 in TextMate I found that Ruby was segfaulting on the TextMate Ruby Bundle output screen.

/Users/codethought/Applications/TextMate.app/Contents/SharedSupport/Bundles/Ruby.tmbundle/Support/RubyMate/catch_exception.rb:16: [BUG] Segmentation fault
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]

Turns out you can fix the error yourself… as I found at rubyflow.com

http://www.rubyflow.com/items/6795-solution-to-ruby-1-9-3-segfaulting-when-using-textmate-ruby-b

To quote here:

Edit the catch_exception.rb file in your Ruby bundle in TextMate. To get into it on my machine I had to do:

mate Applications/TextMate.app/Contents/SharedSupport/Bundles/Ruby.tmbundle/Support/RubyMate/catch_exception.rb

Then, comment out the require “cgi” line. And then remove all references to CGI::escapeHTML.

But a simpler way is to go is to download the change that someone else made from github and install it yourself.

https://gist.github.com/1354592

And put the new version catch_exception.rb in the following directory yourself:

Applications/TextMate.app/Contents/SharedSupport/Bundles/Ruby.tmbundle/Support/RubyMate/

And your problem will be resolved.

Seconds, Minutes, Hours – Converting Time Units in Ruby

I’m sure you’ve probably seen this question in your programming classes.. I decided I wanted to play with Ruby again and was remind of this problem, so thought it might be fun as a simple exercise to do this in Ruby.

The problem is to take a long integer and convert it to hours, minutes and seconds. This is a common issue in games, for example, where a countdown timer is represent internally in seconds. But players don’t want to see some huge number like 43678 seconds. They’d rather see 12:07:58 for hours, minutes and seconds. A couple of uses of the mod operator, and a couple of uses of the built-in formatter and you’re good to go.
Update: I wanted to clean up the code a bit more and put the ruby equivalent of a “main” via the __FILE__ check. This way you can just reuse the code via require if you so desire.

#!/usr/bin/env ruby

class TimeFormatter

  def format_time (timeElapsed) 

    @timeElapsed = timeElapsed

    #find the seconds
    seconds = @timeElapsed % 60

    #find the minutes
    minutes = (@timeElapsed / 60) % 60

    #find the hours
    hours = (@timeElapsed/3600)

    #format the time

    return hours.to_s + ":" + format("%02d",minutes.to_s) + ":" + format("%02d",seconds.to_s)
  end
end

if __FILE__ == $0
  formatter = TimeFormatter.new

  puts formatter.format_time(43678)
end

require: command not found in ruby

While I was getting some old Ruby code of mine out to convert ruby code to pretty HTML-formatted code suitable for blogging from Wolfman’s Howlings I got the following error:


$ ./ruby2html.rb 
./ruby2html.rb: line 9: require: command not found
./ruby2html.rb: line 10: require: command not found
./ruby2html.rb: line 11: require: command not found
./ruby2html.rb: line 14: syntax error near unexpected token `('
./ruby2html.rb: line 14: `    code= File.read(ARGV[0])'

Never saw this problem before, but obviously it was frustrating.

What had happened was I hadn’t deleted the tab spaces before the #!/usr/bin/env ruby line.

Apparently if there is any white space before the #!/usr/bin/env ruby the script will not run.

Swell.. 😐