#10
Mar 26, 2007

Refactoring User Name Part 1

Learn how to clean up your code through refactoring. This episode will show you how to move code from the view into the model to remove duplication and simplify the view.
Tags: refactoring
Download (11.8 MB, 5:44)
alternative download for iPod & Apple TV (7.8 MB, 5:44)
<!-- show.rhtml -->
Name: <%= @user.full_name %>
# models/user.rb
def full_name
  name = first_name + " "
  name += "#{middle_initial}. " unless middle_initial.nil?
  name += last_name
  name
end

6 comments

louis Jul 10, 2007 at 23:50

i was curious about the interpolation of middle_name in the full_name method. why use interpolation there if 'middle_initial' (which i assume is a column in the db) would work too? is it b/c <pre>middle_initial + " " unless middle_initial.nil?</pre> wouldn't work? or would it?
this site is amazing, btw!


Ryan Bates Jul 11, 2007 at 07:32

Concatonating strings like that will cause an error if middle_initial returns nil, which is why I chose to use string interpolation.


louis Jul 11, 2007 at 10:16

thanks!


Beck Lin Apr 30, 2008 at 08:47

Hi Ryan, I think
name += middle_initial + ". " unless middle_initial.nil?
still work, won't cause any error even if middle_initial returns nil.
B/c the priority should be:
(name += middle_initial + ". ") unless middle_initial.nil?
Please let me know if I'm wrong.


kino May 23, 2008 at 01:53

As is proven in the ontological manuals, our faculties exclude the possibility of, in particular, the objects in space and time; in the study of our experience, the noumena prove the validity of, indeed, the Ideal of natural reason.


johny Jun 24, 2008 at 01:44

FUCK U ALL!

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(required)

subscribe:
sponsored by:
if you want to help:
required:
Get Quicktime Player