Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Tuesday, January 18, 2011

Netbeans Won't Debug Rails Project

If the Netbeans says it "can't contact the web server" when running "Debug Project" there is likely an issue with the debug gems that the IDE installs itself. To correct this problem:
  1. Exit Netbeans
  2. $ gem uninstall linecache
  3. $ gem uninstall ruby-debug-base
  4. $ gem uninstall ruby-debug-ide
  5. $ gem install ruby-debug-ide
  6. Start Netbeans, and "Debug Project"

Please note that I am using MRI (native Ruby), not JRuby.
  • Netbeans 6.9.1 (Linux)
  • ruby 1.8.7 (2010-12-23 patchlevel 330) [x86_64-linux]
  • RubyGems 1.4.1
  • linecache (0.43)
  • ruby-debug-base (0.10.4)
  • ruby-debug-ide (0.4.16)

Friday, February 05, 2010

Ruby Multithreading with a Thread Size Limit

I wrote this class in the process of writing a multithreaded Amazon S3 key migrator. S3 doesn't like that, so don't even bother.

You will want to use this with a thread-safe process block.

Tuesday, January 19, 2010

My .irbrc Ruby irb and Rails console configuration

Here is my customized configuration file for Ruby irb and Rails console. You will need to install the wirble and hirb gems for full functionality.

Friday, December 05, 2008

Error Updating RubyGems

If you get the following error:
undefined local variable or method `remote_gemspecs’

Then run:
sudo gem install rubygems-update
sudo gem update

Set JAVA_HOME on Mac OSX

If you want to install the rjb Ruby Gem, you will need to set the JAVA_HOME environment variable.

Add the following line to ~/.login or /etc/profile and restart your terminal.

setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home

Tuesday, February 26, 2008

Advanced puts Syntax in Ruby

The "puts" command can be used with sprintf syntax using the percent (%) sign as follows:

puts "ABC %2.2f" % 3.567
=> "ABC 3.57
"

puts "ABC %2.2f DEF %5s" % [3.567, "abcd"]
=>
"ABC 3.57 DEF abcd"



Thursday, December 13, 2007

Getting Rails 2.0 Running

So, Rails 2.0 has finally been released. It was a bit of a pain getting all my Ruby Gems updated for some reason. Please keep in mind that the MySQL path is specific to OSX 10.5.

$ sudo gem update sources
$ sudo gem update --system

$ sudo gem uninstall mysql
$ sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql
$ sudo gem update -- --with-mysql-dir=/usr/local/mysql
$ sudo gem install rails --include-dependencies

I know there is a bit of duplicated effort there, but I had issues with that MySQL gem.

Finally Rails 2.0 joy. Now to fiddle around with it before starting a new project...

Installing the MySQL Ruby Gem for Rails MySQL Support on OSX Leopard

Some special command line magic is required to install the MySQL Gem on OSX Leopard (10.5).

$ export ARCHFLAGS="-arch i386"
$ sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql


I got this information from http://trac.macosforge.org/projects/ruby/wiki/Troubleshooting.

Wednesday, November 21, 2007

Rails SQL Optimization

In order to automate the optimization of SQL queries in Ruby on Rails, I wrote this script to parse the output of the logs. It finds all the unique SQL queries made by different controllers, and runs an SQL EXPLAIN for each. This is really good for finding missing indexes and large table scans.

I have only tested this on Mac/MySQL, so your mileage may vary (it uses *NIX file paths). This script has saved me MUCH time. Of course, when optimizing queries, don't go overboard on eager loading. ActiveRecord eager loading can be slow if you use multiple nested includes.

Just run this script within your Rails application root directory, and it will create a file named explained.html. You may set the RAILS_ENV environment variable to specify production mode. The script automatically loads your database configuration from log/database.yml

Oh, and there needs to be at least 2 pageloads in your log. I haven't bothered fixing this bug.

So here is the script.

UPDATE: I came across some good tips for additional optimization here.

Thursday, March 22, 2007

A Server Monitor

I can't believe that I haven't been doing proper server monitoring. Well, that had to come to an end. I didn't like any of the monitoring apps out there and I did as a good code primate should and wrote my own. Here is my own server monitor and module written in and for Ruby. It outputs in XML format.

You write a simple XML configuration file, and call the app via the commandline specifying where the config, output, and XSL template are (cross-domain issues with XSL are fun!).

What are you waiting for?

http://code.google.com/p/rubyservermonitor/


Friday, February 09, 2007

Ruby HTTPS with HTTP Basic Auth

Fetching HTTPS data with Ruby using HTTP Basic Authorization

require 'net/http'
require 'net/https'

def get_feed(server,path,username,password)
http = Net::HTTP.new(server,443)
req = Net::HTTP::Get.new(path)
http.use_ssl = true
req.basic_auth username, password
response = http.request(req)
return response.body
end

Monday, January 22, 2007

Strip HTML Tags

It's trivial really...but regexp's are a bitch.

#!/usr/bin/ruby -n
puts $_.gsub(/<\/?[^>]*>/, "")

Thursday, January 18, 2007

Ruby Cheat Sheets

I found a handy ruby gem that makes reference very easy. gem install cheat to install the gem, then run cheat sheets to get a list of available cheat sheets. Run cheat cheat_name to display useful info.

Wednesday, January 03, 2007

Rails Development Using Vim

Rails developers fortunate enough to run OSX have raved to no end about TextMate, but what do the rest of us, being *Nix and Windows users, have available? RadRails is an EXCELLENT IDE, and I have used it almost exclusively for nearly a year now, but as an editor, like all software, it has severe limitations. It's a bit slow and too much of a memory hog for my taste, and not nearly as easy to extend as I would like.

In my experience as a Linux administrator and desktop user, I have learned to appreciate, and now love the Vi editor. Well, more accurately Vim. If you are using a GUI, Cream is an excellent set of customizations for GVim that make Vim more accessible to the new user.

To improve the state of affairs even further, there are various Ruby and Rails specific extensions and customizations that bring Vim to the point of being nearly as good, and better in some ways, than TextMate or RadRails. We're talking autocompletion, autoindention, syntax highlighting, abbreviations, and much much more. See this post (so useful to me that I mirrored it here) for how to tweak Vim for Rails development.


Friday, December 01, 2006

RubyGems Error

If you get this error when trying to install a gem:
/usr/local/lib/ruby/1.8/yaml.rb:133: [BUG] Segmentation fault

Run "gem environment" to find the "gem path" (i.e. /usr/local/lib/ruby/gems/1.8 ) and delete the "source_cache" file located in that directory.


Thursday, October 26, 2006

Cannot Install any Ruby Gems

Problem: Error trying to install ANY gem.
Error Message:
ERROR: While executing gem … (Gem::GemNotFoundException)
Could not find rubyzip (> 0) in the repository
Solution: Delete source_cache file (run gem environment to find the Gems install location)

Wednesday, December 28, 2005

Ruby on Rails

...but still here.

My latest interest is Ruby on Rails. From a complete Ruby on Rails n00b to a custom, from scratch, multiple bank account balancing web app in less than 9 hours. Yeah, thats 9 hours INCLUDING reading, learning, debugging, etc. WOW!

The framework seems to be worth the hype.