Using Ruby to Stay Informed With Innovative Thought

So I was thinking this evening about creating an RSS parser in Ruby. You know… Ruby supports this built in? Big surprise right? All you have to do in require the rss library:

require 'rss'

Then of course if you want to open up a connection to a URL you need to include the open uri library too:

require 'open-uri'

So that’s all the requirements. Next I’ll create a method called ReadRss that will take a single variable defined as “url”.

def ReadRss(url)
  open(url) do |page|
    respond = page.read
    result = RSS::Parser.parse(respond,false)
    puts "Blog: #{result.channel.title}, #{result.channel.description}"
    result.items.each_with_index do |item, i|
      i += 1
      puts "#{i}  #{item.title}"
    end
  end
end

That’s it. Now all you have to do is call ReadRss with the site feed address. Here’s a good hint for you:

ReadRss("https://innovativethought.wordpress.com/feed/")

So now that you can parse RSS feeds right from your Ruby script.  ATOM parser will come shortly.

Advertisements

Cleaning up my Ruby Fizzbuzz

As I become more familiar with Ruby and Rails I’m of course going to start to understand better ways to do a snippet of code. Here is an updated script that is a little leaner:

(1..100).each do |i|
  fb = []
  fb << "Fizz" if (i % 3) == 0
  fb << "Buzz" if (i % 5) == 0
  fb << i if (i % 3) != 0 and (i % 5) != 0
  puts (fb.join "")
end

I am still tring to review my notes, so I just ask that those of you awaiting my review of the Web 2.0 Expo please continue to be patient.

Solving the Fizzbuzz Puzzle with Ruby

So back in February Jeff Atwood over at codinghorror.com was talking about a puzzle to give prospective new-hires when interviewing them entitled “Fizzbuzz”. You can read more about it here.

So with a little thought I decided to solve the fizzbuzz puzzle using Rudy and an Array.

count = 0
100.times do
  count += 1
  fb = []
  fb << "Fizz" if (count % 3) == 0
  fb << "Buzz" if (count % 5) == 0
  fb << count if (count % 3) != 0 and (count % 5) != 0
  puts (fb.join "")
end

For those of you that are curious as to why I used a local variable of count rather then the index identifier, is because the article by Jeff Atwood requests a loop from 1-100 and Ruby starts looping at 0.