def name_shuffler name name.to_s.strip.split(/\s+/).reverse.join(" ") end require "spec" describe "#name_shuffler" do it { expect(name_shuffler "Foo Bar").to eq "Bar Foo"} it { expect(name_shuffler "Foo Bar").to eq "Bar Foo"} it { expect(name_shuffler " Foo Bar").to eq "Bar Foo"} it { expect(name_shuffler "Foo Bar ").to eq "Bar Foo"} it { expect(name_shuffler "Foo").to eq "Foo"} it { expect(name_shuffler " Foo").to eq "Foo"} it { expect(name_shuffler "Foo ").to eq "Foo"} it { expect(name_shuffler "").to eq ""} it { expect(name_shuffler nil).to eq ""} it { expect(name_shuffler 777).to eq "777"} end
>> rspec name_shuffler.rb .......... Finished in 0.0059 seconds (files took 0.15188 seconds to load) 10 examples, 0 failures
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Ruby Language
Robust version and with test specs!
output