#1) What is your favorite Ruby class/library or method and why?
I like Ruby in all. Some awesome points: Blocks/Lambdas, "Everything is an Object", Metaprogramming, etc.
---------------
#2) Given HTML: "
" Using Nokogiri how would you select the src attribute from the image? Show me two different ways to do that correctly the the HTML given.
document = Nokogiri::HTML(open("http://lalala.com/"))
document.at_css('.images img').attr('src')
#or
document.xpath('//div[@class="images"]/img/@src').map {|s| s.value }
---------------
# 3) If found HTML was a collection of li tags within a div with class="attr", how would you use Nokogiri to collect that information into one array?
document.xpath('//li[@class="attr"]').map {|li| li.value }
---------------
# 4) Given the following HTML: Title:
# The Well-Grounded Rubyist
Author:
# David A. Black
Price:
# $34.99
Description:
# A great book for Rubyists
Seller:
# Ruby Scholar
# Please collect all of the data presented into a key-value store. Please include code and the output.
document = Nokogiri::HTML(' Title:
The Well-Grounded Rubyist
Author: David A. Black
Price: $34.99
Description: A great book for Rubyists
Seller: Ruby Scholar
')
books = document.xpath('//div[@class="row"]/span').map do |v|
res = v.xpath('//span').map(&:text)
evens = res.values_at(*a.each_index.select(&:even?))
Hash[*(evens).zip(res - evens).flatten]
end
p books
=> [{"Title:"=>"The Well-Grounded Rubyist", "Author:"=>"David A. Black", "Price:"=>"$34.99", "Description:"=>"A great book for Rubyists", "Seller:"=>"Ruby Scholar"},
{"Title:"=>"The Well-Grounded Rubyist", "Author:"=>"David A. Black", "Price:"=>"$34.99", "Description:"=>"A great book for Rubyists", "Seller:"=>"Ruby Scholar"},
{"Title:"=>"The Well-Grounded Rubyist", "Author:"=>"David A. Black", "Price:"=>"$34.99", "Description:"=>"A great book for Rubyists", "Seller:"=>"Ruby Scholar"},
{"Title:"=>"The Well-Grounded Rubyist", "Author:"=>"David A. Black", "Price:"=>"$34.99", "Description:"=>"A great book for Rubyists", "Seller:"=>"Ruby Scholar"},
{"Title:"=>"The Well-Grounded Rubyist", "Author:"=>"David A. Black", "Price:"=>"$34.99", "Description:"=>"A great book for Rubyists", "Seller:"=>"Ruby Scholar"},
{"Title:"=>"The Well-Grounded Rubyist", "Author:"=>"David A. Black", "Price:"=>"$34.99", "Description:"=>"A great book for Rubyists", "Seller:"=>"Ruby Scholar"},
{"Title:"=>"The Well-Grounded Rubyist", "Author:"=>"David A. Black", "Price:"=>"$34.99", "Description:"=>"A great book for Rubyists", "Seller:"=>"Ruby Scholar"},
{"Title:"=>"The Well-Grounded Rubyist", "Author:"=>"David A. Black", "Price:"=>"$34.99", "Description:"=>"A great book for Rubyists", "Seller:"=>"Ruby Scholar"},
{"Title:"=>"The Well-Grounded Rubyist", "Author:"=>"David A. Black", "Price:"=>"$34.99", "Description:"=>"A great book for Rubyists", "Seller:"=>"Ruby Scholar"},
{"Title:"=>"The Well-Grounded Rubyist", "Author:"=>"David A. Black", "Price:"=>"$34.99", "Description:"=>"A great book for Rubyists", "Seller:"=>"Ruby Scholar"}]
------------------
#5) What Ruby feature do you hate?
It is sometimes slow...