Closed mormolis closed 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.
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 :)
Yeah, you're right...I was just testing you. Well done for figuring it out even with my "help".
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
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:
My code is the following
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
instead of having it with attr_accessor I define a getter method.
Could anyone enlighten me please?
Thank you in advance for your time!