Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > String Manipulation Nuby Question

Reply
Thread Tools

String Manipulation Nuby Question

 
 
Chris Roos
Guest
Posts: n/a
 
      05-05-2005
I have a Person with title, forename and surname (all of which are
optional). I want to return a 'pretty' name for this person in the format..

title + <space> + forename + <space> + surname

..where any extraneous spaces are removed.

My name method currently looks like this

def name
(
(title.to_s.strip.empty? ? "" : title.to_s.strip << " ") <<
(forename.to_s.strip.empty? ? "" : forename.to_s.strip << " ") <<
(surname.to_s.strip.empty? ? "" : surname.to_s.strip)
).strip
end

I created seven tests for this method for each combination of the three
parameters. All seven tests pass with this implementation.

Note. I was half way through writing about how I should probably test
for nil and maybe return an empty string from the three (title,
forename, surname) methods when I realised that all I was really doing
was testing for nil in a roundabout way; i.e. convert possible nil's to
string then strip spaces then check for empty...

I started thinking about this some more and realised that a test for nil
would return false which would allow me to re-write the above method
like so..

def name
(
(title ? title.strip << " " : "") <<
(forename ? forename.strip << " " : "") <<
(surname ? surname.strip : "")
).strip
end

This gets rid of the nasty duplication and double question marks in the
first method.

I also realised that I needed another test - for all nil's. They all
still passed.

What I was originally going to ask was how to improve on the first
implementation above. Even though (in my opinion) the second
implementation is a lot cleaner I'm still interested in whether it can
be made even more succinct as in 'the ruby way'?

It may be a very small method and have taken me about an hour to go
through this process but I'm quite pleased as to how it's turned out.

Chris



 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      05-05-2005
"#{title} #{forename} #{surname}".strip

On 5/5/05, Chris Roos <> wrote:
> I have a Person with title, forename and surname (all of which are
> optional). I want to return a 'pretty' name for this person in the format..
>
> title + <space> + forename + <space> + surname
>
> ..where any extraneous spaces are removed.
>
> My name method currently looks like this
>
> def name
> (
> (title.to_s.strip.empty? ? "" : title.to_s.strip << " ") <<
> (forename.to_s.strip.empty? ? "" : forename.to_s.strip << " ") <<
> (surname.to_s.strip.empty? ? "" : surname.to_s.strip)
> ).strip
> end
>
> I created seven tests for this method for each combination of the three
> parameters. All seven tests pass with this implementation.
>
> Note. I was half way through writing about how I should probably test
> for nil and maybe return an empty string from the three (title,
> forename, surname) methods when I realised that all I was really doing
> was testing for nil in a roundabout way; i.e. convert possible nil's to
> string then strip spaces then check for empty...
>
> I started thinking about this some more and realised that a test for nil
> would return false which would allow me to re-write the above method
> like so..
>
> def name
> (
> (title ? title.strip << " " : "") <<
> (forename ? forename.strip << " " : "") <<
> (surname ? surname.strip : "")
> ).strip
> end
>
> This gets rid of the nasty duplication and double question marks in the
> first method.
>
> I also realised that I needed another test - for all nil's. They all
> still passed.
>
> What I was originally going to ask was how to improve on the first
> implementation above. Even though (in my opinion) the second
> implementation is a lot cleaner I'm still interested in whether it can
> be made even more succinct as in 'the ruby way'?
>
> It may be a very small method and have taken me about an hour to go
> through this process but I'm quite pleased as to how it's turned out.
>
> Chris
>
>




 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      05-05-2005
Hi --

On Fri, 6 May 2005, Chris Roos wrote:

> I have a Person with title, forename and surname (all of which are optional).
> I want to return a 'pretty' name for this person in the format..
>
> title + <space> + forename + <space> + surname
>
> ..where any extraneous spaces are removed.
>
> My name method currently looks like this
>
> def name
> (
> (title.to_s.strip.empty? ? "" : title.to_s.strip << " ") <<
> (forename.to_s.strip.empty? ? "" : forename.to_s.strip << " ") <<
> (surname.to_s.strip.empty? ? "" : surname.to_s.strip)
> ).strip
> end
>

[...]
> What I was originally going to ask was how to improve on the first
> implementation above. Even though (in my opinion) the second implementation
> is a lot cleaner I'm still interested in whether it can be made even more
> succinct as in 'the ruby way'?


I'd let Ruby do more of the work, especially the iterating through the
items. Something like this might be suitable:

class Person
attr_accessor :title, :forename, :surname
def name
[title, forename, surname].compact.map {|s| s.strip }.join(" ")
end
end


David

--
David A. Black



 
Reply With Quote
 
Chris Roos
Guest
Posts: n/a
 
      05-05-2005
Thanks for coming back so fast. Unfortunately, this doesn't pass one of
the tests - where only a title and surname is supplied this
implementation will put two spaces in the middle...

"Mr Bloggs" instead of
"Mr Bloggs"

Chris

Logan Capaldo wrote:
> "#{title} #{forename} #{surname}".strip
>
> On 5/5/05, Chris Roos <> wrote:
>
>>I have a Person with title, forename and surname (all of which are
>>optional). I want to return a 'pretty' name for this person in the format..
>>
>>title + <space> + forename + <space> + surname
>>
>>..where any extraneous spaces are removed.
>>
>>My name method currently looks like this
>>
>>def name
>> (
>> (title.to_s.strip.empty? ? "" : title.to_s.strip << " ") <<
>> (forename.to_s.strip.empty? ? "" : forename.to_s.strip << " ") <<
>> (surname.to_s.strip.empty? ? "" : surname.to_s.strip)
>> ).strip
>>end
>>
>>I created seven tests for this method for each combination of the three
>>parameters. All seven tests pass with this implementation.
>>
>>Note. I was half way through writing about how I should probably test
>>for nil and maybe return an empty string from the three (title,
>>forename, surname) methods when I realised that all I was really doing
>>was testing for nil in a roundabout way; i.e. convert possible nil's to
>>string then strip spaces then check for empty...
>>
>>I started thinking about this some more and realised that a test for nil
>>would return false which would allow me to re-write the above method
>>like so..
>>
>>def name
>> (
>> (title ? title.strip << " " : "") <<
>> (forename ? forename.strip << " " : "") <<
>> (surname ? surname.strip : "")
>> ).strip
>>end
>>
>>This gets rid of the nasty duplication and double question marks in the
>>first method.
>>
>>I also realised that I needed another test - for all nil's. They all
>>still passed.
>>
>>What I was originally going to ask was how to improve on the first
>>implementation above. Even though (in my opinion) the second
>>implementation is a lot cleaner I'm still interested in whether it can
>>be made even more succinct as in 'the ruby way'?
>>
>>It may be a very small method and have taken me about an hour to go
>>through this process but I'm quite pleased as to how it's turned out.
>>
>>Chris
>>
>>

>
>
>
>





 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      05-05-2005
Hi --

On Fri, 6 May 2005, Logan Capaldo wrote:

> On 5/5/05, Chris Roos <> wrote:
>>
>> I have a Person with title, forename and surname (all of which are
>> optional). I want to return a 'pretty' name for this person in the format..

>
> "#{title} #{forename} #{surname}".strip


Much more succinct than mine You would, however, have to put a
squeeze(" ") in there too, to take out extra spaces in the middle.

" Mrs. Emma Peel ".squeeze(" ").strip
=> "Mrs. Emma Peel"


David

--
David A. Black



 
Reply With Quote
 
Eric Hodel
Guest
Posts: n/a
 
      05-05-2005
On 05 May 2005, at 16:45, Logan Capaldo wrote:

> On 5/5/05, Chris Roos <> wrote:
>> I have a Person with title, forename and surname (all of which are
>> optional). I want to return a 'pretty' name for this person in the
>> format..
>>
>> title + <space> + forename + <space> + surname
>>
>> ..where any extraneous spaces are removed.

>
> "#{title} #{forename} #{surname}".strip


This leaves spaces in the middle.

[title, forename, surname].join(' ').gsub(/ /, ' ')

require 'test/unit'

class TestFullName < Test::Unit::TestCase

def setup
@title = "Mr."
@surname = "Hodel"
end

def test_strip
assert_equal "Mr. Hodel", "#{@title} #{@forename} #{@surname}".strip
end

def test_join_gsub
assert_equal("Mr. Hodel",
[@title, @forename, @surname].join(' ').gsub(/ /, '
'))
end

end

--
Eric Hodel - - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04



 
Reply With Quote
 
Dominik Schlütter
Guest
Posts: n/a
 
      05-05-2005
Hi,

Logan Capaldo <> wrote:

> "#{title} #{forename} #{surname}".strip


But that would leave double spaces in the middle if 'forename' was
empty, wouldn't it?


Regards,

Dominik.
 
Reply With Quote
 
Chris Roos
Guest
Posts: n/a
 
      05-05-2005
David A. Black wrote:
> Hi --
>
> On Fri, 6 May 2005, Chris Roos wrote:
>
>> I have a Person with title, forename and surname (all of which are
>> optional). I want to return a 'pretty' name for this person in the
>> format..
>>
>> title + <space> + forename + <space> + surname
>>
>> ..where any extraneous spaces are removed.
>>
>> My name method currently looks like this
>>
>> def name
>> (
>> (title.to_s.strip.empty? ? "" : title.to_s.strip << " ") <<
>> (forename.to_s.strip.empty? ? "" : forename.to_s.strip << " ") <<
>> (surname.to_s.strip.empty? ? "" : surname.to_s.strip)
>> ).strip
>> end
>>

> [...]
>
>> What I was originally going to ask was how to improve on the first
>> implementation above. Even though (in my opinion) the second
>> implementation is a lot cleaner I'm still interested in whether it can
>> be made even more succinct as in 'the ruby way'?

>
>
> I'd let Ruby do more of the work, especially the iterating through the
> items. Something like this might be suitable:
>
> class Person
> attr_accessor :title, :forename, :surname
> def name
> [title, forename, surname].compact.map {|s| s.strip }.join(" ")
> end
> end
>
>
> David
>

Hmm, an hours work and a five line implementation replaced by one line a
matter of minutes... I have a long way to go

Thanks for your help.



 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      05-05-2005
Ok Ok I admit it! I forgot the squeeze!
To reiterate:
"#{title} #{forename} #{surname}".strip.squeeze

On 5/5/05, Dominik Schlütter <schlu-> wrote:
> Hi,
>
> Logan Capaldo <> wrote:
>
> > "#{title} #{forename} #{surname}".strip

>
> But that would leave double spaces in the middle if 'forename' was
> empty, wouldn't it?
>
> Regards,
>
> Dominik.
>
>




 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      05-06-2005
Hi --

On Fri, 6 May 2005, Logan Capaldo wrote:

> Ok Ok I admit it! I forgot the squeeze!
> To reiterate:
> "#{title} #{forename} #{surname}".strip.squeeze


It's got to be squeeze(" "). Otherwise:

" Mrs. Emma Peel ".strip.squeeze
=> "Mrs. Ema Pel"


David

--
David A. Black



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[nuby] shell-like substitution in a string Lionel Thiry Ruby 5 10-14-2004 10:51 PM
Nuby - help on string spliting Dany Cayouette Ruby 3 09-30-2004 10:00 PM
nuby question: question marks in method names Edwin Eyan Moragas Ruby 0 08-30-2004 07:11 AM
nuby question: f.rename(x,y) does not work Boris \BXS\ Schulz Ruby 4 01-03-2004 04:19 PM
nuby question re a method Van Jacques Ruby 0 12-10-2003 02:47 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57