Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@xaviershay
Created December 22, 2008 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xaviershay/39075 to your computer and use it in GitHub Desktop.
Save xaviershay/39075 to your computer and use it in GitHub Desktop.
value = "Don T Alias"
first_name, last_name = value.reverse.split(' ', 2).reverse.collect(&:reverse)
first_name = first_name.to_s
last_name = last_name.to_s
# Refactored:
tokens = value.split(' ')
to_assign = ['' , '' ] if tokens.size == 0
to_assign = [tokens[0] , '' ] if tokens.size == 1
to_assign = [tokens[0..-2].join(' '), tokens.last] if tokens.size > 1
first_name, last_name = *to_assign
---
it 'splits the value and assigns it to first name and last name' do
object.name = "Don Alias"
object.first_name.should == "Don"
object.last_name.should == 'Alias'
end
it 'splits the value and assigns every thing except the last word to first name, and last word to last name' do
object.name = "Don T Alias"
object.first_name.should == "Don T"
object.last_name.should == 'Alias'
end
it 'when only one name is provided it blanks out last name' do
object.name = "Don"
object.first_name.should == "Don"
object.last_name.should == ''
end
it 'when a blank string is provided it blanks both first and last name' do
object.name = ""
object.first_name.should == ""
object.last_name.should == ''
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment