Wednesday, January 30, 2008

What happened to nbrubyide daily build?

What happened to daily build of nbrubyide? I have been checking the last few days and there is no new build. I am so used to getting my morning nbrubyide fix. This is not a complain. I am just wondering.

Here is where I go to download the latest build.
http://deadlock.netbeans.org/hudson/job/ruby/changes

Thursday, January 17, 2008

Converts a comma separated string of numbers into an array of integers

This is all there is to convert a string of comma separated integers to an array of integers.

>> "1,24,53,6,23,6,5".split(",").collect{ |s| s.to_i }
=> [1, 24, 53, 6, 23, 6, 5]

This is why Ruby is so fun to use. Is there a shorter way to do this than what I have?

Tuesday, January 08, 2008

Expose Module methods as Instance and Class methods

Have you ever wondered how to include a Module and has its methods be available as instance methods and class methods? I certainly did and after doing a little research I figured out a way to do this in Ruby. For the experience Ruby programmers out there, this is probably very obvious but for those that haven't completely figured out Class object and instance of a class then this should help a little. Or totally confused you.

I don't know whether it is wise to do this but at least it is possible to do. Anyway, enough talking. Here is the code in all it (not so) glory.

module Methods
def self.included(base)
# add methods in ClassMethods into the meta class
base.extend(ClassMethods)
end

def hi
# accessing the meta class to call the
# real method
class << self
self.hi
end
end

def bye
class << self
self.bye
end
end

module ClassMethods
def hi
puts "Hello"
end

def bye
puts "Good Bye"
end
end
end

class Greeting
include Methods
end

# Calling Instance methods here
Greeting.new().hi
Greeting.new().bye

# calling Class methods here
Greeting.hi
Greeting.bye

Saturday, January 05, 2008

Encryption Data in Ruby using OpenSSL

I had been searching the web for a way to encrypt some data for my Rails project for a while and I finally found something that I could use. Encrypting Sensitive Data with Ruby (on Rails) has straight forward instructions on how to encrypt and decrypt data using OpenSSL in Ruby.

Friday, January 04, 2008

adding special helpers

I was cleaning up my view and decided that I want to add two helpers, display_if and display_unless to help clean up the view.

Instead of doing:

<% if @login %>
Hello <%= @user_name -%>.
<% end %>

It would be like this:

<% display_if @login %>
Hello <%= @user_name -%>.
<% end %>

I came from the Asp.Net world so writing a custom control to do this is not easy. I was expecting it to be difficult in Rails also but it turned out to be very simple.

It took me less than 10 minutes to figure it out from not knowing how to do it at all. Hopefully, I am doing it correctly.
Here is the code for the two helpers. Just put them in your application_helper.rb file.

def display_if(condition, &block)
raise ArgumentError, "Missing block" unless block_given?

if condition
output = capture(&block)
concat(output, block.binding)
end
end
def display_unless(condition, &block)
raise ArgumentError, "Missing block" unless block_given?

unless condition
output = capture(&block)
concat(output, block.binding)
end
end