<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: A better try &#8212; chaining methods and nil</title>
	<link>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/</link>
	<description>Ruby.send(:stop, 'messing around')</description>
	<pubDate>Thu, 24 Jul 2008 14:31:15 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>

	<item>
		<title>By: 프로그래밍 루비 &#187; Blog Archive &#187; 짧은 코드 한 줄이 주는 영감 - try() 놀이</title>
		<link>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-457</link>
		<author>프로그래밍 루비 &#187; Blog Archive &#187; 짧은 코드 한 줄이 주는 영감 - try() 놀이</author>
		<pubDate>Fri, 14 Mar 2008 08:45:45 +0000</pubDate>
		<guid>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-457</guid>
		<description>[...] 풀려던 문제(@person ? @person.name : nil)와 답이 틀렸다는 주장도 있다. try라는 이름에 걸맞을려면 respond_to?보다는 객체가 닐인지만 [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] 풀려던 문제(@person ? @person.name : nil)와 답이 틀렸다는 주장도 있다. try라는 이름에 걸맞을려면 respond_to?보다는 객체가 닐인지만 [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: A better &#8220;try()&#8221; for Ruby, why not do the Groovy way? &#124; Urubatan&#8217;s Weblog</title>
		<link>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-444</link>
		<author>A better &#8220;try()&#8221; for Ruby, why not do the Groovy way? &#124; Urubatan&#8217;s Weblog</author>
		<pubDate>Wed, 05 Mar 2008 14:35:55 +0000</pubDate>
		<guid>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-444</guid>
		<description>[...] new method in Ruby 1.9 is making some people happy and creative too. The only problem with that in my opinion is that every one is fixed [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] new method in Ruby 1.9 is making some people happy and creative too. The only problem with that in my opinion is that every one is fixed [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: coderrr</title>
		<link>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-430</link>
		<author>coderrr</author>
		<pubDate>Tue, 04 Mar 2008 05:08:39 +0000</pubDate>
		<guid>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-430</guid>
		<description>have you seen: http://coderrr.wordpress.com/2007/09/15/the-ternary-destroyer/ ?</description>
		<content:encoded><![CDATA[<p>have you seen: <a href="http://coderrr.wordpress.com/2007/09/15/the-ternary-destroyer/" rel="nofollow">http://coderrr.wordpress.com/2007/09/15/the-ternary-destroyer/</a> ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reg Braithwaite</title>
		<link>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-429</link>
		<author>Reg Braithwaite</author>
		<pubDate>Tue, 04 Mar 2008 01:22:31 +0000</pubDate>
		<guid>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-429</guid>
		<description>I'm flattered you say #andand gets it right... I think it (and kernel#ergo, an almost identical implementation done before I wrote #andand) are good for their use case, but I do think there might be something to try()... maybe an Object#please with #andand-like semantics.

So (expr).andand.foo says "do foo if the expression is not nil) and (expr).please.foo says "do foo if you handle foo." I need to look and see what else people are doing along those lines. In a general sense, I worry about two things:

1. if you use #respond_to? you break when people do method_missing magic. I try to avoid method_missing magic for that reason, it's hard to use reflection. But you have to write for the code people actuallyt use, and;
2. if you handle NoMethodError, you catch your receiver's NoMethodErrors, but also any errors raised if your receiver is trying to handle something and THAT raises an error. That is definitely not the semantics I have in mind!

So... It's all dancing with bears. It's lovely circus-stuff, but you're in a heap of trouble if your partner steps on your toes :-)

Thanks again for your post.</description>
		<content:encoded><![CDATA[<p>I&#8217;m flattered you say #andand gets it right&#8230; I think it (and kernel#ergo, an almost identical implementation done before I wrote #andand) are good for their use case, but I do think there might be something to try()&#8230; maybe an Object#please with #andand-like semantics.</p>
<p>So (expr).andand.foo says &#8220;do foo if the expression is not nil) and (expr).please.foo says &#8220;do foo if you handle foo.&#8221; I need to look and see what else people are doing along those lines. In a general sense, I worry about two things:</p>
<p>1. if you use #respond_to? you break when people do method_missing magic. I try to avoid method_missing magic for that reason, it&#8217;s hard to use reflection. But you have to write for the code people actuallyt use, and;<br />
2. if you handle NoMethodError, you catch your receiver&#8217;s NoMethodErrors, but also any errors raised if your receiver is trying to handle something and THAT raises an error. That is definitely not the semantics I have in mind!</p>
<p>So&#8230; It&#8217;s all dancing with bears. It&#8217;s lovely circus-stuff, but you&#8217;re in a heap of trouble if your partner steps on your toes :-)</p>
<p>Thanks again for your post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Shea</title>
		<link>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-428</link>
		<author>Chris Shea</author>
		<pubDate>Tue, 04 Mar 2008 00:07:38 +0000</pubDate>
		<guid>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-428</guid>
		<description>@Reg: You're right. It should handle arguments and blocks. It's other glaring deficiency is that it lets you get around a method's privateness. Object#andand gets it done right.</description>
		<content:encoded><![CDATA[<p>@Reg: You&#8217;re right. It should handle arguments and blocks. It&#8217;s other glaring deficiency is that it lets you get around a method&#8217;s privateness. Object#andand gets it done right.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reg Braithwaite</title>
		<link>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-427</link>
		<author>Reg Braithwaite</author>
		<pubDate>Mon, 03 Mar 2008 23:15:20 +0000</pubDate>
		<guid>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-427</guid>
		<description>Even though your opening the Object class is destroying Ruby, I like your better try :-)

Here's something to think about: the code you are quoting doesn't handle parameters or blocks. So the obvious fix is to capture an optional block and optional parameters. This will allow:

@some_value.try(:a_method, 42) { &#124;foo&#124; ... }

Have a look at #send_with_default for an example of someone thinking along those lines:

http://rhnh.net/2007/10/2/object-send_with_default

There are links to a bunch of other approaches to this problem at the bottom of my own post explaining #andand:

http://weblog.raganwald.com/2008/01/objectandand-objectme-in-ruby.html

I am adding your post to the list. Thanks for taking the time to share!</description>
		<content:encoded><![CDATA[<p>Even though your opening the Object class is destroying Ruby, I like your better try :-)</p>
<p>Here&#8217;s something to think about: the code you are quoting doesn&#8217;t handle parameters or blocks. So the obvious fix is to capture an optional block and optional parameters. This will allow:</p>
<p>@some_value.try(:a_method, 42) { |foo| &#8230; }</p>
<p>Have a look at #send_with_default for an example of someone thinking along those lines:</p>
<p><a href="http://rhnh.net/2007/10/2/object-send_with_default" rel="nofollow">http://rhnh.net/2007/10/2/object-send_with_default</a></p>
<p>There are links to a bunch of other approaches to this problem at the bottom of my own post explaining #andand:</p>
<p><a href="http://weblog.raganwald.com/2008/01/objectandand-objectme-in-ruby.html" rel="nofollow">http://weblog.raganwald.com/2008/01/objectandand-objectme-in-ruby.html</a></p>
<p>I am adding your post to the list. Thanks for taking the time to share!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arya</title>
		<link>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-426</link>
		<author>Arya</author>
		<pubDate>Mon, 03 Mar 2008 23:03:29 +0000</pubDate>
		<guid>http://ruby.tie-rack.org/53/a-better-try-chaining-methods-and-nil/#comment-426</guid>
		<description>Interesting approach. I definitely like your way better than do_or_do_not simply because it isn't rescuing all exceptions with nil (that just seems like a bad idea). Also, I appreciate the fact that you continue to call the method if it's not nil even if the object does not respond to that method AND that you call self.send except that it doesn't make a difference (at least I think so, but I may be wrong). Any method defined in global scope exists in every object:

def foo
  puts "foo"
end
Object.new.send(:foo)

But in my opinion, you are right to call the method on the object if it's not nil and does not respond to the method:

@some_value = "Hello World
....
@some_value.try(:uniq)  # uniq being the method for an Array and not a String

If someone calls the wrong method on an object they're expecting to be an array, they have a bug and they should be notified of it via an exception, not a silent fail.</description>
		<content:encoded><![CDATA[<p>Interesting approach. I definitely like your way better than do_or_do_not simply because it isn&#8217;t rescuing all exceptions with nil (that just seems like a bad idea). Also, I appreciate the fact that you continue to call the method if it&#8217;s not nil even if the object does not respond to that method AND that you call self.send except that it doesn&#8217;t make a difference (at least I think so, but I may be wrong). Any method defined in global scope exists in every object:</p>
<p>def foo<br />
  puts &#8220;foo&#8221;<br />
end<br />
Object.new.send(:foo)</p>
<p>But in my opinion, you are right to call the method on the object if it&#8217;s not nil and does not respond to the method:</p>
<p>@some_value = &#8220;Hello World<br />
&#8230;.<br />
@some_value.try(:uniq)  # uniq being the method for an Array and not a String</p>
<p>If someone calls the wrong method on an object they&#8217;re expecting to be an array, they have a bug and they should be notified of it via an exception, not a silent fail.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
