Skip to content

Instantly share code, notes, and snippets.

@notahat
Forked from xaviershay/gist:39075
Created December 22, 2008 21:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save notahat/39132 to your computer and use it in GitHub Desktop.
Save notahat/39132 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 to take into account Xavier's pathological aversion to case statements:
words = value.split(' ')
first_name, last_name = if words.size == 0
['', '']
elsif words.size == 1
[words.first, '']
else
[words[0..-2].join(' '), words.last]
end
---
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