Archive for March, 2008

Productivity and Rails (3)

• Zero turnaround time – The Rails development environment do not follow the standard development steps of testing a Web application change – configure, compile, deploy, reset and test. Just make the change and see it work. This setup improves productivity and helps you sustain a productive flow without interruption.

• Scaffolding – Rails can spontaneously create a full set of CRUD (Create, Retrieve, Update, and Delete) operations and views on any database table. This scaffolding can make your program go and work with your database tables. You can gradually increase the replacement of the generated CRUD operations and views with a set that would be more efficient for your use.

Controlling iTunes Using RubyOSA

Did you know that RubyOSA provides a bridge from Ruby to the Apple Event Manager? It allows Ruby programs to automate Mac OS X applications in the same way as AppleScript.

Here’s an example code in controlling iTunes:

require ‘rbosa’
itunes = OSA.app(‘iTunes’)

track = itunes.current_track
p track #
p track.name # “Over The Rainbow”
p track.artist # “Keith Jarrett”
p track.duration # 362.368988037109
p track.date_added.to_s # “2006-06-30″
p track.enabled? # true

# Play the selected track.
itunes.play

# Fade the volume.
100.times { |i| itunes.sound_volume = i; sleep 0.1 }

# Set iChat’s status message to the current track.
OSA.app(‘iChat’).status_message = “Playing: #{track.name}”

Come on and try this out! :)

Getting an Application Object in RubyOSA

In the previous post, you can locate an application by passing its name to OSA.app(). However, there are also several other ways.

You can also locate an application by providing its signature (as a four character string), or bundle ID:

OSA.app(:signature => ‘hook’)

OSA.app(:bundle_id => ‘com.apple.iTunes’)

If the application you want to control hasn’t been installed in one of the standard applications locations, RubyOSA may have some trouble locating it. You can provide the full path of the application to work around this:

OSA.app(:path => ‘/Somewhere/Applications/MyApp.app’)

Note that OSA.app(‘iTunes’) is a convenience shortcut to OSA.app(:name => ‘iTunes’).

Getting Started with RubyOSA

RubyOSA is a bridge between Ruby and the Open Scripting Architecture (OSA). The key idea is to create a Ruby API on the fly based on a target application’s scriptable definition.

Once you target an application with RubyOSA you get a special kind of proxy object, which is the entry point of the generated Ruby API.

Let’s start with something easy:

require ‘rbosa’ # (1)
app = OSA.app(‘iTunes’) # (2)
puts app.current_track.name # (3)
First, we need to require the RubyOSA bridge

**Note that if you installed RubyOSA via RubyGems you might want to require ‘rubygems’ before.

Productivity and Rails (2)

• Less code – Rails reduces the need for configuration files and automatically, it can handle a host of lower-level details even without you having to instruct it. Fewer lines of code are needed to implement the application. Fewer code means faster development and lesser bugs, which makes your code easier to understand, keep, and improve.

• Generators – Rails uses runtime reflection and metaprogramming and thus eliminates a lot of the boilerplate code that you would have to create. The remaining boilerplate code can be eliminated by using the built-in generator scripts in creating it. This gives you more time to concentrate on the business logic.

Fun With Unicode

Here’s an interesting post from Oreillynet.com. Did you know you can do this with Ruby out of the box?

# A real lambda
λ { puts ‘Hello’ }.call => ‘Hello’

# Sigma – sum of all elements
∑(1,2,3) => 6

# Square root
√ 49 => 7.0

How difficult was this to implement? Keep reading!

# Be sure to run with the “-Ku” flag!
module Kernel
alias λ proc

def ∑(*args)
sum = 0
args.each{ |e| sum += e }
sum
end

def √(root)
Math.sqrt(root)
end
end

Pretty tricky, eh?

Just remember the “-Ku”. :)

How I love Ruby On Rails Programming!

Copyright ©Basic Ruby On Rails Programming.