I’m not going to explain what MacRuby is but here’s a short description from the projects website:

MacRuby is a version of Ruby 1.9, ported to run directly on top of Mac OS X core technologies such as the Objective-C common runtime and garbage collector, and the CoreFoundation framework. While still a work in progress, it is the goal of MacRuby to enable the creation of full-fledged Mac OS X applications which do not sacrifice performance in order to enjoy the benefits of using Ruby.

So, to get started you obviously need to install MacRuby, but fear not, the official website provides a graphical installer so it’s just a point-n-click operation.
In order for MacRuby not to interfere with previously installed ruby versions it prefixes all command-line executables with mac* (eg, macruby, macgem, macrake and so on) so you’ll want to keep that in mind.

Hello World

MacRuby comes bundled with HotCocoa, a idiomatic ruby interface to cocoa. Using HotCocoa makes it easy to write to write cocoa apps in your favorite text-editor so you don’t have to resort to XCode and Interface Builder.
But enough talking, it’s time for the mandatory “Hello World” app :)

In your text-editor of choice create a file called hello_world.rb and edit it to look like this:

require "hotcocoa"
include HotCocoa

application :name => "Hello World" do |app|
window :title => "Hello World", :frame => [500, 500, 300, 100] do |win|
win << button(:title => "Click Me!").on_action { NSLog "Hello World" }
win.will_close { exit }
end
end

And that’s it, now save the file and run it from the command-line like this:

$ macruby hello_world.rb

Try clicking the button and you should see something like this in the console:

2009-07-31 23:58:11.752 macruby[28261:10b] Hello World

Well, that’s nice and all, but I want something more

Once you start building anything serious, a single file app isn’t really viable. Fortunally though, HotCocoa comes with a generator:

$ hotcocoa my_app

This will create a directory structure for you, with some rake tasks and a skeleton app. You can try it out like this:

$ cd my_app
$ macrake

Where to go from here?

Both MacRuby and HotCocoa are pretty young and so documentation is sparse, there are however a couple of really good examples under: /Developer/Examples/Ruby/MacRuby/ and /Developer/Examples/Ruby/MacRuby/HotCocoa

Posted in Programming, Ruby at July 31st, 2009. No Comments
Tagged with , . Written by: Patrik Hedman

As part of getting my SuppressValidations gem to load automatically when launching
script/console I took some time to configure my .irbrc file a bit and I thought I’d share it.
So here it is in all it’s glory:


require 'rubygems'
require 'irb/completion'
require 'irb/ext/save-history'

# Tell irb how much it should remember and where
# to save it's history
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"

# Simple prompt
IRB.conf[:PROMPT_MODE] = :SIMPLE

# Auto indent code
IRB.conf[:AUTO_INDENT] = true

# This is only done when using the Rails console
if rails_env = ENV['RAILS_ENV']

# Setup a Rails specific prompt
rails_root = File.basename(Dir.pwd)
IRB.conf[:PROMPT] ||= {}
IRB.conf[:PROMPT][:RAILS] = {
:P ROMPT_I => "#{rails_root}> ",
:P ROMPT_S => "#{rails_root}* ",
:P ROMPT_C => "#{rails_root}? ",
:RETURN => "=> %s\n"
}
IRB.conf[:PROMPT_MODE] = :RAILS

# This is only done when the irb session and
# rails are fully loaded (from Mike Clark)
IRB.conf[:IRB_RC] = Proc.new do

# Log ActiveRecord calls to standard out
ActiveRecord::Base.logger = Logger.new(STDOUT)

# Setup a [] alias for find so you can say:
# Post[1] instead of Post.find(1)
ActiveRecord::Base.instance_eval { alias :[] :find }

# Require and mixin the SuppressValidations gem
# (gem install polly-suppress_validations)
require "suppress_validations"
include SuppressValidations
end
end

And here’s a sample session:

$ script/console
Loading development environment (Rails 2.3.2)
test> User[1]
User Load (0.2ms) SELECT * FROM "users" WHERE ("users"."id" = 1)
=> #
test> User[1].save
User Load (0.4ms) SELECT * FROM "users" WHERE ("users"."id" = 1)
User Exists (0.2ms) SELECT "users".id FROM "users" WHERE ("users"."username" = 'foobar' AND "users".id <> 1) LIMIT 1
User Exists (0.1ms) SELECT "users".id FROM "users" WHERE ("users"."email" = 'foo@bar.baz' AND "users".id <> 1) LIMIT 1
=> true
test> def some_method
puts "Hello from some method!"
end
=> nil
test> some_method
Hello from some method!
=> nil

Posted in Rails, Ruby at July 31st, 2009. 2 Comments
Tagged with , , . Written by: Patrik Hedman

In my last post I showed how you can install my suppress_validations gem on a per-project basis but that’s a bit cumbersome. Wouldn’t it be nicer if it always was available in the console on every project?
I think it would, so here’s how you can accomplish just that:

Edit your ~/.irbrc (or create it if you don’t already have one):

require "rubygems"

if ENV['RAILS_ENV']
IRB.conf[:IRB_RC] = Proc.new do
require "suppress_validations"
include SuppressValidations
end
end

And thats it, you can now fire up script/console and start suppressing validations

$ script/console
Loading development environment (Rails 2.3.2)
>> post = Post.new
=> #
>> post.save
=> false
>> suppress_validations { post.save }
=> true
>> Post.last
=> #

Posted in Rails, Ruby, gems at July 28th, 2009. No Comments
Tagged with , , , . Written by: Patrik Hedman

Validations on ActiveRecord models can really get in the way while trying things out in the console sometimes so the other day when I stumbled over this railscast episode: http://railscasts.com/episodes/62-hacking-activerecord I thought, hey, with some modification that could easily be turned into a plugin.

Install it with:


$ gem install polly-suppress_validations

then add this to your config/environment.rb file:


config.gem "polly-suppress_validations", :source => "http://gems.github.com", :lib => "suppress_validations"

and then unpack it with:


$ rake gems:unpack

or just install it as a plugin:

$ script/plugin install git://github.com/polly/suppress_validations.git

Now say we have a model like this:

class Post < ActiveRecord::Base
validates_presence_of :title, :body
end

Now lets enter script/console and include the SuppressValidations module

$ script/console
Loading development environment (Rails 2.3.2)
>> include SuppressValidations
=> Object

However, trying to create a new post without the required attributes still fails as this example shows:

>> post = Post.new
=> #
>> post.save
=> false

To suppress validations just wrap the call to post.save in a suppress_validations block, like so:

>> suppress_validations { post.save }
=> true
>> Post.last
=> #

Posted in Projects, Rails, Ruby, gems at July 20th, 2009. No Comments
Tagged with , , . Written by: Patrik Hedman

pastbedti.me is using WP-Gravatar