Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3068

Re: To Yield or Not to Yield: An Inferable Question

From Brian Candler <b.candler@pobox.com>
Newsgroups comp.lang.ruby
Subject Re: To Yield or Not to Yield: An Inferable Question
Date 2011-04-17 15:22 -0500
Organization Service de news de lacave.net
Message-ID <c0656e03bb64f14a6b7f3ac7a3ab69e5@ruby-forum.com> (permalink)
References <CA1B8DEB-7979-4D1B-8594-3BA3C72AE76F@carboni.ca>

Show all headers | View raw


Points I'd raise:

1. In my experience, very little real-world Ruby code uses 
'block_given?'. If it needs to yield, it just yields. I'd consider this 
to be a case of duck-typing.

With yield you get a run-time error if no block was passed, but that's 
only one of a much larger set of method call errors (such as calling a 
method with argument of the wrong type).

Consider also that very little code tests 'a.respond_to? :foo' before 
calling 'a.foo'.

2. If a method uses &blk or Proc.new or yield, I'd say it's fairly safe 
to assume that the block *may* be called (at least from the point of 
view of automated documentation). Since it's unprovable in general even 
whether the method returns or not, it seems like hard work (for little 
benefit) to try to decide whether a method which accepts a block *never* 
actually calls it.

3. As you're undoubtedly aware, Ruby is so dynamic that you can't 
analyse a method in isolation anyway. You can decide that a bareword 
like 'foo' is a method call, but you don't know what that method will 
actually do when the program is run - it could be redefined dynamically, 
either within a class or on single objects (in their singleton class).

# in file one
class Foo
  def foo
    true
  end
  def bar
    yield 123 if foo   # yields, obviously
  end
end

# in file two
a = Foo.new
def a.foo; false; end
a.bar { |x| puts "I got #{x}" }   # actually it doesn't

That's an admittedly contrived example, but dynamic method definition 
occurs quite a lot in real applications, e.g. web frameworks like Rails.

Regards,

Brian.

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[ANN] To Yield or Not to Yield: An Inferable Question Michael Edgar <adgar@carboni.ca> - 2011-04-14 12:47 -0500
  Re: To Yield or Not to Yield: An Inferable Question Brian Candler <b.candler@pobox.com> - 2011-04-17 15:22 -0500
    Re: To Yield or Not to Yield: An Inferable Question Michael Edgar <adgar@carboni.ca> - 2011-04-17 16:40 -0500
      Re: To Yield or Not to Yield: An Inferable Question Ryan Davis <ryand-ruby@zenspider.com> - 2011-04-18 03:41 -0500
      Re: To Yield or Not to Yield: An Inferable Question Brian Candler <b.candler@pobox.com> - 2011-04-18 10:53 -0500
      Re: To Yield or Not to Yield: An Inferable Question Brian Candler <b.candler@pobox.com> - 2011-04-20 03:59 -0500
        Re: To Yield or Not to Yield: An Inferable Question Michael Edgar <adgar@carboni.ca> - 2011-04-20 16:46 -0500
          Re: To Yield or Not to Yield: An Inferable Question Brian Candler <b.candler@pobox.com> - 2011-04-21 04:47 -0500
            Re: To Yield or Not to Yield: An Inferable Question Robert Klemme <shortcutter@googlemail.com> - 2011-04-21 06:18 -0500
              Re: To Yield or Not to Yield: An Inferable Question Brian Candler <b.candler@pobox.com> - 2011-04-21 12:55 -0500
  Re: [ANN] To Yield or Not to Yield: An Inferable Question Robert Klemme <shortcutter@googlemail.com> - 2011-04-18 04:34 -0500
    Re: [ANN] To Yield or Not to Yield: An Inferable Question Michael Edgar <adgar@carboni.ca> - 2011-04-18 04:46 -0500
      Re: [ANN] To Yield or Not to Yield: An Inferable Question Robert Klemme <shortcutter@googlemail.com> - 2011-04-18 06:16 -0500
        Re: [ANN] To Yield or Not to Yield: An Inferable Question Michael Edgar <adgar@carboni.ca> - 2011-04-18 07:26 -0500

csiph-web