On Thu, 30 Jul 2009 17:57:48 -0400, Luis Zarrabeitia wrote:
> As I understood the question, it was "was wrong in 'for var in
> container' in comparison with ruby's container.each?"
> 
> What's the (semantic) difference between
> 
> for localVar in container:
>     block
> 
> and
> 
> container.each{|localVar| block}

I don't think "each" is a particularly compelling example of Ruby's 
blocks - as you say, it's easily replaceable with a Python for-loop. The 
advantage of Ruby's syntax is that it allows you to define further 
functions that are sort-of like new control structures. So you have 
things like:

File.open('myfile', 'r') do |file|
        while line = file.gets
                puts line
        end
end

Which is like a python's:

with open('myfile', 'r) as f:
        for line in f:
                print f

However, with Ruby, this kind of scoping construct can be added without 
adding new syntax. Another interesting example is the Sinatra web 
framework, which allows you to write a web app with something like:

get '/' do
        "Index page"
end

post '/:name' do 
        "Posted to " + params[:name]
end

Which looks something like a DSL for web apps, but is accomplished solely 
with normal ruby functions. More: http://www.sinatrarb.com/intro.html

Ruby blocks are, fundamentally, syntactic sugar, but they are kind of 
neat.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to