New to Ruby...help needed

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
 
H

Henning Bekel

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

Just do anything you need to initialize the object in the initialize
function, that's what it's used for.

Here's a class that does what you described:

class SingleInst
attr_accessor :name, :start_index

def initialize(name)
@name = name
# get the digit using a regexp...
@name =~ /<(\d)>/
@start_index = $1.to_i
end
end

x = SingleInst.new("foo <5>")
puts x.start_index
puts x.name

You don't need to define setters and getters like that if you're using
attr_accessor.
I don't know what all that inheritance is supposed to do, put you should
probably read up on the super() method as well.

Henning
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top