Learning Ruby Ruby Code Blocks. Code blocks in Ruby are not like “blocks of code” in other programming languages Blocks are typically associated with.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

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.
MWD1001 Website Production Using JavaScript with Forms.
Functions Prototypes, parameter passing, return values, activation frams.
1. What number does the following array represent?
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 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 -
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
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/
Lecture 12 Oct 13, 2008 Some simple recursive programs recursive problem solving, connection to induction Some examples involving recursion Announcements.
Run time vs. Compile time
CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i
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.
Презентація за розділом “Гумористичні твори”
Центр атестації педагогічних працівників 2014
Галактики і квазари.
Характеристика ІНДІЇ.
Процюк Н.В. вчитель початкових класів Боярської ЗОШ І – ІІІ ст №4
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Iterators CS 367 – Introduction to Data Structures.
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.
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
Lists and More About Strings CS303E: Elements of Computers and Programming.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Ruby and the tools 740Tools05ClassesObjectsVars Topics Ruby Classes Objects Variables Containers Blocks Iterators Spring 2014 CSCE 740 Software Engineering.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
PHP Constructs Advance Database Management Systems Lab no.3.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Духовні символи Голосіївського району
Views of Data Data – nouns of programming world the objects that are manipulated information that is processed Humans like to group information Classes,
1 Design Patterns Delegates Visitor Pattern. 2 Observer Pattern Observer Pattern is an object-oriented design that simulates void-pointers in for instance.
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
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.
Functional Processing of Collections (Advanced) 6.0.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
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)
Scope History of Ruby. Where can you use Ruby? General Features.
C Language By Sra Sontisirikit
CMSC 330: Organization of Programming Languages
Classes, Objects, And Variables
Ruby Testing 2, 11/9/2004.
Проф. д-р Васил Цанов, Институт за икономически изследвания при БАН
ЗУТ ПРОЕКТ на Закон за изменение и допълнение на ЗУТ
О Б Щ И Н А С И Л И С Т Р А П р о е к т Б ю д ж е т г.
Електронни услуги на НАП
Боряна Георгиева – директор на
РАЙОНЕН СЪД - БУРГАС РАБОТНА СРЕЩА СЪС СЪДЕБНИТЕ ЗАСЕДАТЕЛИ ПРИ РАЙОНЕН СЪД – БУРГАС 21 ОКТОМВРИ 2016 г.
Сътрудничество между полицията и другите специалисти в България
Съобщение Ръководството на НУ “Христо Ботев“ – гр. Елин Пелин
НАЦИОНАЛНА АГЕНЦИЯ ЗА ПРИХОДИТЕ
ДОБРОВОЛЕН РЕЗЕРВ НА ВЪОРЪЖЕНИТЕ СИЛИ НА РЕПУБЛИКА БЪЛГАРИЯ
Съвременни софтуерни решения
ПО ПЧЕЛАРСТВО ЗА ТРИГОДИШНИЯ
от проучване на общественото мнение,
Васил Големански Ноември, 2006
Програма за развитие на селските райони
ОПЕРАТИВНА ПРОГРАМА “АДМИНИСТРАТИВЕН КАПАЦИТЕТ”
БАЛИСТИКА НА ТЯЛО ПРИ СВОБОДНО ПАДАНЕ В ЗЕМНАТА АТМОСФЕРА
МЕДИЦИНСКИ УНИВЕРСИТЕТ – ПЛЕВЕН
Стратегия за развитие на клъстера 2015
Моето наследствено призвание
Правна кантора “Джингов, Гугински, Кючуков & Величков”
Безопасност на движението
Functions.
Presentation transcript:

Learning Ruby Ruby Code Blocks

Code blocks in Ruby are not like “blocks of code” in other programming languages Blocks are typically associated with method calls Blocks can implement "iterators“ Blocks can be passed around from method to method

3.times { print "Ho! " } 1.upto( 5 ) { |i| puts i } my_a.each { |element| puts element if element.length > 4 } classic_rock.each { | song, artist | puts "#{artist} performs '#{song}'." } 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

[ 'a', 'e', 'i', 'o', 'u' ].each { |ch| puts ch } ( 'f'..'t' ).each { |ch| print ch } p [2, 3, 5, 7, 11].find_all { |prime| prime*prime > 30 }  [7, 11] returns an array containing all elements for which block is true p calls the inspect method (printing arrays nicely) kids = %w( joseph aaron aideen ) %w is non-interpolated array of words, separated by whitespace kids.each { |child| puts “Child is #{child}\n" } More Block Examples