# Puts a . which method of array is called

**URL:** <https://www.ruby-forum.com/t/puts-a-which-method-of-array-is-called/141434>\
**Category:** Ruby\
**Created:** [June 25, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/puts-a-which-method-of-array-is-called/141434 "2008-06-25T20:52:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [June 25, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/puts-a-which-method-of-array-is-called/141434/1 "2008-06-25T20:52:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [June 26, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/puts-a-which-method-of-array-is-called/141434/2 "2008-06-26T15:22:02Z")

</div>

Thanks. However I am getting error at

require ‘cattr’

What is cattr and where can I get it?

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [June 26, 2008, 12:10am UTC](https://www.ruby-forum.com/t/puts-a-which-method-of-array-is-called/141434/3 "2008-06-26T00:10:06Z")

</div>

On Wed, Jun 25, 2008 at 1:50 PM, Raj S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.rubyforge.org/](http://unroller.rubyforge.org/)  
Or you can write one yourself; Kernel#set\_trace\_func is really what a  
tracing library reduces to.

Daniel Brumbaugh K.

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 K.

([http://rubyforge.org/users/db-keen](http://rubyforge.org/users/db-keen))

# Copyright:: 2008 Daniel Brumbaugh K.

# 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/licenses/](http://www.gnu.org/licenses/).

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
