# Re: Using strings to drill down into objects

**URL:** <https://www.ruby-forum.com/t/re-using-strings-to-drill-down-into-objects/117009>\
**Category:** Ruby\
**Created:** [October 12, 2007, 1:14am UTC](https://www.ruby-forum.com/t/re-using-strings-to-drill-down-into-objects/117009 "2007-10-12T01:14:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ross\_X\_Dawson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@Ross\_X\_Dawson](https://www.ruby-forum.com/u/Ross_X_Dawson)\
**Post date:** [October 12, 2007, 1:14am UTC](https://www.ruby-forum.com/t/re-using-strings-to-drill-down-into-objects/117009/1 "2007-10-12T01:14:40Z")

</div>

Sorry folks the example was not correct

irb(main):001:0\> drill\_down = lambda do |obj, method\_names|  
irb(main):002:1\* o ||= obj  
irb(main):003:1\> method\_names.split(’.’).each {|m| o = o.send(m)}  
irb(main):004:1\> o  
irb(main):005:1\> end  
=\> #Proc:0x40221e48@:1(irb)  
irb(main):006:0\> drill\_down.call(1,‘succ.to\_s’)  
=\> “2”  
irb(main):007:0\> drill\_down.call(1,‘succ.succ.succ.to\_s’)  
=\> “4”  
irb(main):008:0\>

This is what I currently have working. I’m using it to define the  
columns  
in a report so I need to determine the result of arbitrary fields within  
the object model. I just look at the code and think there is a better  
way.

* * *

This communication may contain CONFIDENTIAL information and may also be  
the subject of LEGAL PROFESSIONAL PRIVILEGE and/or under copyright. If  
you are not an intended recipient, you MUST NOT keep, forward, copy,  
use, save or rely on this communication and any such action is  
unauthorised and prohibited. If you have received this communication in  
error, please reply to this e-mail to notify the sender of its incorrect  
delivery, and then delete both it and your reply. Thank you

* * *

---

<div class="post-metadata">

**Author:** ![Ross\_X\_Dawson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@Ross\_X\_Dawson](https://www.ruby-forum.com/u/Ross_X_Dawson)\
**Post date:** [October 12, 2007, 2:00am UTC](https://www.ruby-forum.com/t/re-using-strings-to-drill-down-into-objects/117009/2 "2007-10-12T02:00:15Z")

</div>

Ross X Dawson wrote:

> irb(main):007:0\> drill\_down.call(1,‘succ.succ.succ.to\_s’)  
> =\> “4”  
> irb(main):008:0\>
> 
> This is what I currently have working. I’m using it to define the columns  
> in a report so I need to determine the result of arbitrary fields within  
> the object model. I just look at the code and think there is a better way.

OK, that makes more sense. Still, we don’t know what you have to start  
with (I guess it’s not simply 1 :), and how much flexibility you need in  
accessing various things about that thing.

For example, do you really need a lambda as your “drill down” function?  
One might think if it’s so flexible, a simple method would suffice.

Also, are you sure you have to traverse an object hierarchy by following  
arbitrarily specifiable attribute accessors in turn? It seems quite  
weird for a data model.

Finally, given that you already use lambdas, you could just use those to  
specify the way to access what you’re looking for, starting from a given  
object.

Just to keep some resemblance to your code (it could be made even  
simpler):

def drill\_down(obj, func)  
yield.call(obj)  
end

drill\_down(1, lambda{|o| o.succ.to\_s}) #=\> “2”  
drill\_down(1, lambda{|o| o.succ.succ.succ.to\_s}) #=\> “4”

And then you can simply use code blocks passed to the method call:

def drill\_down(obj)  
yield obj  
end

drill\_down(1) {|o| o.succ.to\_s} #=\> “2”  
drill\_down(1) {|o| o.succ.succ.succ.to\_s} #=\> “4”

If the method which calls drill\_down receives the lambdas in question as  
parameters, you can still pass them on as code blocks:

some\_lambda = lambda{|o| o.succ.to\_s}  
drill\_down(1, &some\_lambda) #=\> “2”

mortee

---

<div class="post-metadata">

**Author:** ![Ross\_X\_Dawson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@Ross\_X\_Dawson](https://www.ruby-forum.com/u/Ross_X_Dawson)\
**Post date:** [October 15, 2007, 12:12pm UTC](https://www.ruby-forum.com/t/re-using-strings-to-drill-down-into-objects/117009/3 "2007-10-15T12:12:49Z")

</div>

2007/10/12, Ross X Dawson [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> irb(main):001:0\> drill\_down = lambda do |obj, method\_names|  
> irb(main):002:1\* o ||= obj  
> irb(main):003:1\> method\_names.split(‘.’).each {|m| o = o.send(m)}  
> irb(main):004:1\> o  
> irb(main):005:1\> end  
> =\> #Proc:0x40221e48@:1(irb)  
> irb(main):006:0\> drill\_down.call(1,‘succ.to\_s’)  
> =\> “2”  
> irb(main):007:0\> drill\_down.call(1,‘succ.succ.succ.to\_s’)  
> =\> “4”

Ross, I would implement something like that with Enumerable#inject:

def drill\_down(obj, method\_names)  
method\_names.split(‘.’).inject(obj) {|o, m| o.send(m)}  
end

But I’m not quite sure that having strings with method calls is the  
way to go. Where do the strings with the method names come from? Do  
the users of your application have to enter them somewhere? What about  
arguments to the method calls?

Regards,  
Pit
