P
Phillip Prentice
Hi,
I am in the process of teaching myself Ruby and so far its great,
however, I am coming from a procedural background and the concept of
objects/classes are a little difficult for me to put into action. Is it
possible to have your object execute code when it is first created?
Perhaps some sample code and an example of what I want is needed.
Here is my desired goal:
x = SingleInst.new("TEST<5>")
puts x.name returns 'TEST<5>"
puts x.start_index returns '5'
Here is my code:
class Sig
attr_accessor :name
def initialize(name)
@name = name
end
def name
return @name
end
def set_name(name)
@name = name
end
end
class Normal < Sig
attr_accessor :name, :type
def initialize(name)
@name = name
@type = "normal"
end
def to_s
@name + "\n" + \
@type + "\n"
end
def get_type
return @type
end
end
class SingleInst < Sig
attr_accessor :name, :type, :start_index
def initialize(name)
@name = name
@type = "SingleInst"
@start_index = ""
end
def set_start_index(name)
##Something here
end
def get_start_index
return @start_index
end
end
I want the SingleInst object to automatically parse the name and grab
the digit enclosed by '<>' and set to start_index when you created a new
SingleInst object. Is this possible or is there a better way to
accomplish this?
Thanks,
Phillip
I am in the process of teaching myself Ruby and so far its great,
however, I am coming from a procedural background and the concept of
objects/classes are a little difficult for me to put into action. Is it
possible to have your object execute code when it is first created?
Perhaps some sample code and an example of what I want is needed.
Here is my desired goal:
x = SingleInst.new("TEST<5>")
puts x.name returns 'TEST<5>"
puts x.start_index returns '5'
Here is my code:
class Sig
attr_accessor :name
def initialize(name)
@name = name
end
def name
return @name
end
def set_name(name)
@name = name
end
end
class Normal < Sig
attr_accessor :name, :type
def initialize(name)
@name = name
@type = "normal"
end
def to_s
@name + "\n" + \
@type + "\n"
end
def get_type
return @type
end
end
class SingleInst < Sig
attr_accessor :name, :type, :start_index
def initialize(name)
@name = name
@type = "SingleInst"
@start_index = ""
end
def set_start_index(name)
##Something here
end
def get_start_index
return @start_index
end
end
I want the SingleInst object to automatically parse the name and grab
the digit enclosed by '<>' and set to start_index when you created a new
SingleInst object. Is this possible or is there a better way to
accomplish this?
Thanks,
Phillip