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