From 7cdb037824ae86e94bd991744253756c8268f834 Mon Sep 17 00:00:00 2001 From: 1s22s1 <1s22s1@users.noreply.github.com> Date: Wed, 18 Jun 2025 23:38:32 +0900 Subject: [PATCH] Simplify sample code --- README.md | 20 ++++++++------------ lib/rss.rb | 17 ++++++----------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index fa90a1a..31eb288 100644 --- a/README.md +++ b/README.md @@ -30,20 +30,16 @@ Or install it yourself as: ### Consuming RSS -If you'd like to read someone's RSS feed with your Ruby code, you've come to the right place. It's really easy to do this, but we'll need the help of open-uri: +If you'd like to read someone's RSS feed with your Ruby code, you've come to the right place. It's really easy to do this: ```ruby - require 'rss' - require 'open-uri' - - url = 'https://www.ruby-lang.org/en/feeds/news.rss' - URI.open(url) do |rss| - feed = RSS::Parser.parse(rss) - puts "Title: #{feed.channel.title}" - feed.items.each do |item| - puts "Item: #{item.title}" - end - end +require 'rss' + +feed = RSS::Parser.parse('https://www.ruby-lang.org/en/feeds/news.rss') +puts "Title: #{feed.channel.title}" +feed.items.each do |item| + puts "Item: #{item.title}" +end ``` As you can see, the workhorse is RSS::Parser#parse, which takes the source of the feed and a parameter that performs validation on the feed. We get back an object that has all of the data from our feed, accessible through methods. This example shows getting the title out of the channel element, and looping through the list of items. diff --git a/lib/rss.rb b/lib/rss.rb index 9da7b11..7c88448 100644 --- a/lib/rss.rb +++ b/lib/rss.rb @@ -19,19 +19,14 @@ # == Consuming RSS # # If you'd like to read someone's RSS feed with your Ruby code, you've come to -# the right place. It's really easy to do this, but we'll need the help of -# open-uri: +# the right place. It's really easy to do this. # # require 'rss' -# require 'open-uri' -# -# url = 'http://www.ruby-lang.org/en/feeds/news.rss' -# URI.open(url) do |rss| -# feed = RSS::Parser.parse(rss) -# puts "Title: #{feed.channel.title}" -# feed.items.each do |item| -# puts "Item: #{item.title}" -# end +# +# feed = RSS::Parser.parse('https://www.ruby-lang.org/en/feeds/news.rss') +# puts "Title: #{feed.channel.title}" +# feed.items.each do |item| +# puts "Item: #{item.title}" # end # # As you can see, the workhorse is RSS::Parser#parse, which takes the source of