Week 05 Iterators, More Methods and Classes Hash, Regex, File I/O Joonhwan Lee human-computer interaction + design lab.
Iterators Writing Methods Classes & Objects Hash File I/O Quiz 4
1. Iterators
Array Iterators: each each: loop Array array.each do variable end code array = [1, 2, 3, 4, 5] array.each do x end puts x 4
Number Iterators: times times: loop Integer integer.times do end code i = 0 10.times do puts i i = i + 1 end 5
Range Range 1st ~ 31st January to December line number 23 to 32 a f ~, to, ~ ~ : range o x in Ruby (1..9) => 1, 2, 3, 4, 5, 6, 7, 8, 9 (1...9) => 1, 2, 3, 4, 5, 6, 7, 8 ('a'..'d') => 'a', 'b', 'c', 'd' 6
Range (1..5).each do x puts x (1..5).to_a ('a'..'f').min ('a'..'f').max ('a'..'f').include?('c') 7
Range http://www.ruby-doc.org/core-2.0.0/range.html (1..5).each do x puts x => 1 2 3 4 5 (1..5).to_a => [1, 2, 3, 4, 5] ('a'..'f').min => 'a' ('a'..'f').max => 'f' ('a'..'f').include?('c') => true 7
Ranges as Conditions for i in 0..5 end puts "current i value is #{i}" (0..5).each do i end puts "current i value is #{i}" 8
Lab 1: 1 1000 1 1000. Result of addition from 1 to 1000 is: 500500. Result of multiplication from 1 to 1000 is: 402387260077093... 9
2. Writing Methods
Iterators & Loops Iterator Loop. iterator loop. lab2.rb. Lab 2,. String, Integer, Array. 11
def method_name code code... end def hello puts "hello, ruby programmer~!" end hello => "hello, ruby programmer~!" 12
. def method_name value code end Lab 3.1 def hello name puts "hello, #{name}" => hello "ruby" hello "java" hello "programmer" 13
. def method_name value1, value2 code end Lab 3.2 def sayname name, times puts "hello, #{name*times}" => sayname "ruby", 4 sayname "java", 10 sayname "programmer", 0 14
** ** Lab 3.4 num = 10 def change_value num num = 2 puts num change_value num => 2 puts num => 10 15
a = "12345" b = a.to_i " (return)".. : x = rand(1..5) y = Math.sqrt(128) 16
return. Lab 4.1 def square(x) puts (x * x) square(5) a = square(10) puts a 17
return. Lab 4.1 def square(x) puts (x * x) square(5) a = square(10) puts a => 25 => 100 => nil 17
Lab 4.2 square def square1(x) x * x square1(5) a1 = square1(10) puts a1 18
square Lab 4.2 def square1(x) x * x square1(5) a1 = square1(10) puts a1 => => => 100 18
Lab 4.3 def square2(x) result = x * x result end Lab 4.4 def square3(x) return x * x 19
Lab 5: lab2.rb. def ask(question) while true puts question answer = gets.chomp.downcase if (answer == 'yes' answer == 'no') if answer == 'yes' wets_bed_result = true else wets_bed_result = false break else puts 'Please answer "yes" or "no".' return wets_bed_result end 20
3. Class
( ).,,.. Numeric, Integer, Float String Regexp Time / File Hash Array 22
.. Time. Time,. now = Time.new # now 23
Object Oriented Programming (OOP) (OOP)., (object).. : :, :,. /. : /. / :. 24
Object Oriented Programming (OOP). : /.., ( : ) :.. ( ) :.. : String sort. 25
Lab 6 class. class Die def roll 1 + rand(6) class method. die1 = Die.new # die1.roll # roll 26
. @. Lab 6 ( ) class Die def roll @number = 1 + rand(6) def showing @number puts Die.new.showing 27
. @. Lab 6 ( ) class Die def roll @number = 1 + rand(6) def showing @number puts Die.new.showing => nil!!! 27
. @. Lab 6 ( ) class Die def roll @number = 1 + rand(6) def showing @number puts Die.new.showing,. die = Die.new die.roll die.showing => nil!!! 27
Lab 6,. class Die def initialize roll def roll @number = 1 + rand(6) def showing @number end 28
Lab 6,. class Die def initialize roll def roll @number = 1 + rand(6) def showing @number die = Die.new initialize roll. end 28
Lab 7: Dragon Dragon. Dragon... 3. (toss), (rock).. ( )..... (Lab7.rb ) 29
Lab 8: Dragon interactive... Lab 7 interactive. Lab 5 ask. Dragon. G-Dragon 'G-Dragon'. commands: feed, toss, walk, rock, put to bed, exit feed 'G-Dragon'. commands: feed, toss, walk, rock, put to bed, exit toss 'G-Dragon'! 30
4. Hash
Hash Class Hash. Array, array hash key-value.,. ["name"] = " " (key-value) ["address"] = " " (key-value) 32
Array vs. Hash Lab 9.1 contact = Array.new contact = [" ", 24, "male", " "] puts contact[0] => puts contact[2] => male Lab 9.2 contact = Hash.new contact["name"] = " " contact["age"] = 24 contact["sex"] = "male" contact["address"] = " " puts contact["age"] => 24 puts contact["address"] => 33
Symbol Hash (symbol). :name => :name :name.to_s => name "name".to_sym => :name 34
Symbol. hash["key"] = value1 hash["key"] = value2 hash["key"] = value3 hash["key"] = value4 hash["key"] = value5... key. hash[:key] = value1 hash[:key] = value2 hash[:key] = value3... key. 35
Hash Hash Lab 10.1 person = Hash.new person[:name] = "Hong Gil Dong" person[:phone_number] = "555-1234" person = {:name => "Hong Gil Dong", :phone_number => "555-1234"} puts person[:name] puts person[:phone_number] 36
Hash Lab 10.2 Hash person[:phone_number] = "010-555-1234" Hash person[:sex] = "male" p person => {:name => "Hong Gil Dong", :phone_number => "010-555-1234", :sex => "male"} 37
Hash Iterator Lab 10.3 Array contact = [" ", 24, "male", " "] contact.each do v puts v end Hash contact = {:name => " ", :sex => "male", :address => " "} contact.each do k, v puts "#{k}: #{v}" 38
5. File I/O
IO Class. File. gets, puts, print IO IO read, write, readline. IO File. 40
File save open File. file = File.new("filename", "mode") mode. r : read-only r+ : read-write w : write-only w+ : read-write file.close 41
File. file = File.new("test.txt", "w"). file << "hello, ruby". file.close 42
Lab 11: One Little Monkey. One little monkey jumping on the bed He fell off and bumped his head So Momma called the doctor and the doctor said No more monkeys jumping on the bed! Printed on 2013/10/12 at 10:45AM 43
File. file = File.new("test.txt", "r"). a = file.read. file.each do line puts line.upcase end. file.close 44
Lab 12: One Little Monkey Lab 11 One Little Monkey. 45
Lab 13: Jobs' Speech jobs_speech.txt.,. ",.. : I AM HONORED TO BE... : gsub(',',''), split() 46
Quiz 4 Take Home Quiz, comment quiz4_.rb etl : 3 ( 2.5, 0.5)
Quiz 4:. Hash, Key, key value 1. Hash sort_by value sort. THE: 91 I: 86 TO: 71 AND: 49 WAS: 48 48
Questions?