Saturday, August 27, 2011

Usage of Barometer gem

You can use barometer right out of the box, as it is configured to use one register-less (no API key required) international weather service (wunderground.com).

  require 'barometer'
  barometer = Barometer.new("Paris")
  weather = barometer.measure  puts weather.current.temperature

The available sources are:

  Wunderground.com (:wunderground) [default]    Yahoo! Weather (:yahoo)


Google Weather (:google) Weather.com (:weather_dot_com) [requires key]

WeatherBug.com (:weather_bug) [requires key]

Source Configuration:

Barometer can be configured to use multiple weather service APIs (either in a primary/failover config or in parallel). Each weather service can also have its own config.

Weather services in parallel

  Barometer.config = { 1 => [:yahoo, :google] }

Weather services in primary/failover

  Barometer.config = { 1 => [:yahoo], 2 => :wunderground }

Weather services, one with some configuration. In this case we are setting a weight value, this weight is respected when calculating averages.

  Barometer.config = { 1 => [{:wunderground => {:weight => 2}}, :google] }

Weather services, one with keys.

  Barometer.config = { 1 => [:yahoo, {:weather_dot_com => {:keys =>

{:partner => PARTNER_KEY, :license => LICENSE_KEY } }}] }

Multiple weather API, with hierarchy:

  require 'barometer'  # use yahoo and google, if they both fail, use wunderground

Barometer.config = { 1 => [:yahoo, :google], 2 => :wunderground }
barometer = Barometer.new("Paris") weather = barometer.measure
puts weather.current.temperture

Tuesday, August 23, 2011

Puzzle is that there is a bar which can contain n marbles. But one of the marble is heavier than others. Write a program to get heaviest marble.

Ruby 1.9:

def numeric?(object)
true if Float(object) rescue false
end

def enter_heavy_weight(j)
print "Enter weight of marble #{j}: "
@weights << enter_numeric(gets)
end

def enter_numeric(value)
if numeric?(value)
value.to_f
else
print "Please enter integer value: "
enter_numeric(gets)
end
end

def heavy_weight
print "Please Enter Total Number of Marbles: "
n = enter_numeric(gets)
@weights = []
for i in 1..n
enter_heavy_weight(i)
end

heaviest_marble_position = @weights.each_with_index.max[1]

puts "Heaviest Marble is at position: #{heaviest_marble_position + 1} Having Weight: #{@weights[heaviest_marble_position]}"

end

heavy_weight

Sunday, February 28, 2010

Begin Playing With Rails 3

Update: There’s now an official gem for working with Rails 3 beta.

$ sudo gem install tzinfo builder memcache-client rack rack-test erubis mail text-format bundler thor i18n
$ sudo gem install rack-mount --version=0.4.0
$ sudo gem install rails --pre
$ rails -v
Rails 3.0.0.beta

Installing Ruby 1.9.1 on Ubuntu 9.04

The dependencies are simple;
sudo apt-get install build-essential libssl-dev libreadline5 libreadline5-dev zlib1g zlib1g-dev

Then the usual process…

mkdir ~/src && cd ~/src
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p0.tar.gz
tar -xvf ruby-1.9.1-p0.tar.gz
cd ruby-1.9.1-p0
./configure
make
make test
sudo make install

Wednesday, July 15, 2009

Installing Ruby on Rails, Apache2 And Passenger in Linux

1. Installing Ruby on Rails:

i. Download the latest ruby source and rubygems source to /usr/local/src/ and

extract them. At the time of this writing, those commands were:

ii. cd /usr/local/src

iii. wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p72.tar.gz (wget is the

cmd which downloads the tar.gz file)

iv. tar xzf ruby-1.8.7-p72.tar.gz

v. wget http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz

vi. tar xzf rubygems-1.3.1.tgz

2. Go into the Ruby directory and compile it, like so:

cd ruby-1.8.7-p72

./configure

make

NOTE: This was the key part to watch. This time around, you should see

compile messages stating that zlib was compiled successfully at the end of the

log on your screen.

3. Now install Ruby, if all went well:

make install

4. Now go setup RubyGems, like so:

cd ../rubygems-1.3.1

ruby setup.rb

5. Update the gem system for good measure

gem update --system

6. Install Rails

gem install rails

How to Install the Apache Web Server?

Apache is one of the most popular Web servers on the Web right now, and

part of its charm is that it's free. It also has a lot of features that make it very

extensible and useful for many different types of Web sites. It is a server that

is used for personal Web pages up to enterprise level sites.

This article will discuss how to install Apache on a Linux system. Before we

start you should be at least comfortable working in Linux - changing

directories, using tar and gunzip, and compiling with make (I'll discuss where

to get binaries if you don't want to mess with compiling your own). You should

also have access to the root account on the server machine.

Download Apache:

I recommend downloading the latest stable release. At the time of this

writing, that was Apache 2.0. The best place to get Apache is from the

Apache HTTP Server download site. Download the sources appropriate to

your system. Binary releases are available as well.

Extract the Files:

Once you've downloaded the files you need to uncompress them and

untarring:

i. gunzip -d httpd-2_0_NN.tar.gz

ii. tar xvf httpd-2_0_NN.tar

This creates a new directory under the current directory with the source

files.

Configuring:

Once you've got the files, you need to tell your machine where to find

everything by configuring the source files. The easiest way is to accept all

the defaults and just type:

./configure

Of course, most people don't want to accept just the default choices. The most

important option is the prefix= option. This specifies the directory where the

Apache files will be installed. You can also set specific environment variables

and modules. Some of the modules I like to have installed are:

* mod_alias - to map different parts of the URL tree

* mod_include - to parse Server Side Includes

* mod_mime - to associate file extensions with its MIME-type

* mod_rewrite - to rewrite URLs on the fly

* mod_speling (sic) - to help your readers who might misspell URLs

* mod_ssl - to allow for strong cryptography using SSL

* mod_userdir - to allow system users to have their own Web page directories

Please keep in mind that these aren't all the modules I might install on a given

system. Read the details about the modules to determine which ones you

need.

Build:

As with any source installation, you'll then need to build the installation:

make

make install

Customize:

Assuming that there were no problems, you are ready to customize your

Apache configuration. This really just amounts to editing the httpd.conf file.

This file is located in the PREFIX/conf directory. I generally edit it with vi:

vi PREFIX/conf/httpd.conf

Note: you'll need to be root to edit this file.

Follow the instructions in this file to edit your configuration the way you want

it. More help is available on the Apache Web site.

Test Your Server:

Open a Web browser on the same machine and type http://localhost/ in the

address box. You should see a page similar to the one in the partial screen

shot above. Specifically, it will say in big letters "Seeing this instead of the

website you expected?" This is good news, as it means your server installed

correctly.

Start Editing/Uploading Pages

Once your server is up and running you can start posting pages. Have fun

building your Web site.


Easiest way to install Passenger:

# Open a terminal, and type:

gem install passenger

# Type:

passenger-install-apache2-module

Or, if you want to install the Nginx version instead:

passenger-install-nginx-module

Installing a Crontab File

What is cron?

Crontab is a simple text file that holds a list of commands that are to be run at specified times.Cron is a that executes shell commands periodically on a given schedule. Cron is driven by a crontab, a configuration file that holds details of what commands are to be run along with a timetable of when to run them.

Description:

crontab -e [Edits a copy of the user's crontab file]

crontab -r [Remove the user's crontab files]

crontab -l [Lists the user's crontab file]

Crontab syntax:

A crontab file has six fields for specifying minute, hour, day of month, month, day of week and the command to be run at that interval. See below:

*     *     *     *     *  command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)

Any line beginning with a ``#'' is a comment and is ignored.

Crontab examples:

* * * * *  #Runs every minute
*/2 * * * * #Runs every 2 minutes
35 * * * * #Runs at 35 minutes past the hour
30 5 * * * #Runs at 5:30 am every day
45 18 * * * #Runs at 6:45 pm every day
00 3 * * 0 #Runs at 3:00 am every Sunday
00 2 * * 6 #Runs at 2:00 am every Satuday
00 1 * * Sun #Runs at 1:00 am every Sunday
30 5 1 * * #Runs at 5:30 am on the first day of every month
00 0-23/2 03 08 * #Runs every other hour on the 3rd of August


There are also special strings that can be used:
@reboot #Runs at boot
@yearly #Runs once a year [0 0 1 1 *]
@annually #Runs once a year [0 0 1 1 *]
@monthly #Runs once a month [0 0 1 * *]
@weekly #Runs once a week [0 0 * * 0]
@daily #Runs once a day [0 0 * * *]
@midnight #Runs once a day [0 0 * * *]
@hourly #Runs once an hour [0 * * * *]

Monday, July 6, 2009

RailRoad (Ruby on Rails diagrams generator)

What is RailRoad?

RailRoad is a class diagrams generator for Ruby on Rails applications. It's a Ruby script that loads the application classes and analyzes its properties (attributes, methods) and relationships (inheritance, model associations like has_many, etc.) The output is a graph description in the DOT language, suitable to be handled with tools like Graphviz.

RailRoad can produce:

  • Model diagrams, showing both inheritance hierarchy and models associations. You can choose to show the model "content columns" and its types.
  • Controller diagrams, showing inheritance hierarchy. You can include the controllers' methods, grouped by its visibility (public, protected, private.)
  • State machine diagramas (for use with the "acts_as_state_machine" plugin.)

Download RailRoad

If you're using gem, you can install RailRoad by issuing:

gem install railroad

Usage

Run RailRoad on the Rails application's root directory. You can redirect its output to a .dot file or pipe it to the dot or neato utilities to produce a graphic. Model diagrams are intended to be processed using dot and controller diagrams are best processed using neato.

railroad [options] command

Common options

  • -b, --brief
    Generate compact diagram (no attributes nor methods)
  • -e, --exclude file1[,fileN]
    Exclude given files
  • -i, --inheritance
    Include inheritance relations
  • -l, --label
    Add a label with diagram information (type, date, migration, version)
  • -o, --output FILE
    Write diagram to file FILE
  • -r, --root PATH
    Set PATH as the application root
  • -v, --verbose
    Enable verbose output (produce messages to STDOUT)

Models diagram options

  • -a, --all
    Include all models (not only ActiveRecord::Base derived)
  • --hide-magic
    Hide magic field names
  • --hide-types
    Hide attributes type
  • -j, --join
    Concentrate edges
  • -m, --modules
    Include modules
  • -p, --plugins-models
    Include plugins models
  • -t, --transitive
    Include transitive associations (through inheritance)

Controllers diagram options

  • --hide-public
    Hide public methods
  • --hide-protected
    Hide protected methods
  • --hide-private
    Hide private methods

Other options

  • -h, --help
    Show this message
  • --version
    Show version and copyright

Commands

(You must supply one of these)

  • -M, --models
    Generate models diagram
  • -C, --controllers
    Generate controllers diagram
  • -A, --aasm
    Generate "acts as state machine" diagram

Examples

  • railroad -o models.dot -M
    Produces a models diagram to the file 'models.dot'
  • railroad -a -i -o full_models.dot -M
    Models diagram with all classes showing inheritance relations
  • railroad -M | dot -Tsvg > models.svg
    Model diagram in SVG format
  • railroad -C | neato -Tpng > controllers.png
    Controller diagram in PNG format
  • railroad -h
    Shows usage help