Quick help refactoring Ruby Class

I've written this class that returns feed updates, but am thinking it can be further improved. It's not glitchy or anything, but as a new ruby developer, I reckon it's always good to improve :-)

class FeedManager attr_accessor :feed_object, :update, :new_entries require 'feedtosis' def initialize(feed_url) @feed_object = Feedtosis::Client.new(feed_url) fetch end def fetch @feed_object.fetch end def update @updates = fetch end def updated? @updates.new_entries.count > 0 ? true : false end def new_entries @updates.new_entries end end

As you can see, it's quite simple, but the things I'm seeing that aren't quite right are:

  1. Whenever I call fetch via terminal, it prints a list with the updates, when it's really supposed return an object.

So as an example, in the terminal if I do something like:

client = Feedtosis::Client.new('http://stackoverflow.com/feeds') result = client.fetch

I then get:

<Curl::Easy http://stackoverflow.com/feeds>

Which is exactly what I'd expect. However, when doing the same thing with "inniting" class with:


I'm getting the object returning as an array with all the items on the feed.

Sure I'm doing something wrong, so any help refactoring this class will he greatly appreciated.

Also, I'd like to see comments about my implementation, as well as any sort of comment to make it better would be welcome.

Thanks in advance

-------------Problems Reply------------

Try this:

class FeedManager

require 'feedtosis'

attr_accessor :feed_object

def initialize(feed_url)
self.feed_object = Feedtosis::Client.new(feed_url)
def fetch
def updates (reload = true)
@updates = reload ? fetch : @updates
def updated?
updates(false).new_entries.count > 0
def new_entries

Now you can get the updates as follows:

result = FeedManager.new("http://stackoverflow.com/feeds").updates

PS: I have removed the attr_accessor for :update, and :new_entries.


I have added code to enable conditional cache reload.

feed = FeedManager.new("http://stackoverflow.com/feeds")
updates = feed.updates # reloads the updates
# do something

updates = feed.updates(false) # get the updates from cache.

  1. :update, @updates

  2. count > 0 ? true : false can be just count > 0

It appears that you expect the initialize method to return the result of calling update. Initialize is basically a constructor in Ruby, so it will return the new FeedManager object.

It's also very "unusual" to place a require statement in the middle of a class definition.

Category:ruby on rails Views:0 Time:2010-03-21

Related post

  • refactoring ruby method 2011-02-27

    this is a simple question... Does this method can be refactored? def sum total = 0 [1,2,3,4].each do |num| total += num end total end thanks for your help! --------------Solutions------------- [1,2,3,4].inject { |total,num| total= total+num } OR as p

  • Help refactor Ruby code 2011-03-31

    Is there a better way to write this Ruby code? Please help to refactor. def get_code(val) case val when 0..20 then 'E2' when 20..32 then 'E1' when 33..40 then 'D' when 41..50 then 'C2' when 51..60 then 'C1' when 61..70 then 'B2' when 71..80 then 'B1'

  • Refactoring Ruby : Converting string array to int array 2011-10-25

    I'm refactoring a checkers program, and I am trying to process a players move request (in the form of "3, 3, 5, 5" for example) into an int array. I have the following method, but it doesn't feel as Ruby-like as I know it could be: def translate_move

  • Refactor ruby on rails model 2012-01-13

    Given the following code, How would you refactor this so that the method search_word has access to issueid? I would say that changing the function search_word so it accepts 3 arguments or making issueid an instance variable (@issueid) could be consid

  • Rails 3 - Refactor ruby conditions 2011-04-20

    I'd like to know if there is a simpler way to do these 2 conditions in ruby : if params[:action] == 'index' || params[:action] == 'show' and if !(comment = (session[:my_params].include?(:comment) rescue nil)).nil? Thanks in advance --------------Solu

  • How do I quickly reorder a Ruby Array given an order? 2011-10-29

    I have an array of values, and an array which determines the order. How can I quickly re-arrange the array in the given order? data = ['0','1','2','3','4','5'] order = [3,1,2,0,4,5] I want: data = ['3','1','2','0','4','5'] --------------Solutions----

  • Refactor Ruby on Rails snippets 2011-11-24

    Association between Account and user is one to many Account has many Users I need to do something in account model method which needs user object. Right now, I'm doing like: User.first.account.get_something(User.first) But It doesn't seems to be good

  • Refactor ruby helper method 2010-10-12

    I have a helper method which checks whether the collection of objects is empty? If not then it checks each one to make sure that the the existing event_id is not the @current_event.id. Here is my crack at it: def build_answers(question) if question.a

  • How to quickly print Ruby hashes in a table format? 2011-10-26

    Is there a way to quickly print a ruby hash in a table format into a file? Such as: keyA keyB keyC ... 123 234 345 125 347 4456 ... where the values of the hash are arrays of different sizes. Or is using a double loop the only way? Thanks -----------

  • IDE with refactoring support for Ruby on Rails 2012-01-02

    I am a java developer and use the Eclipse IDE for developing java code. I use the built-in refactoring features a lot, since good refactoring support from the IDE helps to speed up and leads to higher code quality. I would not like to miss it and use

  • "refactor refactor refactor your code." What does this mean exactly and why do it? 2009-11-13

    I often heard from professionals blog something like refactoring your code whenever the chance you get. What is it exactly? Rewriting your code in simpler and fewer lines? What is the purpose of doing this? --------------Solutions------------- Refact

  • Why learn Perl, Python, Ruby if the company is using C++, C# or Java as the application language? 2008-09-17

    I wonder why would a C++, C#, Java developer want to learn a dynamic language? Assuming the company won't switch its main development language from C++/C#/Java to a dynamic one what use is there for a dynamic language? What helper tasks can be done b

  • Can Ruby really be used as a functional language? 2008-10-17

    Can Ruby really be used as a functional language? What are some good tutorials to teach this facet of the language? Note: I really want to use and stick with Ruby as my primary language so I am not interested at this point in being converted to YAFL

  • Writing Module for Ruby 2009-02-14

    How do you write a module for ruby. in python you can use # module.py def helloworld(name): print "Hello, %s" % name # main.py import module module.helloworld("Jim") Back to the Question how do you create a module in/for ruby --------------Solutions-

  • Ruby and Ruby on Rails crash course syllabus 2009-02-18

    I intend to design an induction plan or you can say a crash course syllabus for developers starting to learn Ruby and Ruby on rails. I am not sure about number of topics to be covered (And the depth of coverage) as this is a 3 day course (24 to 27 ho

  • Is there a "Code Complete" book for Ruby? 2009-11-09

    I began reading the book "Code Complete" 2nd edition, but stopped reading when I noticed most of the solutions were easily solvable in Ruby with Ruby idioms. Is there a similar book for Ruby? Here's the version that I started reading: http://www.amaz

  • Worth learning Ruby if not using Rails? 2010-01-22

    So I took a quick look at Ruby some days ago. At first I was quite astonished by some things it features which really seem to make a programmers life easier. (Use of blocks, object model,...) But then there is this big problem: Nobody seems to be usi

  • Text manipulation in Ruby 2010-02-24

    I'm trying to write a word counter for LyX files. Life is almost very simple as most lines that need to be ignored begin with a \ (I'm prepared to make the assumption that no textual lines begin with backslashes) - however there are some lines that l

  • How do you remove functionality from a program in ruby? 2010-05-05

    You have some code you want to remove associated with an obsolete piece of functionality from a ruby project. How do ensure that you get rid of all of the code? Some guidelines that usually help in refactoring ruby apply, but there are added challeng

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.212 (s). 11 q(s)