[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Method definitions and Line numbers

Brian Takita

1/21/2006 12:37:00 AM

Hello,

How does Ruby store where methods are defined?
For example, where does Ruby look to get file names and line numbers in the
stack trace?

--
Thank you,
Brian Takita
http://weblog.freeo...
5 Answers

Logan Capaldo

1/21/2006 3:06:00 AM

0


On Jan 20, 2006, at 7:37 PM, Brian Takita wrote:

> Hello,
>
> How does Ruby store where methods are defined?
> For example, where does Ruby look to get file names and line
> numbers in the
> stack trace?
>
> --
> Thank you,
> Brian Takita
> http://weblog.freeo...

Check out the __FILE__ and __LINE__ constants.


Brian Takita

1/21/2006 5:32:00 AM

0

>
> Check out the __FILE__ and __LINE__ constants.


I apologize for not being clear.

Lets say we have a class Test that raises an error.

class Test
def method_with_an_error
raise Error
end
end

t = Test.new
t.method_with_an_error

Now the following error message is outputted in the command line.

/home/btakita/src/scratch3.rb:3:in `method_with_an_error': Exception
(Exception)
from /home/btakita/src/scratch3.rb:8

How does Ruby keep track where :method_with_an_error is stored?
Can I make a query as to where :method_with_an_error is defined?

--
Thank you,
Brian Takita
http://weblog.freeo...

Brian Takita

1/21/2006 5:35:00 AM

0

I meant to paste this code:

class Test
def method_with_an_error
raise Exception
end
end

t = Test.new
t.method_with_an_error

Gene Tani

1/21/2006 8:27:00 AM

0


Brian Takita wrote:
> >
> > Check out the __FILE__ and __LINE__ constants.
>
>
> I apologize for not being clear.
>
> Lets say we have a class Test that raises an error.
>
> class Test
> def method_with_an_error
> raise Error
> end
> end
>
> t = Test.new
> t.method_with_an_error
>
> Now the following error message is outputted in the command line.
>
> /home/btakita/src/scratch3.rb:3:in `method_with_an_error': Exception
> (Exception)
> from /home/btakita/src/scratch3.rb:8
>
> How does Ruby keep track where :method_with_an_error is stored?
> Can I make a query as to where :method_with_an_error is defined?
>
> --
> Thank you,
> Brian Takita
> http://weblog.freeo...

Exception#backtrace is what you want

also can access the stack in:

Kernel#caller
Kernel#set_trace_func

Tracer lib (std lib)

Robert Klemme

1/21/2006 1:16:00 PM

0

2006/1/21, Brian Takita <brian.takita@gmail.com>:
> >
> > Check out the __FILE__ and __LINE__ constants.
>
>
> I apologize for not being clear.
>
> Lets say we have a class Test that raises an error.
>
> class Test
> def method_with_an_error
> raise Error
> end
> end
>
> t = Test.new
> t.method_with_an_error
>
> Now the following error message is outputted in the command line.
>
> /home/btakita/src/scratch3.rb:3:in `method_with_an_error': Exception
> (Exception)
> from /home/btakita/src/scratch3.rb:8
>
> How does Ruby keep track where :method_with_an_error is stored?
> Can I make a query as to where :method_with_an_error is defined?

You mean without it raising an excepption? AFAIK there is no other
way than searching sources for the definition. You can also use $: to
check directories in the load path.

Wait, there might be one other option: use set_trace_func at the
beginning of your script (before the first require) and record all
class and method definitions.

Kind regards

robert

--
Have a look: http://www.flickr.com/photos/fu...