[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

New to Ruby...help needed

Phillip Prentice

9/15/2008 10:15:00 PM

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
--
Posted via http://www.ruby-....

1 Answer

Henning Bekel

9/15/2008 11:10:00 PM

0

Phillip Prentice wrote:

> 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