Topic: What does this code do?

I'm reading through a tutorial and found a solution for it (because I'm
stuck), but I don't understand some code, so I was hoping someone could
shed some light on it:

USERNAME_REGEX = /@\w+/i

class Micropost < ActiveRecord::Base
    def people_replied
      users = []
      self.content.clone.gsub!( USERNAME_REGEX ).each do |username|
        user = User.find_by_username(username[1..-1])
        users << user if user
      end
      users
    end
end

This is a blog/twitter like app and I'm guessing self.content should
contain  a string like '@mike hi how are you?'.  What would be the
result in the users array with such a string?  Would it just be 'mike' or would it end up being 'mike hi how are you?'

The class User looks like so:

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation,
:username

  has_many :microposts, :dependent => :destroy
end

Re: What does this code do?

It seems the method loops through the post and creates an array of all the users mentioned in it.
I it could be @mike @sven @sara. The functions try to see if there is users with those usernames and add them to the array.

Re: What does this code do?

So it cuts of any text after the username, correct?

Re: What does this code do?

Just grabs the names and makes an array of the users.
Edit: Its userobjects, not just the user names. And yes it skips the rest of the text.

Last edited by AndreasE (2012-07-16 13:10:44)

Re: What does this code do?

thanks for your help AndreasE!