Sunday, October 29, 2006

ruby and YAML

this tutorial walk through processing yaml files with ruby, I will not go explain why, which, or what is yaml4r but I will explain how to load, save, update yaml file using ruby (Yaml4r).

first of all you need to include yaml.rb in your class by adding this: require 'yaml.rb'

In the following we add an array object to yaml file

Step one: loading object from yaml file.
obj = YAML::load(File.open('test.yaml'))

Step two: modifying the object (which is an array) and save it back to the file.
obj << 'anything' File.open('test.yaml', 'w+') { |out| YAML::dump(obj, out) }



A complete code that do so would look like the following: require 'yaml.rb'

obj = YAML::load(File.open('test.yaml'))

if obj
obj << 'anything'
else
obj = ['anything']
end

File.open('test.yaml', 'w+') { |out|
YAML::dump(obj, out)
}

Saturday, October 28, 2006

rails text search

This tutorial shows you how to search your data at your Rails application. we will use an easy, and flixible plugin called ferret.

ready? follow up:
  • install ferret gem:
gem install ferret
  • install ferret plugin:
script/plugin install svn://projects.jkraemer.net/acts_as_ferret/tags/stable/acts_as_ferret
  • at your model add the following line:
act_as_ferret or customize your search to only some fields like this: act_as_ferret :fields => [:name, :firstname, :surename]
  • now your database fields are searchable.
to search your data type: .find_by_contents("osama OR dwairi") and walk through them as array