RubyMine Public Preview

Posted by John
on Wednesday, 19 November 2008

RubyMine

From the makers of Resharper, a brand new Ruby IDE for Windows / Mac / Linux.

...might finally be a reason to switch off TextMate ;-)

Installing on Linux is a little difficult but do-able, first off download the Linux build and extract it.

Next up you'll need to add an environment variable into your .bashrc file,

gedit .bashrc

Then add this to the end of the file, with the path of your JAVA JDK location.

export JDK_HOME=/usr/lib/jvm/java-6-openjdk/

Save it and reload Terminal so it's got the new variable, then in Terminal navigate into the bin directory of RubyMine you just downloaded,

cd ~/Desktop/rubymine472/bin

Then remove the last line of rubymine.vmoptions

gedit rubymine.vmoptions

  remove this line => -agentlib:yjpagent=disablej2ee,sessionname=rubymine

Once done you should be able to start up RubyMine;

~/Desktop/rubymine472/bin/rubymine.sh

Then you can add it to your .bashrc file so you can fire it up any time you want with an alias,

alias rubymine="~/Desktop/rubymine472/bin/rubymine.sh"

And then

rubymine

Textile & Markdown in Ruby

Posted by John
on Thursday, 13 November 2008
Textile

Simple to implement with the redcloth gem

sudo gem install RedCloth

Usage,

require 'redcloth'
s = RedCloth.new "p. format this with textile"
puts s
Markdown

With markdown you've got three implementations to try,

  • bluecloth gem (last maintained 2005, slow)
  • rdiscount gem (better c implementation, fast!)
  • maruku gem (pure ruby implementation)

To implement with BlueCloth

sudo gem install bluecloth

Then,

require 'bluecloth'
s = BlueCloth::new("# format this with markdown")
puts s

Or RDiscount

sudo gem install rdiscount

Then,

require 'rdiscount'
s = RDiscount.new("# format this with markdown")
puts s

Ruby date_select conversion

Posted by John
on Wednesday, 19 March 2008

Had a bit of a pain with Ruby's handling of Date/Time values coming from a Date_Select form element, managed to solve it though.

The problem was that in my form...

<%=date_select(:date,'',:start_year => 1950,:include_blank => false, :default => { :year => '1970' })%>

...the value I got back from the submitted field,

params[:date]

became...

'(3i)4(1i)1953(2i)5'

Bit of a mess, obviously I was expecting something a little more legible like 1953-5-4 (yyyy-mm-dd), so I bashed my head against a brick wall trying to work it out, I could have made my life easier with some select_date blocks but I'm a guy how likes to ask 'why' so I perceviered.

Turns out in my travels you can do this to break it into a usable DateTime object..

Time.mktime(params[:date]['(1i)'],params[:date]['(2i)'],params[:date]['(3i)'])

Which will return something much more legible...

Sun May 19 00:00:00 +0100 1968

Or whip it into a method to do exactly what you want...

def convert_date(obj)
  return "#{obj['(1i)']}-#{obj['(2i)']}-#{obj['(2i)']}"
end

<%=convert_date(params[:date])%>

But wait, it doesn't end there, you could use Date.new or Date.civil to parse it into a real date object like...

def convert_date(obj)
  return Date.new(obj['(1i)'].to_i,obj['(2i)'].to_i,obj['(3i)'].to_i)    
end

Which will return...

1967-03-19

Much nicer, just remember to use .to_i otherwise you'll get...

comparison of String with 0 failed

Enjoy!

Sending mail with Rails

Posted by John
on Monday, 05 November 2007

Finally managed to work this out, what follows is something that does 100% work!

Install your Mail Client

Choose which one you're gonna use, SendMail or the more powerful Postfix;

sudo aptitude install sendmail
..or
sudo aptitude install postfix

...however, for this example I'm going to be using the easier Sendmail.

Create your Mailer Model

First things first, your gonna need a mailer model, so to create one go to the root of your rails app and type...

script/generate mailer UserMailer

This will generate a couple of new directories but more importantly thru the magic of rails by using 'mailer' it will know that your creating a model that will use the ActionMailer library and so things will be handled differently with this one than other models.

Setup Rails to use SendMail

Now we could use SMTP to send emails from our web app, but to be perfectly honest that's like a major headache for the newbie to setup. So as long as your running this on a linux box you can use the more simpler SendMail libaries to send email.

To use these in production, edit your production.rb environment file...

myapp/config/environments/production.rb

and add these lines...

config.action_mailer.raise_delivery_errors = true

ActionMailer::Base.delivery_method = :sendmail

ActionMailer::Base.sendmail_settings = {
:location       => '/usr/sbin/sendmail',
:arguments      => '-i -t'
}

What the first line will do is notify you of any delivery errors, useful in testing, you can turn this off in production by setting it to false.

  • :location tells the actionmailer where to find the sendmail libraries, default location.

  • :arguments tell sendmail to send the email immediately, rather than wait, so your email does get sent.

That's that done, remember you could use SMTP but it can be a nightmare to setup installing postfix, setting your MX records etc. this however is simple & quick.

Back to our UserMailer model

Now that's all sorted, open your UserMailer model...

myapp/app/models/user_mailer.rb

And add...

def test
recipients  "user@destination.com"
from        "admin@source.com"
subject     "Thank you for Registering"
body        "test email"
end

What we've done is created a method that will email user@destination.com when it get's called.

Which can be done by using...

User_Mailer.deliver_test

...anywhere in our app's code.

Notice the deliver_ prefix in front of our method test. This is how we associate our 'magic' ActionMailer methods associated when we generated the model.

Further Reading

You can optionally expand this with the examples below, but hopefully the above has helped you get over one of the major headaches of Ruby on Rails.

IronRuby (aka Ruby for .NET)

Posted by John
on Tuesday, 02 October 2007

With all the talk about this hot new language it seems only right that Microsoft has moved their focus over to the opensource arena with the impending release of IronRuby.

What this is in a nutshell is a plugin to Visual Studio 2005 that allows users to develop Ruby Apps within the VS.NET Dynamic Language Runtime.

They did this a while back with the release of IronPython (Python on .NET), but this is one developer who really hopes the impending Orcas release of Visual Studio has this beauty firmly tied in.

Fingers crossed,