[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

regular expression. newbie problem.

Johnathan Smith

12/6/2007 3:43:00 PM

Hi,

i know this is a pretty basic problem but im a newbie so any help would
be greatly appreciated

im trying to write a name class one which returns a first name a
surname, first and second initials

however, i realise i regular expression is needed to go through the name
to produce the correct output. however im unsure of whaty regular
expression to use and whether my class is looking correct in general

anyhelp would be greatly appreciated

thanks

name class:

class Name

def initialize(name)
@fname = name.slice!(rexesp)
@sname = name.slice!(regexp)
@mname = name.slice!(regexp)

def firstname
return @fname
end

def surname
return @sname
end

def firstinitial
return @finitial
end

def firstinitial
return @sinitial
end
--
Posted via http://www.ruby-....

14 Answers

Reacher

12/6/2007 3:49:00 PM

0

On Dec 6, 9:42 am, Johnathan Smith <stu...@hotmail.com> wrote:
> Hi,
>
> i know this is a pretty basic problem but im a newbie so any help would
> be greatly appreciated
>
> im trying to write a name class one which returns a first name a
> surname, first and second initials
>
> however, i realise i regular expression is needed to go through the name
> to produce the correct output. however im unsure of whaty regular
> expression to use and whether my class is looking correct in general
>
> anyhelp would be greatly appreciated
>
> thanks
>
> name class:
>
> class Name
>
> def initialize(name)
> @fname = name.slice!(rexesp)
> @sname = name.slice!(regexp)
> @mname = name.slice!(regexp)
>
> def firstname
> return @fname
> end
>
> def surname
> return @sname
> end
>
> def firstinitial
> return @finitial
> end
>
> def firstinitial
> return @sinitial
> end
> --
> Posted viahttp://www.ruby-....

You can achieve this with ease by better utilizing Ruby's string
functionality
From irb:
>> first_name = 'Osmosis'
'Osmosis'
>> last_name = 'Jones'
'Jones'
>> full_name = [first_name, last_name].join(' ')
'Osmosis Jones'
>> short_name = [first_name, last_name.first].join(' ')
'Osmosis J'
>> initials = [(first_name.first + '.'), (last_name.first + '.').join(' ')
'O. J.'

Johnathan Smith

12/6/2007 3:58:00 PM

0

thank you very much for your help
and yes i realise this can be achieved but i plan to use this class in
conjections with others i have already written

would you be able to provide any help with the orignal problem?

thanks
--
Posted via http://www.ruby-....

Lee Jarvis

12/6/2007 4:04:00 PM

0

class Name
def initialize(name)
@fname, @mname, @sname = name.split
end
attr_reader :fname, :mname, :sname
def initials
"#{fname[0].chr}.#{mname[0].chr}.#{sname[0].chr}"
end
end

a = Name.new("Lee John Jarvis")
p a.fname #=> 'Lee'
p a.mname #=> 'John'
p a.sname #=> 'Jarvis'
p a.initials #=> 'L.J.J'

Although you probably should use some regexp to handle extra ordinary
middle names.

Or am I well off?

Regards,
Lee
--
Posted via http://www.ruby-....

William James

12/6/2007 4:04:00 PM

0

On Dec 6, 9:42 am, Johnathan Smith <stu...@hotmail.com> wrote:
> Hi,
>
> i know this is a pretty basic problem but im a newbie so any help would
> be greatly appreciated
>
> im trying to write a name class one which returns a first name a
> surname, first and second initials
>
> however, i realise i regular expression is needed to go through the name
> to produce the correct output. however im unsure of whaty regular
> expression to use and whether my class is looking correct in general
>
> anyhelp would be greatly appreciated
>
> thanks
>
> name class:
>
> class Name
>
> def initialize(name)
> @fname = name.slice!(rexesp)
> @sname = name.slice!(regexp)
> @mname = name.slice!(regexp)
>
> def firstname
> return @fname
> end
>
> def surname
> return @sname
> end
>
> def firstinitial
> return @finitial
> end
>
> def firstinitial
> return @sinitial
> end
> --
> Posted viahttp://www.ruby-....


p "Chidiock Tichborne".strip.match( /^(\S+)( +\S.*)? +(\S+)$/ ).to_a
p "Edgar A. Poe".strip.match( /^(\S+)( +\S.*)? +(\S+)$/ ).to_a
p "J. R. R. Tolkien".strip.match( /^(\S+)( +\S.*)? +(\S+)$/ ).to_a
_,first,middle,sur = "Ambrose Bierce".
strip.match( /^(\S+)( +\S.*)? +(\S+)$/ ).to_a
puts "#{ first }:#{ middle }:#{ sur }"

---- output ----
["Chidiock Tichborne", "Chidiock", nil, "Tichborne"]
["Edgar A. Poe", "Edgar", " A.", "Poe"]
["J. R. R. Tolkien", "J.", " R. R.", "Tolkien"]
Ambrose::Bierce

Lee Jarvis

12/6/2007 4:07:00 PM

0

Lee Jarvis wrote:
> Regards,
> Lee


Sorry, I didn't realize you didn't want middle names aswell, I kinda
thought that was what @mname was for originally..

As for the class, you don't end the initialize method or the class
itself.

And you redefine the 'firstinitial' method

Using attribute readers gets rid of the need for methods like this:

def foo
return @bar
end


Regards,
Lee
--
Posted via http://www.ruby-....

Johnathan Smith

12/6/2007 4:20:00 PM

0

> a = Name.new("Lee John Jarvis")
> p a.fname #=> 'Lee'
> p a.mname #=> 'John'
> p a.sname #=> 'Jarvis'
> p a.initials #=> 'L.J.J'
>

hi,

the class looks good

although im getting errors with the output

im entering: a = Name.new("Johnathan Micheal Smith")

but im getting an error which says

syntax error near unexpected token `('

any idea why

thanks
--
Posted via http://www.ruby-....

Lee Jarvis

12/6/2007 4:23:00 PM

0

Johnathan Smith wrote:
> any idea why
>
> thanks

class Name
def initialize(name)
@fname, @mname, @sname = name.split
end
attr_reader :fname, :mname, :sname
def initials
"#{fname[0].chr}.#{mname[0].chr}.#{sname[0].chr}"
end
end

a = Name.new("Johnathan Micheal Smith")
p a.fname
p a.mname
p a.sname
p a.initials

Works for me..

c0re:~$ ruby test.rb
"Johnathan"
"Micheal"
"Smith"
"J.M.S"


Regards,
Lee
--
Posted via http://www.ruby-....

Johnathan Smith

12/6/2007 4:33:00 PM

0

hmm still being unsuccessful

im using linux and have name.rb stored in a file called Ruby

can you see if im inputing anything wrong

sh-3.2$ cd Desktop
sh-3.2$ cd Ruby
sh-3.2$ a = Name.new("Stuart Richard Little")
sh: syntax error near unexpected token `('
sh-3.2$
--
Posted via http://www.ruby-....

Sebastian Hungerecker

12/6/2007 4:37:00 PM

0

Johnathan Smith wrote:
> sh-3.2$ cd Desktop
> sh-3.2$ cd Ruby
> sh-3.2$ a = Name.new("Stuart Richard Little")
> sh: syntax error near unexpected token `('
> sh-3.2$

You are aware that you are trying to input ruby code into a unix shell, yes?


--
NP: Graveworm - Scars Of Sorrow
Jabber: sepp2k@jabber.org
ICQ: 205544826

Johnathan Smith

12/6/2007 4:39:00 PM

0

> You are aware that you are trying to input ruby code into a unix shell,
> yes?

yes
but then i tried in irb too but still having problems

h-3.2$ irb
irb(main):001:0> name.rb
NameError: undefined local variable or method `name' for main:Object
from (irb):1
irb(main):002:0> a = Name.new("John Matthew Johnson")
NameError: uninitialized constant Name
from (irb):2

i know im probably being very thick here but i appreciate the help

--
Posted via http://www.ruby-....