Languages

Fsharp
ActionScript
xBase
Clean
GPSS
PureBasic
Sieve
Erlang
JOVIAL
Mercury
Linda
DataFlex
PostScript
FoxPro2
VFP
Cobol
Prolog
Jython
Awk
VisualBasic
JavaScript
Matlab
ASP
Haskell
Csharp
D
Smalltalk
Nemerle
Pixilang
Java
SQL
Python
ObjectPascal
Ruby
Perl
Pascal
Assembler
PHP
C
Functions  Add function  Users  Registration  Enter   About  ASCII Table  Our helpers

Ruby-on-Rails


1 Iterators in Ruby

2 7.5 Writing an Iterator Over a Data Structure

3 7.6 Changing the Way an Object Iterates

4 7.7 Writing Block Methods That Classify or Collect

Iterators in Ruby



(Page 1 of 4 )

In this second part of a three-part article, we continue our discussion of code blocks and begin focusing on iterators as well. It is excerpted from chapter eight of the Ruby Cookbook, written by Lucas Carlson and Leonard Richardson (O_Reilly, 2006; ISBN: 0596523696). Copyright © 2006 O_Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O_Reilly Media.

7.4 Blocks as Closures: Using Outside Variables Within a Code Block

Problem

You want to share variables between a method, and a code block defined within it.

Solution

Just reference the variables, and Ruby will do the right thing. Here’s a method that adds a certain number to every element of an array:

  def add_to_all(array, number)
    array.collect { |x| x + number }
  end

  add_to_all([1, 2, 3], 10)   # => [11, 12, 13]

Enumerable#collectcan’t accessnumberdirectly, but it’s passed a block that can access it, sincenumberwas in scope when the block was defined.

Discussion

A Ruby block is a closure: it carries around the context in which it was defined. This is useful because it lets you define a block as though it were part of your normal code, then tear it off and send it to a predefined piece of code for processing.

A Ruby block contains references to the variable bindings, not copies of the values. If the variable changes later, the block will have access to the new value:

  tax_percent = 6
  position = lambda do
   
"I have always supported a #{tax_percent}% tax on imported limes."
  end
  position.call
  # => "I have always supported a 6% tax on imported limes."

  tax_percent = 7.25
  position.call
  # => "I have always supported a 7.25% tax on imported limes."

This works both ways: you can rebind or modify a variable from within a block.

  counter = 0
  4.times { counter += 1; puts "Counter now #{counter}"}
  # Counter now 1
  # Counter now 2
  # Counter now 3
  # Counter now 4
  counter                            # => 4

This is especially useful when you want to simulateinjectorcollect in conjunction with a strange iterator. You can create a storage object outside the block, and add things to it from within the block. This code simulatesEnumerable#collect, but it collects the elements of an array in reverse order:

  accumulator = []
  [1, 2, 3].reverse_each { |x| accumulator << x + 1 }

  accumulator               # => [4, 3, 2]

Theaccumulatorvariable is not within the scope ofArray#reverse_each, but it is within the scope of the block.


1 2 3 4
Birth of a Hurricane

Image of a hurricane over the Caribbean between Cuba and Central America.

Summer storms are a regular feature in the North Atlantic, and while most pose little threat to our shores, a choice few become devastating hurricanes.

To decipher which storms could bring danger, and which will not, atmospheric scientists are heading to the tropics to observe these systems as they form and dissipate--or develop into hurricanes.

By learning to identify which weather systems are the most critical to track, the efforts may ultimately allow for earlier hurricane ...

More at http://www.nsf.gov/news/news_summ.jsp?cntn_id=117388&WT.mc_id=USNSF_51&WT.mc_ev=click


This is an NSF News item.

PycckaR
BepcuR


Articles
Articles


Library
Library


Downloads
Downloads

Google Chrome Golf 6
 © Internet, books, teachers and Rudevich Alexander brains.