Skip to content

Instantly share code, notes, and snippets.

@joho
Forked from notahat/gist:39132
Created December 22, 2008 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joho/39169 to your computer and use it in GitHub Desktop.
Save joho/39169 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:
last_space_index = value.rindex(' ') || value.size
first_name, last_name = value[0..last_space_index].strip, value[last_space_index..value.size].strip
---
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