This post originated from an RSS feed registered with Ruby Buzz
by Guy Naor.
Original Post: Cool Things With Ruby
Feed Title: Famundo - The Dev Blog
Feed URL: http://devblog.famundo.com/xml/rss/feed.xml
Feed Description: A blog describing the development and related technologies involved in creating famundo.com - a family management sytem written using Ruby On Rails and postgres
After getting over the learning curve (or we can call it the "suck" period) Ruby suddenly get you into the "WOW!" phase.
So to show some cool stuff you can do with Ruby that really has some wow factor, I collected a few small and pretty simple examples of code I'm using.
Ruby + Regular Expressions
Like Perl before it, Ruby has a very well integrated regular expression engine. And in addition it has code blocks that make some things just amazingly simple.
In this example I had to parse a manually created sql file containing all the commands needed to create a database. I prefer to have it as a regular sql file so that I can run it through the psql processor when needed. It's also easier to read and understand this way. But the file is full of comments, white space and commands, and we need to clean that up so that we get a command by command we can execute. Each command can span multiple lines and always ends with a ;
Read the file, clean it and send it to the SQL executor:
File.open("#{RAILS_ROOT}db/family_files.sql") do |f|
# Remove anything followinf -- (sql remark) and
# replace all multiple whitespace with a single space
# Then scan for commands by looking for the ending ;
f.read.gsub(/(--.*?\n\s*?)|(\s+)/m," ").scan(/\s+(.+?);/) do |res|
execute_sql res[0], sql_params
end
end
The file also includes some replaceable parameters as needed by psql - those are of the format :param_name. And we need to replace then with the sql_params from the code above. As in psql, we replace those parameters only if they are passed in the sql_params hash. If they are not in the hash we leave them as is.
Parse the SQL Command and Replace the Params
def execute_sql( sql, params )
# Replace palceholders with params.
# We search for all items in the form :fm_space, :fm_location, etc...
# And replace them with items in our params hash if they exist
if params && !params.empty?
while sql =~ /.+((:)(\w+?))(\s+.*|$)/
val = params[$3.intern]
sql.gsub!($1, val) if val
end
end
# Execute the SQL command
ActiveRecord::Base.connection.execute sql
end
method_unknown()
method_unknown() is a big part of meta-programming in ruby as it allow an object to appear as having all sort of methods and attributes it doesn't really have. Rails does extensive use of method_unknown() for everything from reading model attributes to implementing all kind of find methods.
We have a model we divided into two to increase security. Some parts are writable by the users, and some only by the system. We use has_one for that, but now the model isn't as clean as before, as we have to access some of the attribute through the has_one model. To make it look as a seamless object, we used method_unknown() to fake the child object methods/attributes in the parent object.
Code to Access Child Attributes in the Parent
class Child
attr_accessor :attr1, :attr2
end
class Parent
attr_accessor :attr3, :attr2
def initialize
@child = Child.new
end
def method_missing(method_id, *args, &block)
begin
@child.send method_id, *args, &block
rescue Exception
super
end
end
end
irb> p = Parent.new
irb> p.attr1 = 5
irb> p.attr1 #=> 5
irb> p.attr8 # Throws NoMethodError