Friday, December 7, 2007

Ruby Classes



1. Classes are themselves represented as objects.
The first thing you’ll notice when you begin to learn ruby, is that all things are objects. Ruby is a dynamic 100% object oriented language (along with lots of others, as I seemed to have forgoten). This really opens the doors to a new type of programming, called meta-programming. Meta-programming allows you to open up classes and objects as the application is running and modify them in any way needed. Even classes are objects, and thus have their own set of methods available to them. Each time you create a class you’re actually creating an object of class Class. In the code below you’ll notice that Dog has lots of methods, but it doesn’t have a method named class. Dog can call the method “class” because it is an object of type class. This gives it access to all of ruby’s class object methods. A list of available methods for class Object can be found at Ruby Central. A more updated list can be found in the Programming Ruby 2nd Edition book. If you plan doing anything beyond a recipe, or depot app, you’ll need this book!
class Dog
## lots of Dog methods that aren't def class
end

p Dog.class # => Class

2. Methods can be defined on a per-object basis
Methods can also be created for a specific instance of an object. These methods are only available to the the object named in the method call.
  dog = Object.new

def dog.bark
puts "ruff ruff"
end

dog.bark # => ruff ruff

3. You can also define per-object methods using class << dog
You can also create objects by using the Object.new method. This now makes dog a certified object, and thus you are free to add methods to this object.
  dog = Object.new

class << dog
def bark
puts "ruff ruff"
end
end
dog.bark # => ruff ruff

4. outside of methods, within a class … end block, self refers to the class
Self always references the current object. Inside of any class, self will be treated just the same as Dog (if you’re in class Dog).
  class Dog
p self # => Dog
end

5. “class methods” are simply per-object methods defined on classes
In ruby, you are able to tie a class to a particular object. Remember from tip 4 that you can replace self with Dog, and nothing about the code would change. This use of self defines a class that is based around the object self. Here, self is the class object Dog. This now allows you to add methods to class Dog. These new methods are class methods, and will be available to Dog. This means that you don’t have to create an instance of this object to call this method.
  class Dog
class << self
def bark
puts "ruff ruff"
end
end
end
Dog.bark # => ruff ruff

As you can see, ruby leaves the door wide open for you to modify all classes while your app is running.

(see more here: http://www.recentrambles.com)

Monday, February 19, 2007

Create and load XML in PHP

This is a tutorial for creating a new XML file from PHP and loading the XML file.
This tutorial has two files generatexml.php and loadfeeds.php

Requirements:
1. PHP5
2. Apache
The XML files are generated in PHP with the help of DOM. The following are the PHP codes

Creating an XML :
Create a PHP file with the name generatexml.php and copy and paste the code in it

$doom = new DOMDocument("1.0");

//create the root node of the XML file
$root = $dom->appendChild($dom->createElement("demo"));

// this is used to create the FRONTEND node
$frontend = $root->appendChild($dom->createElement("frontend"));
//chlids of frontend node
//first node
$flash = $frontend->appendChild($dom->createElement("app"));
$flash->appendChild($dom->createTextNode("Flash"));
//second node
$ajax = $frontend->appendChild($dom->createElement("app"));
$ajax->appendChild($dom->createTextNode("Ajax"));

// this is used to create the SERVERSIDE node
$serverside = $root->appendChild($dom->createElement("serverside"));
//chlids of frontend node
//first node
$php = $serverside->appendChild($dom->createElement("app"));
$php->appendChild($dom->createTextNode("PHP"));
$php->setAttribute("type","opensource");
//second node
$rails = $serverside->appendChild($dom->createElement("app"));
$rails->appendChild($dom->createTextNode("Ruby on Rails"));

// this is used to create the BACKEND node
$backend = $root->appendChild($dom->createElement("backend"));
//chlids of frontend node
//first node
$mysql = $backend->appendChild($dom->createElement("app"));
$mysql->appendChild($dom->createTextNode("MySQL"));
$mysql->setAttribute("type","opensource");
//second node
$postgres = $backend->appendChild($dom->createElement("app"));
$postgres->appendChild($dom->createTextNode("PostgreSQL"));

//save the XML file created
$dom->formatOutput = true;
$dom -> save("demo.xml");

Loading an XML :
Create a PHP file with the name loadfeeds.php and copy and paste the code in it

$dom = new DOMDocument("1.0");
header("Content-Type: text/xml");
$dom ->load( 'demo.xml' );
echo $dom ->saveXML();

First execute generatexml.php to create a xml file "demo.xml" in the current directory.
Then execute loadfeeds.php to load the xml file

Tuesday, February 13, 2007

Ruby On Rails (ROR)

Ruby on Rails is an open source web application framework written in Ruby that closely follows the Model View Controller (MVC) architecture. It strives for simplicity, allowing real-world applications to be developed in less code than other frameworks and with a minimum of configuration. One of Rails' guiding principles is "Don't Repeat Yourself".

Thursday, January 4, 2007

welcome

This is the welcome post