[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

puts a . which method of array is called

Raj Singh

6/25/2008 6:50:00 PM

a = w(1 2 3)


puts a

prints
1
2
3


I implemented
class Array
def to_s
'to_s is called'
end
def to_str
'to_str is called'
end
end


I am not able to find when I do 'puts a' which method of a is called. I
also tried inspect and to_ary method but doesn't seem to work.


Also I would like to know how to go about finding which method is called
without looking at the c file. Is there some kind of trace I can do to
detect which method was called?
--
Posted via http://www.ruby-....

2 Answers

Daniel Brumbaugh Keeney

6/25/2008 10:08:00 PM

0

On Wed, Jun 25, 2008 at 1:50 PM, Raj Singh <neeraj.jsr@gmail.com> wrote:
> a = w(1 2 3)
>
> puts a

> I implemented
> class Array
> def to_s
> 'to_s is called'
> end
> def to_str
> 'to_str is called'
> end
> end

I'm afraid you're barking up the wrong tree. In the IO#puts documentation,
" If called with an array argument, writes each element on a new line."
It could look something like this:
def puts(*objects)
objects.each do |obj|
if obj.kind_of? Array
puts(*obj)
else
write(obj.to_s + $/
end
end
end

> Also I would like to know how to go about finding which method is called
> without looking at the c file. Is there some kind of trace I can do to
> detect which method was called?

ruby includes a library named tracer.
http://unroller.ruby...
Or you can write one yourself; Kernel#set_trace_func is really what a
tracing library reduces to.

Daniel Brumbaugh Keeney

I have provided my own home-grown tracer below, which, I'm afraid,
isn't much to speak of.

README

Quartzhouse is a tool for tracing Ruby programs so you can more quickly
familiarize yourself with the program flow. It's output is simple,
merely a long list of lines looking something like this:
RubyLex.lex_int2 at /usr/lib/ruby/1.8/irb/ruby-lex.rb:533

In fact, that's most all there is to QuartzHouse. Frankly, QuartzHouse
is really just Kernel.set_trace_func all spiffed up. It has nice
feature where you can call QuartzHouse.set_ignorables and any modules
already loaded will be ignored, but is otherwised unadorned.

PLEASE NOTE:

You should almost always redirect the output of QuartzHouse. By
default, it sends output to $stderr. Any reasonably complicated
program will have hundreds of lines of output.

EXAMPLES:

If I wanted to trace irb, I would do it like this:

ruby -wrquartzhouse -e'QuartzHouse.set_ignorables;require "irb";
QuartzHouse.start_trace;IRB.start' 2>irb_trace

This traces the runtime method calls of IRB (only those caused by the
IRB.start). It then redirects the output into a file `irb_trace', since
the default output is to $stderr. This can be changed by
Quartzhouse.output = IO_LIKE_OBJ
where IO_LIKE_OBJ can be anything that responds to `write'. Addional
filters can be placed by Quartzhouse.ignorables << a_module

I often filter output like this:
ruby -wrquartzhouse ... 2>1 1>/dev/null | grep something | uniq

#!/usr/bin/env ruby
# Author:: Daniel Brumbaugh Keeney (http://rubyforge.org/use...)
# Copyright:: 2008 Daniel Brumbaugh Keeney
# License:: GPLv3+
#
# This file is part of QuartzHouse.
#
# QuartzHouse is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# QuartzHouse is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with QuartzHouse. If not, see <http://www.gnu.org/lic....

require 'tempfile'
require 'cattr'

TOPLEVEL_OBJECT = self

module QuartzHouse

TRACKING_PROC = proc do |event, file, line, id, binding, klass|

if event=="call" or event=="c-call" or
event==:call or event==:"c-call"

unless @@ignorables.include? klass

@@storage.puts "#{klass}.#{id} at #{file}:#{line}"

end
end
end

# Remove any ignored modules, delete any recorded trace, and set
# output to $stderr.
def QuartzHouse.reset
@@storage.close(true) rescue false
@@ignorables = Array.new
@@output = $stderr
@@storage = Tempfile.new "quartzhouse_tmp"
nil
end

# Look through the ObjectSpace for any modules and adds them all to
# the ignore list
def QuartzHouse.set_ignorables
ObjectSpace.each_object(Module) do |obj|
@@ignorables << obj
end
@@ignorables << ENV << ARGV << ARGF << TOPLEVEL_OBJECT
end

# Use io.write to output the full trace record and close trace file.
def QuartzHouse.print_trace io = @@output
@@storage.close
@@storage.open
while line = @@storage.gets
io.write line
end
@@storage.close
nil
end

# Begin tracing, ignoring all the ignorables.
def QuartzHouse.start_trace
at_exit do
set_trace_func nil
print_trace $stderr
end
set_trace_func TRACKING_PROC
nil
end

cattr_accessor :output, :ignorables

QuartzHouse.reset

end

Raj Singh

6/26/2008 1:20:00 PM

0

Thanks. However I am getting error at

require 'cattr'

What is cattr and where can I get it?
--
Posted via http://www.ruby-....