From: Sam Duncan Date: 2012-07-16T11:31:13+09:00 Subject: Re: What does this code do? On 07/16/2012 02:19 PM, Mike Glaz wrote: > 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? > > 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 Without knowing much about ActiveRecord, I would imagine your users array to contain an instance of class User for each person who had a record whereby their 'username' matched a '@' fragment from your Micropost instances content. In other words; if the content contained '@mike', then your users array would contain a User instance with the username attribute of 'mike'. Does that match what you see? If this is a 'Rails' app you could probably get additional help from the Rails mailing list. Hope that helps. Sam