# Called\_from 0.1.0 released

**URL:** <https://www.ruby-forum.com/t/called-from-0-1-0-released/178651>\
**Category:** Ruby\
**Created:** [November 9, 2009, 12:25am UTC](https://www.ruby-forum.com/t/called-from-0-1-0-released/178651 "2009-11-09T00:25:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![makoto\_kuwata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@makoto\_kuwata](https://www.ruby-forum.com/u/makoto_kuwata)\
**Post date:** [November 9, 2009, 12:25am UTC](https://www.ruby-forum.com/t/called-from-0-1-0-released/178651/1 "2009-11-09T00:25:42Z")

</div>

I releaded called\_from 0.1.0.

> **[GitHub - kwatch/called\_from: alternatives of caller()\[0\] =~ /(.\*):(\\d+)( in...](https://github.com/kwatch/called_from)**
>
> alternatives of caller()\[0\] =~ /(.\*):(\\d+)( in \`(.\*)')?/ - GitHub - kwatch/called\_from: alternatives of caller()\[0\] =~ /(.\*):(\\d+)( in \`(.\*)')?/

Kernel#caller() is very useful, but very slow.  
‘called\_from’ solves this problem.

## About

‘called\_from’ provides called\_from() global function  
which gets filename and line number of caller.

In short:

```
require 'called_from'
filename, linenum, function = called_from(1)

```

is equivarent to:

```
caller(1)[0] =~ /:(\d+)( in `(.*)')?/
filename, linenum, function = $`, $1, $2

```

But called\_from() is much faster than caller()[0].

## Example

```
require 'called_from'

##
## report not only what sql is queried but also from where query()

```

is called.  
## (this is very convenient for development and debugging)  
## in such examle, speed of called\_from() is very important.  
##  
def query(sql)  
filename, linenum, function = called\_from()  
log.debug(“#{Time.now}: #{sql} (called from #{filename}:#  
{linenum})”)  
return connection.query(sql)  
end

```
##
## if cache data is older than template file's timestamp,
## refresh cache data.
## it is very important that called_from() is much faster than

```

caller()[0]  
## because speed is necessary for caching.  
##  
def cache\_helper(cache\_key)  
## check template file’s timestamp  
template\_filename, linenum, function = called\_from()  
timestamp = File.mtime(template\_filename)  
## if cache data is newer than template’s timestamp, return data  
data = @cache.get(cache\_key, :timestamp=\>timestamp)  
return data if data  
## if cache data is older than template’s timestamp, refresh it  
data = yield()  
@cache.set(cache\_key, data)  
return data  
end

## Benchmark

Here is an example result of benchmark on Merb application.  
This shows that called\_from() is more than 30 times faster than caller  
()[0].

```
*** n=100000
                             user system total

```

real  
caller()[0] 7.920000 0.400000 8.320000  
( 8.985343)  
caller()[0] (\*1) 8.590000 0.420000 9.010000  
( 9.804065)  
called\_from() 0.240000 0.010000 0.250000  
( 0.257151)  
called\_from() (\*2) 0.250000 0.000000 0.250000  
( 0.268603)

```
(*1) retrieve filename and line number using pattern match (=~ /:

```

(\d+)/)  
(\*2) retrieve filename and line number

## Installation

```
$ sudo gem install called_from

```

Or

```
$ tar xzf called_from_X.X.X.tar.gz
$ cd called_from_X.X.X/
$ ruby setup.rb config
$ ruby setup.rb setup
$ sudo ruby setup.rb install

```

Notice: called\_from is implemented in both C-extention and pure Ruby.  
C-extention is available on UNIX-like OS and Ruby 1.8.x.

---

<div class="post-metadata">

**Author:** ![makoto\_kuwata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@makoto\_kuwata](https://www.ruby-forum.com/u/makoto_kuwata)\
**Post date:** [November 9, 2009, 12:29am UTC](https://www.ruby-forum.com/t/called-from-0-1-0-released/178651/2 "2009-11-09T00:29:48Z")

</div>

Hi, this is really interesting, but:

$ gem install called\_from  
Building native extensions. This could take a while…  
/Users/tony/.rvm/jruby-1.3.1/lib/ruby/1.8/mkmf.rb:7: JRuby does not  
support  
native extensions. Check [wiki.jruby.org](http://wiki.jruby.org) for alternatives.  
(NotImplementedError)

We are in the process of moving our codebase onto JRuby. It’s nice you  
have  
a pure Ruby version available but not if I can’t install the gem on  
JRuby.

---

<div class="post-metadata">

**Author:** ![makoto\_kuwata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@makoto\_kuwata](https://www.ruby-forum.com/u/makoto_kuwata)\
**Post date:** [November 9, 2009, 1:51am UTC](https://www.ruby-forum.com/t/called-from-0-1-0-released/178651/3 "2009-11-09T01:51:44Z")

</div>

This is probably the most common usage of Kernel#caller. You’ve made  
it more useful and much faster – great extension!

jeremy

---

<div class="post-metadata">

**Author:** ![makoto\_kuwata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@makoto\_kuwata](https://www.ruby-forum.com/u/makoto_kuwata)\
**Post date:** [November 9, 2009, 11:12pm UTC](https://www.ruby-forum.com/t/called-from-0-1-0-released/178651/4 "2009-11-09T23:12:35Z")

</div>

I’ve long wished that caller returned an array of BacktraceLine…  
likewise  
Exception#backtrace.

class BacktraceLine  
attr\_reader :line, :file, :method, :arguments  
end

Not sure if it’s a daydream or an REP…

Judson

---

<div class="post-metadata">

**Author:** ![makoto\_kuwata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@makoto\_kuwata](https://www.ruby-forum.com/u/makoto_kuwata)\
**Post date:** [November 10, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/called-from-0-1-0-released/178651/5 "2009-11-10T17:06:35Z")

</div>

On Nov 8, 6:25 pm, makoto kuwata [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I releaded called\_from 0.1.0.[GitHub - kwatch/called\_from: alternatives of caller()[0] =~ /(.\*):(\d+)( in `(.\*)')?/](http://github.com/kwatch/called_from)
> 
> Kernel#caller() is very useful, but very slow.  
> ‘called\_from’ solves this problem.

Have you submitted this to Ruby core?

Personally I hate the name (b/c it ends in a preposition), but  
functionally it seems like an excellent good addition to the core  
language.

---

<div class="post-metadata">

**Author:** ![makoto\_kuwata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@makoto\_kuwata](https://www.ruby-forum.com/u/makoto_kuwata)\
**Post date:** [November 10, 2009, 4:59pm UTC](https://www.ruby-forum.com/t/called-from-0-1-0-released/178651/6 "2009-11-10T16:59:19Z")

</div>

> We are in the process of moving our codebase onto JRuby. It’s nice you  
> have  
> a pure Ruby version available but not if I can’t install the gem on  
> JRuby.

Also fails on 1.9. I know there was a commit recently to allow  
Thread.list.map &:backtrace (can’t seem to find it right now).  
so that might help.  
Thanks!  
-r
