Method definitions and Line numbers

B

Brian Takita

------=_Part_3374_12632521.1137803831508
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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.freeopinion.org

------=_Part_3374_12632521.1137803831508--
 
L

Logan Capaldo

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?

Check out the __FILE__ and __LINE__ constants.
 
B

Brian Takita

------=_Part_4807_3036472.1137821535399
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
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 =3D 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.freeopinion.org

------=_Part_4807_3036472.1137821535399--
 
B

Brian Takita

------=_Part_4819_21449552.1137821714435
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I meant to paste this code:

class Test
def method_with_an_error
raise Exception
end
end

t =3D Test.new
t.method_with_an_error

------=_Part_4819_21449552.1137821714435--
 
G

Gene Tani

Brian said:
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?

Exception#backtrace is what you want

also can access the stack in:

Kernel#caller
Kernel#set_trace_func

Tracer lib (std lib)
 
R

Robert Klemme

2006/1/21 said:
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 =3D 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
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top