Home > Programming > Ruby Programming



Ruby Programming CS 132A

Sort Desciption:

Somedude says By now you know that Ruby uses blocks for iteration. This is a core feature of Ruby. You remember that a block is some code between curly braces.



Content Inside:

Ruby Programming CS 132A Somedude says By now you know that Ruby uses blocks for iteration. This is a core feature of Ruby. You remember that a block is some code between curly braces. And that blocks can only be used as arguments to methods, like this. [1,2,3].each { |i| puts i } # 1 # 2 # 3 This is the bl ock You can use a "do" block when the block is more than one line. 99.times do |n|  if n % 2 == 0    puts n.to_s + " is even."  else    puts n.to_s + " is odd."  end end # 1 is odd. # 2 is even. . # 99 is odd. Blocks and Procs R uby provid es tw o synta x es for creating blocks. (1) The Do Block The {} block is commonly used when only a single line of code is required. 99.times { |t| puts "So far, #{t} times." } # So far, 1 times. # So far, 2 times. . # So far, 99 times. Blocks and Procs And the {} Block (2) The {} Block The bracket syntax has a higher precedence than the do.end syntax. Look at these examples. 1.upto 3 do |x|  puts x end # 1 # 2 # 3 1.upto 3 {|x| puts x } # SyntaxError: compile error Blocks and Procs W arning!!! W orks fine E rror!! What w ent wrong? Blocks and Procs 1.upto 3 {|x| puts x } # SyntaxError: compile error The curly brackets have a HIGHER PRECEDENCE than the do.end block, so the block attaches to the number 3 rather than to the upto() method! Somedude says What this means is that Ruby looked at the code and saw the method and the curly brackets with the 3 between them. The curly brackets had the higher precedence, so Ruby attached the block to the "3" instead of the "upto()". {} attaches HERE! The Solution . Use parentheses! Parentheses have the highest precedence. When theres any chance that your code may be confusing to Ruby (and yourself), use parentheses. 1.upto(3) {|x| puts x } # 1 # 2 # 3 Blocks and Procs ...

Source: norbert.ccsf.cc.ca.us


add to Google Reader add to Google Bookmark add to bloglines add to newsgator add to FURL add to digg add to webnews add to Netscape add to Yahoo MyWeb add to spurl.net add to diigo Bookmark newsvine Bookmark del.icio.us Bookmark @ SIMPIFY Bookmark MISTER WONG Bookmark Linkarena Bookmark icio.de Bookmark oneview Bookmark folkd.com Bookmark yigg.de Bookmark reddit Bookmark StumbleUpon Bookmark Slashdot Bookmark blinklist Bookmark technorati add to blogmarks add to blinkbits add to ma.gnolia add to smarking.com add to netvouz add to co.mments add to Connotea add to de.lirio.us
Search Terms:

 

Related Files

Ruby Course —animmersive programming course—

Filed under: Programming and Ruby Programming
Licence Copyrightc 2004-2006 Brian Schroeder Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1 ...

Programming in Ruby

Filed under: Programming and Ruby Programming
Programming in Ruby Dave Thomas Andy Hun t 1 September 13,2000 1 Dave and Andyare authors of Programming Ruby and The Pragmatic Programmer , both from Addison-Wesley.

A Little Ruby, A Lot of Objects

Filed under: Programming and Ruby Programming
... to use Ruby for general-purpose programming - and you should, since its a wonderful rapid- development language for many types of applications - the book to read is Programming Ruby ...

JRuby: Bringing Ruby to the Java Platform

Filed under: Programming and Ruby Programming
Sun Confidential: Internal Only 2 Agenda - Part One • Who Am I • The Ruby Programming Language • Walkthrough 1: Introduction to Ruby • Why Use Ruby? • JRuby:

Sockets programming in Ruby

Filed under: Programming and Ruby Programming
Sockets programming in Ruby Explore Rubysfundamental sockets interfaces for networking applications Skill Level: Intermediate M. Tim Jones ( mtj@mtjones.com ) Senior Principal ...