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
Chapter 2.2 – More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld senior.
Advertisements

ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
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 Objects and Classes
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Chapter 5 Functions.
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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Learning Ruby - 7 Methods. def addem ( first, second ) first + second end # of addem. addem( 23, 6 ) def addemall ( first, *rest ) rest.each { |r| first.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
Introduction to Python
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.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Introduction to Methods
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Java Unit 9: Arrays Declaring and Processing Arrays.
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.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
David Stotts Computer Science Department UNC Chapel Hill.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
1 Programming Week 2 2 Inheritance Basic Java Language Section.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
I Power Higher Computing Software Development High Level Language Constructs.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Python Let’s get started!.
Controlling Program Flow with Decision Structures.
A High Flying Overview CS139 – Fall 2006 How far we have come.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.
Objects, If, Routines. Creating Objects an object is a fundamental software element that has a state and a set of behaviors. Ex: bank account or student.
Sequences and for loops. Simple for loops A for loop is used to do something with every element of a sequence scala> for (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.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
CS-104 Final Exam Review Victor Norman.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 5 - Functions Outline 5.1 Introduction
CS 106A, Lecture 7 Parameters and Return
Functions BIS1523 – Lecture 17.
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Type & Typeclass Syntax in function
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Fundamentals of Functional Programming
CISC101 Reminders Assignment 3 due today.
Class code for pythonroom.com cchsp2cs
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_with_index { | song, where | puts "#{song} performs '#{where}'." } 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,13,17,19].find_all { |prime| prime*prime > 30 }  [7, 11, 13,17, 19] 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

Dictionary Moment in·ter·po·late –verb (used with object) 1. to introduce (something additional or extraneous) between other things or parts; interject; interpose; intercalate. 2. Mathematics. to insert, estimate, or find an intermediate term in (a sequence). 3. to alter (a text) by the insertion of new matter, esp. deceptively or without authorization. 4. to insert (new or spurious matter) in this manner.

Ruby Control Flow

If else end puts 'I am a fortune-teller. Tell me your name:' name = gets.chomp # OR name= gets # name.strip! if name == 'Chris' puts 'I see great things in your future.' else puts 'Your future is... Oh my! Look at the time!' puts 'I really have to go, sorry!' end

While loops command = ‘ ‘ while command != 'bye' puts command command = gets.chomp end puts 'Come again soon!'

Methods We’ve seen several methods so far: puts, gets, chomp, to_s, and even +, -, * and / Usually use dot notation is really just a shortcut way of writing ‘pit ’ * 5

String methods lineWidth = 50 puts('Old Mother Hubbard'.center(lineWidth)) lineWidth = 40 str = '--> text <--' puts str.ljust lineWidth #parens are optional

Random Numbers rand -> float greater than or equal to 0.0 and less than 1.0. rand(5) -> int in range 0..4 srand 1776 gives the random number generator a seed puts(Math::PI) #:: is scope resolution

Defining own methods def sayMoo numberOfMoos puts 'mooooooo...'*numberOfMoos end # Note local variables def doubleThis num numTimes2 = num*2 puts num.to_s+' doubled is '+numTimes2.to_s end doubleThis 44 puts numTimes2.to_s # ERROR undefined variable

Defining own methods def sayMoo numberOfMoos puts 'mooooooo...'*numberOfMoos end numTimes2 = 0 # defined def doubleThis num numTimes2 = num*2 # local scope puts num.to_s+' doubled is '+numTimes2.to_s end doubleThis 44 -> 44 doubled is 88 puts numTimes2.to_s -> 0

Makes local copy def littlePest var var = nil puts 'HAHA! I ruined your variable!' end var = 'You can\'t even touch my variable!‘ littlePest var puts var Local variables are local to the code construct in which they are declared. For example, a local variable declared in a method or within a loop cannot be accessed outside of that loop or method. Local variable names must begin with either an underscore or a lower case letter

defined? tells the scope arg = 5 numTimes2 = 0 def doubleThis num numTimes2 = num*2 p defined? num ->"local-variable" p defined? arg ->nil puts num.to_s+' doubled is '+numTimes2.to_s end

def addThem ( first, second ) first + second  The value of the last statement in the method is value returned, but you can also use “return” end # of addThem. puts addThem( 23, 6 )  29 def addThemAll ( first, *rest )  variable number of parameters tot=first rest.each{|val| tot +=val} tot  or return tot end p addThemAll( 1, 2, 3, 4, 5, 6, 7 )  28 rest is of type Array. The *parameter must be the last item Ruby Methods are Easy!

Scope Local variables (begin with either an underscore or a lower case letter) and are local to the code construct in which they are declared. For example, a local variable declared in a method or within a loop cannot be accessed outside of that loop or method. Global variables (begin with $) are accessible from anywhere in the Ruby program, regardless of where they are declared. A class variable (begins with is shared amongst all instances of a class. Instance variables (begin are similar to Class variables except that their values are local to specific instances of an object.

Blocks Scope - closures sum = 0 (1..10).each {|i| sum +=i} p sum -> 55 # available as known before (1..10).each {|i| last=i} p last -> undefined local variable or method ‘last’

def yup yield end yup {puts "where is the beef?"} Yield – calls provided block yield: calls the provided block where is the beef?

At Seats: what is output? def test msg = "OF COURSE“ <- local variable to test puts "You are in the method" yield puts "You are again back to the method" yield end msg = "HOPEFULLY“ <- different local variable test {puts "You are in the block" + msg}

Output - Notice Scope You are in the method You are in the blockHOPEFULLY You are again back to the method You are in the blockHOPEFULLY

Yield – reusing code (block can have parameters) def triple(max) yield 3*max end triple(8) { |x| print x} ->24

Yield – reusing code what is output? def cubes(max) i=1 while i < max yield i**3 i += 1 end cubes(8) { |x| print x, ", "} sum = 0 cubes(8) { |y| sum += y} print "\nsum=",sum product = 1 cubes(8) { |z| product *= z} print "\nproduct=",product Scope of block passed is calling block not called block. sum is NOT known inside cubes but is known inside code block.

Yield – reusing code (output) def cubes(max) i=1 while i < max yield i**3 i += 1 end cubes(8) { |x| print x, ", "} sum = 0 cubes(8) { |y| sum += y} print "\nsum=",sum product = 1 cubes(8) { |z| product *= z} print "\nproduct=",product 1, 8, 27, 64, 125, 216, 343, sum=784 product= Scope of block passed is calling block not called block. sum is NOT known inside cubes but is known inside code block.

def compute( what ) if block_given? yield( what ) else what end # of if. end # of compute puts compute( 22 ) puts compute ( 11 ) { |num| num*num } puts compute (4 ) { |num| num+num } Yield block_given? is built-in Return value is last thing executed

Implicitly passing and invoking blocks def foo(*args) yield(args.join(' ')) end foo('oscar', 'Meyer', ‘wiener'){|name| puts "Hello #{name}"} -> Hello oscar Meyer wiener

Explicitly passing, binding and invoking blocks def foo(*args, &blk) blk.call(args.join(' ')) end foo('oscar', 'Meyer', ‘wiener'){|name| puts "Hello #{name}"} ->Hello oscar Meyer wiener The & binds the block to the variable blk making it available as a Proc object.Proc

Explicitly passing, binding and invoking blocks via a block variable def foo(*args) blk = args.delete_at(-1) blk.call(args.join(' ')) end the_block = lambda {|name| puts "Hello #{name}"} foo('oscar', 'Meyer', ' wiener ',the_block) -> Hello oscar Meyer wiener

This is how each could be implemented via yield self – current class “this” class Array # Note how I can extend the functionality of built-in classes def my_each() 0.upto(length-1) do |i| yield self[i]  call to provided code with my ith element end return self  if want to use in assign statement or nest calls end puts "Array\n" t = ["quit", "felt", "worry", "smith", "white", "abandon", "fish", "this", "oboe"] t.my_each{|i| print i, "\n" } p t.my_each{|i| print "The element is ", i, "\n" }.sort! -> needs my_each to return the array

What does this do? class NumericSequences def fib(limit) yield 0, 0 yield 1, 1 prev = 0 curr = 1 i=2 while (i <= limit) new = prev + curr yield i, new prev = curr curr = new i = i+1 end g = NumericSequences.new -> Creates instance of class g.fib(10) {|i,x| print "The #{i}th number: #{x}\n"}

Using yield to print Fibonacci numbers class NumericSequences def fib(limit) yield 0, 0 yield 1, 1 prev = 0 curr = 1 i=2 while (i <= limit) new = prev + curr yield i, new prev = curr curr = new i = i+1 end g = NumericSequences.new g.fib(10) {|i,x| print "The #{i}th Fibonacci number: #{x}\n"} The 0th Fibonacci number: 0 The 1th Fibonacci number: 1 The 2th Fibonacci number: 1 The 3th Fibonacci number: 2 The 4th Fibonacci number: 3 The 5th Fibonacci number: 5 The 6th Fibonacci number: 8 The 7th Fibonacci number: 13 The 8th Fibonacci number: 21 The 9th Fibonacci number: 34 The 10th Fibonacci number: 55  allows two arguments to be used

def give_back ( a, *b ) return a end # of give_back. p give_back( 10 ) p give_back( 10, 11 ) def give_back2 ( a, *b ) return a, b end first, rest = give_back2( 1, 2, 3, 4, 5 ) Return multiple values

Creating a class class Die def roll 1 + rand(6) end end # Let's make a couple of dice... dice = [Die.new, Die.new] #...and roll them. dice.each do |die| puts die.roll end Better to have a variable to store the current value

class Die def initialize #When an object is created, #its initialize method (if it has one defined) is always called. # I'll just roll the die roll end def = 1 + rand(6) end def end die = Die.new die.roll puts die.showing

class Die attr_reader :numberShowing attr_writer :timesRolled def initialize = 0 roll end = 1 + rand(6) end def end def printIt puts "Die " + " times rolled:" end die = Die.new puts die.getValue puts die.numberShowing # access local variables die.timesRolled = 5 die.printIt -> Die 6 times rolled:5

Closure – method plus scope The ability to take a block of code (code in between do and end), wrap it up in an object (called a proc), store it in a variable or pass it to a method, and run the code in the block whenever you feel like. So it's kind of like a method itself, except that it isn't bound to an object (it is an object), and you can store it or pass it around like you can with any object msg = "NICE " toast = Proc.new do puts 'Cheers!' + msg end def doit toast msg = "yes" toast.call end doit toast Cheers!NICE

Procs with arguments doYouLike = Proc.new do |aGoodThing| puts 'I *really* like '+aGoodThing+'!' end doYouLike.call 'chocolate' doYouLike.call 'ruby‘ # the call is required? Why not just use methods? Well, it's because there are some things you just can't do with methods. In particular, you can't pass methods into other methods (but you can pass procs into methods), and methods can't return other methods (but they can return procs). This is simply because procs are objects; methods aren't.

def doSelfImportantly someProc puts 'Everybody just HOLD ON! I have something to do...' someProc.call puts 'Ok everyone, I\'m done. Go on with what you were doing.' end sayHello = Proc.new do puts 'hello' end sayGoodbye = Proc.new do puts 'goodbye' end doSelfImportantly sayHello doSelfImportantly sayGoodbye

Passing a Class def create_from_factory(factory) factory.new end obj = create_from_factory(Array)

Try at seats "99 bottles of beer on the wall..." Write a program which prints out the lyrics to that beloved classic, that field-trip favorite: "99 Bottles of Beer on the Wall.“

Extending built-in classes – truly object oriented class Integer def isodd odd= self%2==1 end p 10.isodd ->false class Integer def is_perfect_square? root =Math.sqrt(self).to_i #puts root.to_s root*root == self end puts 10.is_perfect_square? puts 9.is_perfect_square?