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
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
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
···
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
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?
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.
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
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
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
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:
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
···
--
NP: Anathema - Alternative 4
Jabber: sepp2k@jabber.org
ICQ: 205544826
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?