Ruby Methods, Procs, etc. CSCI400 21 September 2017 Ruby Methods, - - PowerPoint PPT Presentation

ruby methods procs etc
SMART_READER_LITE
LIVE PREVIEW

Ruby Methods, Procs, etc. CSCI400 21 September 2017 Ruby Methods, - - PowerPoint PPT Presentation

Method Arguments Ruby Methods, Procs, etc. CSCI400 21 September 2017 Ruby Methods, Procs, etc. Method Arguments Color Key Clickable URL link Write down an answer to this for class participation Just a comment dont confuse


slide-1
SLIDE 1

Method Arguments

Ruby Methods, Procs, etc.

CSCI400 21 September 2017

Ruby Methods, Procs, etc.

slide-2
SLIDE 2

Method Arguments

Color Key

  • Clickable URL link
  • Write down an answer to this for class participation
  • Just a comment – don’t confuse with yellow

Ruby Methods, Procs, etc.

slide-3
SLIDE 3

Method Arguments

Brief Refactor Exercise (1/2)

def fib(limit) yield 1 yield 1 a = 1 b = 1 while (i < limit) t = a a = a + b b = t yield a i = i + 1 end end

Ruby Methods, Procs, etc.

slide-4
SLIDE 4

Method Arguments

Brief Refactor Exercise (2/2)

def fib(limit) a, b = 0, 1 (1..limit).each do a, b = b, a + b yield a end end 11 lines → 4 lines (of actual logic/content)

Ruby Methods, Procs, etc.

slide-5
SLIDE 5

Method Arguments

Topics

  • Method arguments
  • Some new, some review
  • Proc

Ruby Methods, Procs, etc.

slide-6
SLIDE 6

Method Arguments

Method Arguments

Ruby Methods, Procs, etc.

slide-7
SLIDE 7

Method Arguments

Overview

  • Default value
  • Recall: Hangman.new vs. Hangman.new "myWords.txt"
  • Variable number of arguments
  • def readwrite(*syms)
  • Pass arguments in Hash
  • Block as function argument
  • When method has yield

Some of this section is review, and some is new

Ruby Methods, Procs, etc.

slide-8
SLIDE 8

Method Arguments

Default Value (1)

def title(name, len=3) name[0, len] end puts title("Mr. Brandon McCartney") puts title("Mrs. Doubtfire", 4)

Ruby Methods, Procs, etc.

slide-9
SLIDE 9

Method Arguments

Default Value (2)

# can use expression in default value def shift(x, dx=x/100.0) x + dx end puts shift(5) puts shift(10, 1)

Ruby Methods, Procs, etc.

slide-10
SLIDE 10

Method Arguments

Variable Argument Count

  • Similar to * (splat)
  • a, *b = 1, 2, 3
  • * before param in function definition
  • Param → array of 0 or more args
  • * before array param in function call
  • Param array → separate arguments

Ruby Methods, Procs, etc.

slide-11
SLIDE 11

Method Arguments

Variable Arg Examples

def limitedSum(max, *rest) total = rest.sum if total <= max total else max end end limitedSum(20, 1, 4, 5) limitedSum(20, 10, 20, 30) data = [1, 4, 5] limitedSum(20, *data)

Ruby Methods, Procs, etc.

slide-12
SLIDE 12

Method Arguments

Hashes for Arguments

  • Argument order
  • Could break client’s code if changed
  • Solution: send hash

Ruby Methods, Procs, etc.

slide-13
SLIDE 13

Method Arguments

Hashes for Arguments

def http(config) if config.key?(:tls) puts "Using HTTPS" end if config.key?(:basic_auth) puts "Using HTTP with BasicAuth" end end

Ruby Methods, Procs, etc.

slide-14
SLIDE 14

Method Arguments

Hashes for Arguments

def http(config) if config.key?(:tls) puts "Using HTTPS" end if config.key?(:basic_auth) puts "Using HTTP with BasicAuth" end end httpConfig = { :tls => true, :basic_auth => { "user" => "pass" } } http(httpConfig)

Ruby Methods, Procs, etc.

slide-15
SLIDE 15

Method Arguments

Blocks

  • Blocks are syntactic structures
  • Not objects
  • Identified by...
  • Curly braces: { ... }
  • do/end keywords: do .... end
  • Can create objects that represent blocks
  • Okay, but why?

Ruby Methods, Procs, etc.

slide-16
SLIDE 16

Method Arguments

yield

  • yield statement
  • Gives control to user-specified block

Ruby Methods, Procs, etc.

slide-17
SLIDE 17

Method Arguments

yield Example

def fib(limit) a, b = 0, 1 (1..limit).each do a, b = b, a + b yield a end end fib(10) { |x| puts "Fibonacci number: #{x}" }

Ruby Methods, Procs, etc.

slide-18
SLIDE 18

Method Arguments

Objects that Represent Blocks

  • Proc
  • Has block-like behavior
  • But can pass multiple procs into function
  • Lambdas*
  • Generally: anonymous functions
  • Ruby: lambdas are Proc instances
  • Blocks vs. Procs vs. Lambdas

*We’ll do lambdas in Haskell

Ruby Methods, Procs, etc.

slide-19
SLIDE 19

Method Arguments

Implicit Block Argument

def fib(limit) a, b = 0, 1 (1..limit).each do a, b = b, a + b yield a end end fib(10) { |x| puts x }

Ruby Methods, Procs, etc.

slide-20
SLIDE 20

Method Arguments

Explicit Block Argument

# block arg must be last argument # also, notice the `&` for the block arg def fib(limit, &block) a, b = 0, 1 (1..limit).each do a, b = b, a + b block.call(a) # could stil use `yield` end end fib(10) { |x| puts x }

Ruby Methods, Procs, etc.

slide-21
SLIDE 21

Method Arguments

Proc Argument

# notice no `&` this time -- `Proc` is just an object def fib(limit, proc) a, b = 0, 1 (1..limit).each do a, b = b, a + b proc.call(a) end end p = Proc.new { |x| puts x } fib(10, p)

Ruby Methods, Procs, etc.

slide-22
SLIDE 22

Method Arguments

Block vs. Proc

What’s the difference?

  • Block: one-time use
  • Proc: reusable
  • Pass to multiple functions
  • Provide with library

Ruby Methods, Procs, etc.

slide-23
SLIDE 23

Method Arguments

Block and Proc Uses

  • Goal: Encrypt file byte-by-byte
  • while more data to read...

1 read a byte 2 encrypt byte (with block or Proc) 3 write byte

  • encrypt step is up to user

Ruby Methods, Procs, etc.

slide-24
SLIDE 24

Method Arguments

Real-World Example (1)

class RailsAppTest < ActiveSupport::TestCase test "start web server" do # sequence of instructions + assert end end test prints formatted results

Ruby Methods, Procs, etc.

slide-25
SLIDE 25

Method Arguments

Real-World Example (2)

class Order < ActiveRecord::Base before_save :normalize_card_number, if: Proc.new { |order| order.paid_with_card? } # if: proc is a hash, same as { :if => proc } end

Ruby Methods, Procs, etc.

slide-26
SLIDE 26

Method Arguments

Proc Exercise (1/2)

  • Write function powers(max, proc1, proc2)
  • Applies proc1 and proc2 to integers [1, max]
  • See next slide for example output

Ruby Methods, Procs, etc.

slide-27
SLIDE 27

Method Arguments

Proc Exercise (2/2)

$ ruby procExample.rb Calling powers -- square and cube 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 Calling powers -- square and fourth power 1 1 1 2 4 16 3 9 81 4 16 256 5 25 625

Ruby Methods, Procs, etc.