# New to Ruby...help needed

**URL:** <https://www.ruby-forum.com/t/new-to-ruby-help-needed/148658>\
**Category:** Ruby\
**Created:** [September 16, 2008, 12:22am UTC](https://www.ruby-forum.com/t/new-to-ruby-help-needed/148658 "2008-09-16T00:22:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pip9ball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@pip9ball](https://www.ruby-forum.com/u/pip9ball)\
**Post date:** [September 16, 2008, 12:22am UTC](https://www.ruby-forum.com/t/new-to-ruby-help-needed/148658/1 "2008-09-16T00:22:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pip9ball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@pip9ball](https://www.ruby-forum.com/u/pip9ball)\
**Post date:** [September 16, 2008, 1:15am UTC](https://www.ruby-forum.com/t/new-to-ruby-help-needed/148658/2 "2008-09-16T01:15:28Z")

</div>

Phillip Prentice wrote:

> x = SingleInst.new(“TEST\<5\>”)  
> def name  
> @name = name  
> end  
> ##Something here  
> 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
