Learning Ruby - 4 Ruby Code Blocks. Pay Attention - This is important! Code blocks in Ruby are not like “blocks of code” in other programming languages.

Slides:



Advertisements
Similar presentations
LOOPS Loops are lines of code that can be run more than one time. Each time is called an iteration and in for loops there is also an index that is changing.
Advertisements

Program Looping EE2372 Software Design I Dr. Gerardo Rosiles.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Order Analysis of Algorithms Debdeep Mukhopadhyay IIT Madras.
3.1.3 Program Flow control_1 Understand the need for structure Breaking things down.
The method of program compaction for real-time applications Ruslan L. Smeliansky Lomonosov Moscow State University Faculty of Computational Mathematics.
Preliminaries Attendance sheets –I remembered! HW1 due tonight –progress report Stage 1 due on Friday –progress report.
Learning Ruby Ruby Code Blocks. Code blocks in Ruby are not like “blocks of code” in other programming languages Blocks are typically associated with.
Learning Ruby Ruby Code Blocks. Code blocks in Ruby are not like “blocks of code” in other programming languages Blocks are typically associated with.
Learning Ruby Methods. We’ve seen several so far: puts, gets, chomp, to_s, and even +, -, * and / Usually use dot notation is really just a shortcut.
CSE341: Programming Languages Lecture 20 Blocks & Procs; Inheritance & Overriding Dan Grossman Fall 2011.
Learning Ruby Ruby Hashes. Hashes Hashes introduce a new accessing method – similar to arrays but indexing is by arbitrary keys of any type. Not magic.
Learning Ruby - 2 Ruby Arrays and Lists. Ruby Arrays are Cool! We've already seen a Ruby array – song_lines Ruby arrays shrink and grow dynamically -
Learning Ruby - 5 Files. while line = gets puts line end while line = gets puts line.downcase end while line = gets puts line.downcase if line =~ /UP/
How to load a program file? Lisp programs in Allegro are saved under the file extension.cl. To load a file into the Lisp console, use the following: (load.
Learning Ruby - 3 Ruby Hashes. Guess What? Ruby hashes are cool! Unlike arrays, which associate object references with numbered indices, hashes associate.
CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i
Learning Ruby - 14 Grabbag IV - the fourth installment of the Trilogy.
The fourth programming assignment J.-F. Pâris Fall 2014.
1.Define zoning. 2.Define building codes. 3.List the key players in implementing CPTED. 4.List the two thresholds of review for new construction or redevelopment.
CS1101: Programming Methodology Aaron Tan.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
PYTHON PROGRAMMING Week 8 - Wednesday. WHAT DO THESE DO? for x in range(5): print x for x in range(1,5): print x for x in range(1,5): print x**2 numbers.
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
A brief introduction to 11ACM 朱旻申. Outline What is Groovy ? Differences between Groovy & Java About closure & Dagger Methods.
Introduction of Engineering (ENGR 10) Guest Lecture March 24, 2014.
Ruby and the tools 740Tools05ClassesObjectsVars Topics Ruby Classes Objects Variables Containers Blocks Iterators Spring 2014 CSCE 740 Software Engineering.
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
Topic 25 - more array algorithms 1 "To excel in Java, or any computer language, you want to build skill in both the "large" and "small". By "large" I mean.
Data Structure & Algorithm Lecture 1 – Selection & Insertion Sort JJCAO Steal some from Prof. Yoram Moses.
18-1 Chapter 18 Themes for Class Discussion Measuring and Delivering Marketing Performance Copyright © 2008 by The McGraw-Hill Companies, Inc. All rights.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
And other languages….  Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros.
Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Critical Analysis: Print media Ligia Hernandez C&I 5472 Ligia Hernandez C&I 5472.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Loops CS 103 February 13, 2009 Author: Nate Hamm.
Tuples Chapter 10 Python for Informatics: Exploring Information
ASP.NET User Controls. User Controls In addition to using Web server controls in your ASP.NET Web pages, you can create your own custom, reusable controls.
1 Design Patterns Delegates Visitor Pattern. 2 Observer Pattern Observer Pattern is an object-oriented design that simulates void-pointers in for instance.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
Be “GUI ready” developing in RPG by Robert Arce from PrismaTech. Be “GUI ready” developing in RPG-ILE Presented by: Robert Arce.
How does the author use text features to help the reader better understand animal traits and how animals can be classified?
Lists in Python Lists as Arguments/Parameters. Lists as arguments to functions Just like other data types, lists can be sent to functions as arguments.
Chapter 5 Hashes and databases. File contains more than numbers NameScore Johnny8.65 Juan9.12 Joseph8.45 Stacey7.81 Aideen8.05 Zack7.21 Aaron8.31.
Functional Processing of Collections (Advanced) 6.0.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Function Objects and Comparators
What to do today: Brief history of Ruby General qualities/info
Unit 3: Lesson 9-Looping and Random Numbers
Iteration Implemented through loop constructs (e.g., in C++)
Event loops 17-Jan-19.
1 Hour Training Bubbles Selling Skills Model
Generic Programming with Templates:
Introduction to Computer Science
Print the following triangle, using nested loops
Journal – Pick ONE What makes a “good” friend versus a “bad” friend? Think about: the qualities you look for in a friend and what you expect a friend.
Topic 25 - more array algorithms
Code Blocks, Closures, and Continuations
Have you ever listened to a song that you wanted to hear over and over? Have you ever heard a song that caught your attention and you wanted to hear it.
Location Of The Online Examples
Presentation transcript:

Learning Ruby - 4 Ruby Code Blocks

Pay Attention - This is important! Code blocks in Ruby are not like “blocks of code” in other programming languages Blocks can implement "callbacks" and "iterators" Blocks can be passed around from method to method Blocks are typically associated with method calls

3.times { print "Ho! " } 1.upto( 5 ) { |i| puts i } my_a.each { |element| puts element if element.length > 4 } classic_rock.collect do | song, artist | puts "#{artist} performs '#{song}'." end # of do. puts songs.collect { | song, artist | "#{artist} performs '#{song}'." }.sort Block Examples Seen So Far

How Code Blocks Work The code within {.. } or do.. end is passed to the method (as if it were another parameter) The method then arranges to call the block of code as required It is possible to write custom methods that take advantage of this Ruby feature The "yield" method will call an associated code block from within a method Ruby code blocks are no more evident than when used as iterators

ENV.collect do | key, value | print key + ' -> ' + value + "\n" end # of do. [ 'a', 'e', 'i', 'o', 'u' ].each { |ch| puts ch } ( 'f'..'t' ).each { |ch| print ch } [2, 3, 5, 7, 11].find { |prime| prime*prime > 30 } kids = %w( joseph aaron aideen ) kids.each { |child| puts "#{child}\n" } More Block Examples

More... Ruby So Far Code blocks are cool! Code blocks are used to implement iterators Take the time to understand how they work Before using while (or any other looping construct), ask yourself if an iterating code block will do instead