1 / 13

Methods

Methods. A simple example. def embiggen ( str ) str = "#{ str }!!!" str = str.upcase return str end puts( embiggen ('hello world') ) #=> HELLO WORLD!!!. Parts of a method. The name how you will refer to the method. S tick to lowercase letters and underscores. The arguments

ilario
Télécharger la présentation

Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Methods

  2. A simple example defembiggen(str) str = "#{str}!!!" str = str.upcase return str end puts( embiggen('hello world') ) #=> HELLO WORLD!!!

  3. Parts of a method • The name • how you will refer to the method. Stick to lowercase letters and underscores. • The arguments • These are the variables, if any, that the method will use or operate on. • The block • the chunk of code that does something • The return value • is what the method passes back to whatever called it – you can think of it as the method's answer.

  4. Add Em defadd_em(name, repeats) (1..repeats).each do |the_name| puts name end end add_em("Jerry",8)

  5. Exercise: Arguments and return Write a method that: • Accepts two strings as arguments • Outputs to screen a string that consists of the two strings concatenated and capitalized • The method should return only the concatenated string

  6. Solution defmush_these_words(str1, str2) return (str1+str2).capitalize end

  7. Scope def foo(var_of_limited_scope) puts "#{var_of_limited_scope} is inside method foo" end foo("hello world") #=> hello world is inside method foo puts var_of_limited_scope #=> NameError: undefined local variable or method `var_of_limited_scope'

  8. DRY • Don’t Repeat Yourself • Methods for things you do often • Keep things modular

  9. require "open-uri" url = "http://www.nytimes.com" pattern = "<img" page = open(url).read tags = page.scan(pattern) puts "The site #{url} has #{tags.length} img tags"

  10. Exercise • Write a method that takes a URL as an argument and returns a count of the number of images. require "open-uri" url = "http://www.nytimes.com" pattern = "<img" page = open(url).read tags = page.scan(pattern) puts "The site #{url} has #{tags.length} img tags"

  11. require "open-uri" defcount_image_tags(url) pattern = "<img" page = open(url).read tags = page.scan(pattern) tags.length end

  12. Can we make it better? • Abstraction

  13. require "open-uri" defcount_any_tags(url, tag) pattern = /<#{tag}\b/ page = open(url).read tags = page.scan(pattern) tags.length end

More Related