makersacademy / problem-solving

For problem-solving during the PreCourse
6 stars 2 forks source link

ruby-kickstart 2:13 - Failure/Error: expect(user.blogs.first).to eq blog #117

Closed mormolis closed 7 years ago

mormolis commented 7 years ago

Hello Makers,

I have a problem in ruby-kickstart 2:13 and I cannot tell where exactly I am doing wrong. I checked the solution but it doesn't seem a lot different than mine. I assume that it has to do with ruby classes and how to retrieve objects from an array.

the error as it appears on the bash:

Syntax OK
........F

Failures:

  1) User can retrieve its added blogs
     Failure/Error: expect(user.blogs.first).to eq blog

     NoMethodError:
       undefined method date' for #<Array:0x000000028d6808>
     # ./session2/challenge/13_classes_iterating_sorting.rb:86:in =='
     # ./session2/spec/13.rb:120:in block (2 levels) in <top (required)>'

Finished in 0.00415 seconds (files took 0.07319 seconds to load)
9 examples, 1 failure

Failed examples:

rspec ./session2/spec/13.rb:117 # User can retrieve its added blogs

My code is the following

require 'date'
class Blog
    attr_accessor :date, :user, :text
    def initialize(date, user, text)
        @date = date
        @user =user
        @text=text
    end
    def summary
        temp = @text.split(" ")
        if temp.size >10
            return temp[(0..9)].join(" ")
        else
            return @text
        end
    end
    def entry
        return "#{@user.username} #{@date}\n#{@text}"
    end
    def ==(other)
        if @date == other.date && @user == other.user && text == other.text
            return true
        end
        return false
    end
end
class User
    attr_accessor :username

    def initialize(username)
        @username = username
        @blog_entries =[]
    end
    def add_blog(date, text)
        blog_entry = Blog.new(date, self , text)
        @blog_entries.unshift(blog_entry)
    end
    def blogs
        return @blog_entries
    end
end

I do not understand whats wrong... I can only see two differences from the solution and I assume they have to do with @blog_entries.unshift(blog_entry)

and

def blogs
        return @blog_entries
    end

instead of having it with attr_accessor I define a getter method.

Could anyone enlighten me please?

Thank you in advance for your time!

haletothewood commented 7 years ago

I believe it may be how you're initializing your date, user and text. If you use self.name = name it initialises in a way that allows you to use it as a method that you can call such as other.name. In the way you have it it's just initialised as a keyword or variable which is why it's not identified as a method.

mormolis commented 7 years ago

Hi David! Thanks for your time! The @ symbol in front of a variable automatically makes it instance variable so its the same as @var, self.var (can anyone confirm?)

I think I found the mistake by looking at the solution I noticed that in add_blog method the solution returns the blog object it created. Something that I hadn't done. I fixed that. Now it throws another error about the way the blog entries are queued but I think I can fix that easily.

Thanks again for your time :)

haletothewood commented 7 years ago

Yeah, you're right...I was just testing you. Well done for figuring it out even with my "help".

mormolis commented 7 years ago

Thanks again! The process of discussing it made me find the solution. :)

and for the record the following is the complete solution

require 'date'
class Blog
    attr_accessor :date, :user, :text
    def initialize(date, user, text)
        @date = date
        @user =user
        @text=text
    end
    def summary
        temp = @text.split(" ")
        if temp.size >10
            return temp[(0..9)].join(" ")
        else
            return @text
        end
    end
    def entry
        return "#{@user.username} #{@date}\n#{@text}"
    end
    def ==(other)
        if @date == other.date && @user == other.user && text == other.text
            return true
        end
        return false
    end
    def <=>(other) 
        if @date > other.date
            return -1
        elsif @date < other.date
            return 1
        else
            return 0
        end
    end
end
class User
    attr_accessor :username

    def initialize(username)
        @username = username
        @blog_entries =[]
    end
    def add_blog(date, text)
        blog_entry = Blog.new(date, self , text)
        @blog_entries.unshift(blog_entry)
        @blog_entries.sort!
        blog_entry
    end
    def blogs
        return @blog_entries
    end
end