Regular expression. newbie problem

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

On Dec 6, 9:42 am, Johnathan S. [email protected] wrote:

expression to use and whether my class is looking correct in general
@fname = name.slice!(rexesp)

def firstinitial
return @finitial
end

def firstinitial
return @sinitial
end

Posted viahttp://www.ruby-forum.com/.

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.’

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

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

Lee J. 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

On Dec 6, 9:42 am, Johnathan S. [email protected] wrote:

expression to use and whether my class is looking correct in general
@fname = name.slice!(rexesp)

def firstinitial
return @finitial
end

def firstinitial
return @sinitial
end

Posted viahttp://www.ruby-forum.com/.

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

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

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$

Johnathan S. 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

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 J.son”)
NameError: uninitialized constant Name
from (irb):2

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

Johnathan S. 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?

Johnathan S. wrote:

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 J.son”)
NameError: uninitialized constant Name
from (irb):2

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

You need to require the file with the Name class in it.
But in this case.

Copy the code I gave you, (the class and the main code)
open up a shell and cd to the directory of the script and run:

~$ ruby name.rb

Regards,
Lee

working

sorry

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 J.son”)
NameError: uninitialized constant Name
from (irb):2

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$

Would I be a terrible person if I laughed at these?

Johnathan S. wrote:

irb(main):001:0> name.rb
NameError: undefined local variable or method `name’ for main:Object
from (irb):1

You enter ruby code in irb. name.rb is not valid ruby code (well, it
could be
if there’s an object name with a method rb, but in this case there
isn’t) -
it’s a file name. You could either write valid ruby code containing this
filename (like require “name.rb” which would load the file and run the
code
inside it) or execute name.rb from the shell (by typing “./name.rb” or
“ruby
name.rb”, not by typing in ruby code). Assuming of course that the file
exists and contains valid ruby code.

irb(main):002:0> a = Name.new(“John Matthew J.son”)
NameError: uninitialized constant Name
from (irb):2

You have not previously defined Name (at least not in the current irb
session).

HTH,
Sebastian

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs