Ruby Containers, Iterators, and Blocks

Slides:



Advertisements
Similar presentations
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
Advertisements

The Ruby Programming Language
Introduction to Ruby CS 480/680 – Comparative Languages.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Methods and Hashes.
Built-in Data Structures in Python An Introduction.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Introduction to python programming
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
Test 2 Review Outline.
Review 1.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
Variables Variables are used to store data or information.
CS 371 Web Application Programming
Python Loops and Iteration
Working with Collections of Data
Topics Introduction to Repetition Structures
CSc 110, Autumn 2016 Lecture 27: Sets and Dictionaries
Racket CSC270 Pepper major portions credited to
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
JavaScript Syntax and Semantics
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Classes, Objects, And Variables
While Loops in Python.
Python - Loops and Iteration
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Introduction to Python
Hash table another data structure for implementing a map or a set
CS313D: Advanced Programming Language
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
COSC 1323 – Computer Science Concepts I
Introduction to Python
Python Primer 2: Functions and Control Flow
Concepts From Alice Switching to Java Copyright © Curt Hill.
CSc 110, Spring 2018 Lecture 33: Dictionaries
Hash Tables.
Lecture 10 List Richard Gesick.
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Chapter 18-3 Recursion Dale/Weems.
CIT 383: Administrative Scripting
CSCI 3333 Data Structures Array
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Introduction to Python: Day Two
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Python Primer 1: Types and Operators
A QUICK START TO OPL IBM ILOG OPL V6.3 > Starting Kit >
How to use hash tables to solve olympiad problems
CIT 383: Administrative Scripting
CHAPTER 21 LOOPS 1.
Hash Tables By JJ Shepherd.
PHP an introduction.
Conditional Loops Counted Loops
Insertion Sort and Shell Sort
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
Code Blocks, Closures, and Continuations
CSE 3302 Programming Languages
Dasar-Dasar Pemrograman 2: Java Basics
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
Introduction to Python
Selamat Datang di “Programming Essentials in Python”
Introduction to Java Collection
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Ruby Containers, Iterators, and Blocks CSE 413 Autumn 2008 Ruby Containers, Iterators, and Blocks

Containers in Ruby Ruby has general, easy-to-use container classes, like most scripting languages Two major kinds Arrays: ordered by position Hashes: collections of <key, value> pairs Often known as associative arrays, maps, or dictionaries Unordered

Ruby Arrays Instances of class Array Create with an array literal, or Array.new words = [ “how”, “now”, “brown”, “cow” ] stuff = [ “thing”, 413, nil ] seq = Array.new Indexed with [ ] operator, 0-origin; negative indices count from right words[0] stuff[2] words[-2] seq[1] = “something”

Ruby Hashes Instances of class Hash Create with an hash literal, or Hash.new pets = { “spot” => “dog”, “puff” => “cat” } tbl = Array.new Indexed with [ ] operator pets[“puff”] pets[“fido”] pets[“cheeta”] = “monkey” (Can use almost anything as key type; can use anything as element type)

Containers and Iterators All containers respond to the message “each”, executing a block of code for each item in the container words.each { puts “another word” } words.each { | w | puts w }

Blocks A block is a sequence of statements surrounded by { … } or do … end Blocks must appear immediately following the method call that executes them, on the same line Blocks may have 1 or more parameters at the beginning surrounded by | … | Initialized by the method that runs the block

Blocks as Closures Blocks can access variables in surrounding scopes all_words words.each { | w | all_words = all_words + w + “ ” } These are almost, but not quite, first-class closures as in Scheme (some differences in scope rules)

More Block Uses Besides iterating through containers, blocks are used in many other contexts 3.times { puts “hello” } n = 0 100.times { | k | n += k } puts “sum of 0 + … + 99 is ” + n We’ll see more examples of blocks as well as how to write code that uses them later