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

> some_list.each do |this_item| puts this_time end

> for this_item in some_list: print(this_time)

Just a nitpick:

for one liners you can write:

some_list.each { |this_time| puts this_time }



If each element of some_list responds to to_s with something sensible, which it would need to in order for puts to work in the first place, you can just write:

   puts some_list
This works as puts implicitly does the equivalent of calling Array() on its arguments and then calls `to_s` on each one.

`some_list.each ...` is somewhat more generic, as `puts some_list` requires `some_list` to respond to `to_ary` for it to work as expected, which is probably less common than implementing 'each'. (if you implement a collection class in Ruby, at least implement `each` and `to_ary`)




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

Search: