Skip to content

Instantly share code, notes, and snippets.

@Goom11
Created October 18, 2015 04:41
Show Gist options
  • Save Goom11/4860c22af8972e272abd to your computer and use it in GitHub Desktop.
Save Goom11/4860c22af8972e272abd to your computer and use it in GitHub Desktop.
FPForTheIntrigued In Ruby Included weather xml example at end
numbers = [1, 2, 3, 4, 5, 6]
totalBad = 0
for element in numbers
totalBad += element
end
# puts totalBad
total = numbers.reduce(0) { |carry, element|
carry + element
}
# puts total
doubledBad = []
for element in numbers
doubledBad.push(element * 2)
end
# puts doubledBad.inspect
doubled = numbers.map { |e|
e * 2
}
# puts doubled.inspect
greet = "Hello"
# puts greet
# puts greet.class
class CarBad
@year
@miles
def initialize(year, miles)
@year = year
@miles = miles
end
def drive(distance)
@miles += distance
end
def getYear
@year
end
def getMiles
@miles
end
def setMiles(miles)
@miles = miles
end
end
class Car
attr_reader :year
attr_accessor :miles
def initialize(year, miles)
@year = year
@miles = miles
end
def drive(distance)
@miles += distance
end
end
car1 = Car.new(2013, 0)
# puts car1.inspect
car1.drive(10)
# puts car1.inspect
def totalPricesBad(prices)
total = 0
for price in prices
total += price
end
total
end
prices = [10, 15, 20, 25, 30, 35, 40]
# puts totalPricesBad(prices)
def totalPricesOver25(prices)
total = 0
for price in prices
if price > 25
total += price
end
end
total
end
def totalPricesUnder25(prices)
total = 0
for price in prices
if price < 25
total += price
end
end
total
end
def totalPrices(prices, &selector)
total = 0
for price in prices
if selector.call(price)
total += price
end
end
total
end
totalPricesGood = totalPrices(prices) { |price|
true
}
# puts totalPricesGood
totalPricesUnder25 = totalPrices(prices) { |price|
price < 25
}
# puts totalPricesUnder25
totalPricesOver25 = totalPrices(prices) { |price|
price > 25
}
# puts totalPricesOver25
totalPricesOver25Better = prices.select { |price|
price > 25
}.inject(0) { |carry, price|
carry + price
}
# puts totalPricesOver25Better
require 'net/http'
require 'uri'
require 'nokogiri'
def getWeatherInfo(cityCode)
url = "https://weather.yahooapis.com/forecastrss?w=" + cityCode.to_s
puts url
source = Net::HTTP.get(URI.parse(url))
xml_doc = Nokogiri::XML(source)
puts xml_doc.xpath("//yweather:location // @city"),
xml_doc.xpath("//yweather:location // @region")
xml_doc.xpath("//yweather:condition // @temp")
end
# getWeatherInfo(2391271)
for id in 2391271..2391279
# puts getWeatherInfo(id)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment