Three new methods were added to DateTime class. The
methods are #utc, #utc? and #utc_offset. Look an example for each one:
date = DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24))
#=> Mon, 21 Feb 2005 10:11:12 -0600
date.utc
#=> Mon, 21 Feb 2005 16:11:12 +0000
>> DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc?
#=> false
>> DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc?
#=> true
>> DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc_offset
#=> -21600
Monday, November 3, 2008
New Ruby Web Application Framework: Merb
Merb is a micro-framework (developed by Ezra Zygmuntowicz) that ties in with Mongrel and erb and provides basic controller and view templating. It's an ideal way to put together quick and simple Web applications with Ruby that don't rely on any of the fancier features offered by Rails. It does have support for ActiveRecord, however. Merb allows you to create small systems that produce dynamic requests and can interact with databases but without the significant weight of the Rails framework. Where Ruby on Rails is a Big Mac, Merb is a McNugget.
Friday, July 4, 2008
Block Helper Methods
You may have used helper methods before to streamline the code in your views, and you may have even used block helpers, to further improve your refactoring, but probably not block helpers with their own methods. This technique is how text_field :product, :name became f.text_field :name.
You can use these blocks like this in your views:
navigation('title') do n
= n.option('Services', services_path)
= n.option('About Us', about_us_path)
end
Monday, April 14, 2008
About YAML
What It Is YAML?
YAML is a human friendly data serialization standard for all programming languages.
Using YAML for configuration:
YAML-formatted fixtures are a very human-friendly way to describe your sample data. These types of fixtures have the .yml file extension (as in users.yml).
Loading the YAML file:
Loading the YAML is extremely simple. Here is the example given below
config:
name: Mohan Kumar
email: email@company.com
First we require the YAML library:
require 'yaml'
Ok, now we can make a read_config method:
def do_config
config = YAML.load_file("config.yaml")
@name= config["config"]["name"]
@email = config["config"]["email"]
end
more efficient way to do this would be to loop through the hash that is created by the do_config method and just set the key to an instance variable.
YAML is a human friendly data serialization standard for all programming languages.
Using YAML for configuration:
YAML-formatted fixtures are a very human-friendly way to describe your sample data. These types of fixtures have the .yml file extension (as in users.yml).
Loading the YAML file:
Loading the YAML is extremely simple. Here is the example given below
config:
name: Mohan Kumar
email: email@company.com
First we require the YAML library:
require 'yaml'
Ok, now we can make a read_config method:
def do_config
config = YAML.load_file("config.yaml")
@name= config["config"]["name"]
@email = config["config"]["email"]
end
more efficient way to do this would be to loop through the hash that is created by the do_config method and just set the key to an instance variable.
Array.rassoc
Very usefull function. Recently i used in my project. This function will search array whose elements are also arrays.(i.e) It compares key with the second element of each contained array using == and returns the first contained array that matches.
a = [ [ "ID", "id"], ["NAME", "name"], ["LOCATION", "location"], ["CITY", "city"] ]
a.rassoc("name") #=> ["NAME", "name"]
a.rassoc("four") #=> nil
a = [ [ "ID", "id"], ["NAME", "name"], ["LOCATION", "location"], ["CITY", "city"] ]
a.rassoc("name") #=> ["NAME", "name"]
a.rassoc("four") #=> nil
Saturday, February 2, 2008
Ruby Recursion
Definition provided by Wikipedia:
"In mathematics and computer science, recursion is a particular way of specifying (or constructing) a class of objects (or an object from a certain class) with the help of a reference to other objects of the class: a recursive definition defines objects in terms of the already defined objects of the class. "
"In mathematics and computer science, recursion is a particular way of specifying (or constructing) a class of objects (or an object from a certain class) with the help of a reference to other objects of the class: a recursive definition defines objects in terms of the already defined objects of the class. "
Read more about general recursion concepts here on Wikipedia
One of the classic examples that are taught to people that want to learn how to program is the factorial (when they need to learn recursion).
For example:
n! (notation for factorial) = 1*2*3...*n
5!=1*2*3*4*5
How would you do that in Ruby? Like this:
def factorial(n)
if n == 0
1
else
n * factorial(n-1)
end
end
end
You might notice a strange thing at first, that the "factorial" calls itself later in the code : n * factorial(n-1)
This is a recursive call.The whole idea behind the few lines above is the fact that the formula for finding out n! is actually calculated based on (n-1)! like this: n!=n*(n-1)!
A formula like this would allow us to solve the problem recursively, by calculating every factorial under n!
A formula like this would allow us to solve the problem recursively, by calculating every factorial under n!
Now, if you want to find out how much 3! is, you use the factorial you just defined above:
factorial(3)
factorial(3)

Here's another way of solving a factorial with methods(discussed in more detail later):
class Integer
def factorial
if self == 0
1
else
self * (self - 1).factorial
end
end
end
You find out how much 3! is by calling the method:
3.factorial
Ruby Statements
Statements Generalized, a Ruby statement is each and every instruction you write.
For example, this:
var= "I am a ruby developer"
or this:
if (var!=1)
var1="wrong" are statements of some sort.
A statement is more of a general programming term not necessarily with a specific format in Ruby.It is true that,unlike C++ for example, you don't need to end each line with a ; sign or the fact that if..else or other conditional instructions might be written in a slightly different way that in other programming languages.
In a more restrictive meaning ("by the book," so to speak), a statement is an instruction that does not return a value. I've met a lot of people that no longer make this distinction though; probably for the wrong reasons. If you were to consider this definition, the second code example I gave would no longer represent a statement, but a larger piece of code.
There isn't much to say about statements in Ruby in particular, you will most likely pick up everything there is to know about them just by following through the tutorial.Check this article to read more about statements.
For example, this:
var= "I am a ruby developer"
or this:
if (var!=1)
var1="wrong" are statements of some sort.
A statement is more of a general programming term not necessarily with a specific format in Ruby.It is true that,unlike C++ for example, you don't need to end each line with a ; sign or the fact that if..else or other conditional instructions might be written in a slightly different way that in other programming languages.
In a more restrictive meaning ("by the book," so to speak), a statement is an instruction that does not return a value. I've met a lot of people that no longer make this distinction though; probably for the wrong reasons. If you were to consider this definition, the second code example I gave would no longer represent a statement, but a larger piece of code.
There isn't much to say about statements in Ruby in particular, you will most likely pick up everything there is to know about them just by following through the tutorial.Check this article to read more about statements.
Subscribe to:
Posts (Atom)







