regular expression. newbie problem.

  • Thread starter Johnathan Smith
  • Start date
J

Johnathan Smith

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
 
R

Reacher

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

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

Johnathan Smith

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
 
L

Lee Jarvis

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
 
W

William James

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


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
 
L

Lee Jarvis

Lee said:
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
 
J

Johnathan Smith

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
 
L

Lee Jarvis

Johnathan said:
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
 
J

Johnathan Smith

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$
 
S

Sebastian Hungerecker

Johnathan said:
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?
 
J

Johnathan Smith

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

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
 
L

Lee Jarvis

Johnathan said:
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

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
 
S

Sebastian Hungerecker

Johnathan said:
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 Johnson")
NameError: uninitialized constant Name
from (irb):2

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


HTH,
Sebastian
 
P

Peter Bunyan

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
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top