Is it possible in Ruby to instantiate another object of a different class using a class method?
I have spent a lot of time to no avail researching Google, the Ruby docs, and Stack Overflow, for an answer to my query, so I am posting this question as a last resort.
Two classes, User, and Blog. In User below I am trying to create a new instance of an object for Blog, and carry over some attributes.
In class User there are 2 methods;
class User
attr_accessor :blogs, :username
def initialize(username)
self.username = username
self.blogs = []
end
def add_blog(date, text)
self.blogs << [Date.parse(date),text]
new_blog = [Date.parse(date),text]
Blog.new(@username,date,text)
end
end
Using the above add_blog I would like to initialize & send a new object to the Blog class.
class Blog
attr_accessor :text, :date, :user
def initialize(user,date,text)
user = user
date = date
text = text
end
end