Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> That's not different than what PHP does now.

You could write code like that in PHP, and the GP post with the ruby example does so, but PHP generators do not work like that.

> I think you might be confused. I'm referring to the GP post that has the example with the yield call in it, the ruby example.

The Ruby example doesn't have the problem you mentioned because Ruby doesn't have generators at all. In Python and PHP, yield means "Suspend execution of this function and return a value to the caller, who can later call this function again to resume its execution." In Ruby, yield means "Call the block that was passed to this function with the values after yield," or exactly what I've written in Python without using the yield keyword. Ruby's "yeild line" is Python's "f(x)".

Edited: I think I understand what you mean after reading once more. If you mistook the function containing yield in Ruby for a function that does not contain yield you would still have a problem, but not the same problem you would have in PHP or Python. It would raise an exception, while PHP and Python's would just return an iterable.

  def each_line_in_file 
  ..   yield 1 
  .. end
  => nil
  each_line_in_file
  (eval):2: (eval):2:in `each_line_in_file': no block given (LocalJumpError)
      from (eval):3


> I think I understand what you mean after reading once more.

Thanks for taking the time to understand. My apologies for not being clear from the outset.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: