From cdbef84b5bd2fe2e6fdfaf3d9b5647bfd0b26874 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 6 Feb 2012 23:19:36 -0500 Subject: [PATCH 01/29] ex1 --- ex1.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 ex1.rb diff --git a/ex1.rb b/ex1.rb new file mode 100644 index 0000000..1fd549f --- /dev/null +++ b/ex1.rb @@ -0,0 +1,7 @@ +puts "Hello World!" +puts "Hello Again" +puts "I like typing this." +puts "This is fun." +puts 'Yay! Printing.' +puts "I'd much rather you 'not'." +puts 'I "said" do not touch this.' From 593c16025caf63828ba06b7c935073444efa7701 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 7 Feb 2012 22:35:18 -0500 Subject: [PATCH 02/29] Continuing on ... seemingly missed ex3 and ex4 --- .gitignore | 1 + ex2.rb | 9 +++++++++ ex5.rb | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 .gitignore create mode 100644 ex2.rb create mode 100644 ex5.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/ex2.rb b/ex2.rb new file mode 100644 index 0000000..a0168a6 --- /dev/null +++ b/ex2.rb @@ -0,0 +1,9 @@ +# A comment, this is so you can read your program later. +# Anything after the # is ignored by Ruby. + +puts "I could have code like this." # and the comment after is ignored. + +# You can also use a comment to "disable" or comment out a piece of code: +# puts "This won't run." + +puts "This will run." diff --git a/ex5.rb b/ex5.rb new file mode 100644 index 0000000..5998ab2 --- /dev/null +++ b/ex5.rb @@ -0,0 +1,18 @@ +my_name = 'Zed A. Shaw' +my_age = 35 +my_height = 74 +my_weight = 180 +my_eyes = 'Blue' +my_teeth = 'White' +my_hair = 'Brown' + +puts "Let's talk about %s." % my_name +puts "He's %d inches tall." % my_height +puts "He's %d pounds heavy." % my_weight +puts "Actually that's not too heavy." +puts "He's got %s eyes and %s hair." % [my_eyes, my_hair] +puts "His teeth are usually %s depending on the coffee." % my_teeth + +# this line is tricky, try to get it exactly right +puts "If I add %d, %d, and %d I get %d." % [ + my_age, my_height, my_weight, my_age + my_height + my_weight] From 67b1aff3bc3d51c2c45904fcfc7bf872937e7f58 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 7 Feb 2012 23:27:20 -0500 Subject: [PATCH 03/29] adding ex6 --- ex6.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 ex6.rb diff --git a/ex6.rb b/ex6.rb new file mode 100644 index 0000000..b6361f5 --- /dev/null +++ b/ex6.rb @@ -0,0 +1,20 @@ +x = "There are #{10} types of people." +binary = "binary" +do_not = "don't" +y = "Those who know #{binary} and those who #{do_not}." + +puts x +puts y + +puts "I said: #{x}" +puts "I also said: '#{y}'." + +hilarious = false +joke_evaluation = "Isn't that joke so funny?! #{hilarious}" + +puts joke_evaluation + +w = "This is the left side of..." +e = "a string with a right side." + +puts w + e From e5be159ad407f92a6b5da8bee6bb621a25d5a137 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 11 Feb 2012 12:21:58 -0500 Subject: [PATCH 04/29] Back-filling ex3 so that I can actually talk about it at the next meeting --- ex3.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 ex3.rb diff --git a/ex3.rb b/ex3.rb new file mode 100644 index 0000000..4392bf3 --- /dev/null +++ b/ex3.rb @@ -0,0 +1,23 @@ +puts "I will now count my chickens:" + +puts "Hens", 25 + 30 / 6 +puts "Roosters", 100 - 25 * 3 % 4 + +puts "Now I will count the eggs:" + +puts 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 + +puts "Is it true that 3 + 2 < 5 - 7?" + +puts 3 + 2 < 5 - 7 + +puts "What is 3 + 2?", 3 + 2 +puts "What is 5 - 7?", 5 - 7 + +puts "Oh, that's why it's false." + +puts "How about some more." + +puts "Is it greater?", 5 > -2 +puts "Is it greater or equal?", 5 >= -2 +puts "Is it less or equal?", 5 <= -2 From bc9371983171174eb1b7b23b72a48fd983f95da6 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 11 Feb 2012 12:41:03 -0500 Subject: [PATCH 05/29] Back-filling ex4 --- ex4.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 ex4.rb diff --git a/ex4.rb b/ex4.rb new file mode 100644 index 0000000..1712506 --- /dev/null +++ b/ex4.rb @@ -0,0 +1,15 @@ +cars = 100 +space_in_a_car = 4.0 +drivers = 30 +passengers = 90 +cars_not_driven = cars - drivers +cars_driven = drivers +carpool_capacity = cars_driven * space_in_a_car +average_passengers_per_car = passengers / cars_driven + +puts "There are #{cars} cars available." +puts "There are only #{drivers} drivers available." +puts "There will be #{cars_not_driven} empty cars today." +puts "We can transport #{carpool_capacity} people today." +puts "We have #{passengers} passengers to carpool today." +puts "We need to put about #{average_passengers_per_car} in each car." From f2ee86111f3578a8f436ae09b44af4d2de91696d Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 19 Feb 2012 13:36:30 -0500 Subject: [PATCH 06/29] new files for exercises 7 and 8 --- ex7.rb | 25 +++++++++++++++++++++++++ ex8.rb | 12 ++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 ex7.rb create mode 100644 ex8.rb diff --git a/ex7.rb b/ex7.rb new file mode 100644 index 0000000..e163cfa --- /dev/null +++ b/ex7.rb @@ -0,0 +1,25 @@ +puts "Mary had a little lamb." +puts "Its fleece was white as %s." % 'snow' +puts "And everywhere that Mary went." +puts "." * 10 # what'd that do? + +end1 = "C" +end2 = "h" +end3 = "e" +end4 = "e" +end5 = "s" +end6 = "e" +end7 = "B" +end8 = "u" +end9 = "r" +end10 = "g" +end11 = "e" +end12 = "r" + +# notice how we are using print instead of puts here. change it to puts +# and see what happens. +print end1 + end2 + end3 + end4 + end5 + end6 +print end7 + end8 + end9 + end10 + end11 + end12 + +# this just is polite use of the terminal, try removing it +puts diff --git a/ex8.rb b/ex8.rb new file mode 100644 index 0000000..a75b3db --- /dev/null +++ b/ex8.rb @@ -0,0 +1,12 @@ +formatter = "%s %s %s %s" + +puts formatter % [1, 2, 3, 4] +puts formatter % ["one", "two", "three", "four"] +puts formatter % [true, false, false, true] +puts formatter % [formatter, formatter, formatter, formatter] +puts formatter % [ + "I had this thing.", + "That you could type up right.", + "But it didn't sing.", + "So I said goodnight." +] From 0a3aa9c5ec8617aaba03fbbe00758ca5e667807e Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 19 Feb 2012 13:40:22 -0500 Subject: [PATCH 07/29] file for ex9 --- ex9.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 ex9.rb diff --git a/ex9.rb b/ex9.rb new file mode 100644 index 0000000..5bd8195 --- /dev/null +++ b/ex9.rb @@ -0,0 +1,14 @@ +# Here's some new strange stuff, remember type it exactly. + +days = "Mon Tue Wed Thu Fri Sat Sun" +months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" + +puts "Here are the days: ", days +puts "Here are the months: ", months + +puts < Date: Sun, 19 Feb 2012 13:43:12 -0500 Subject: [PATCH 08/29] file for ex10 --- ex10.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 ex10.rb diff --git a/ex10.rb b/ex10.rb new file mode 100644 index 0000000..4da7001 --- /dev/null +++ b/ex10.rb @@ -0,0 +1,15 @@ +tabby_cat = "\tI'm tabbed in." +persian_cat = "I'm split\non a line." +backslash_cat = "I'm \\ a \\ cat." + +fat_cat = < Date: Sun, 19 Feb 2012 13:52:42 -0500 Subject: [PATCH 09/29] file for ex11 --- ex11.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 ex11.rb diff --git a/ex11.rb b/ex11.rb new file mode 100644 index 0000000..56c1b89 --- /dev/null +++ b/ex11.rb @@ -0,0 +1,8 @@ +print "How old are you? " +age = gets.chomp +print "How tall are you? " +height = gets.chomp +print "Howmuch do you weigh? " +weight = gets.chomp + +puts "So, you're #{age} old, #{height} tall, and #{weight} heavy." From e1cf5f8292ab047dff4086a82eb62bd329b916cd Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 19 Feb 2012 13:56:34 -0500 Subject: [PATCH 10/29] file for ex12 --- ex12.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 ex12.rb diff --git a/ex12.rb b/ex12.rb new file mode 100644 index 0000000..1fc78cc --- /dev/null +++ b/ex12.rb @@ -0,0 +1,10 @@ +require 'open-uri' + +open("http://www.ruby-lang.org/en") do |f| + f.each_line {|line| p line} + puts f.base_uri # + puts f.content_type # "text/html" + puts f.charset # "iso-8859-1" + puts f.content_encoding # [] + puts f.last_modified # Thu Dec 05 02:45:02 UTC 2002 +end From dc709e15490f358637403875066e87ae6407c93a Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 25 Feb 2012 11:59:56 -0500 Subject: [PATCH 11/29] file for ex13 --- ex13.rb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 ex13.rb diff --git a/ex13.rb b/ex13.rb new file mode 100644 index 0000000..49fe1d1 --- /dev/null +++ b/ex13.rb @@ -0,0 +1,6 @@ +first, second, third = ARGV + +puts "The script is called: #{$0}" +puts "Your first variable is: #{first}" +puts "Your second variable is: #{second}" +puts "Your third variable is: #{third}" From 0bb1ac3cb4ac573b56e6af6e4044b3b6df5c5e52 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 25 Feb 2012 12:01:36 -0500 Subject: [PATCH 12/29] ex13 extra credit --- ex13.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ex13.rb b/ex13.rb index 49fe1d1..a5e38c6 100644 --- a/ex13.rb +++ b/ex13.rb @@ -4,3 +4,6 @@ puts "The script is called: #{$0}" puts "Your first variable is: #{first}" puts "Your second variable is: #{second}" puts "Your third variable is: #{third}" + +print "And your fourth is?: " +puts "That's right, it's #{STDIN.gets.chomp}" From c9d0be6147145201bbedf9c3f6785b433b50cceb Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 25 Feb 2012 23:15:29 -0500 Subject: [PATCH 13/29] file for ex 14 --- ex14.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 ex14.rb diff --git a/ex14.rb b/ex14.rb new file mode 100644 index 0000000..11437b2 --- /dev/null +++ b/ex14.rb @@ -0,0 +1,22 @@ +user = ARGV.first +prompt = '> ' + +puts "Hi #{user}, I'm the #{$0} script." +puts "I'd like to ask you a few questions." +puts "Do you like me #{user}?" +print prompt +likes = STDIN.gets.chomp() + +puts "Where do you live #{user}?" +print prompt +lives = STDIN.gets.chomp() + +puts "What kind of computer do you have?" +print prompt +computer = STDIN.gets.chomp() + +puts < Date: Sat, 25 Feb 2012 23:31:25 -0500 Subject: [PATCH 14/29] ex 14 extra credit --- ex14-ec.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ex14-ec.rb diff --git a/ex14-ec.rb b/ex14-ec.rb new file mode 100644 index 0000000..689222e --- /dev/null +++ b/ex14-ec.rb @@ -0,0 +1,28 @@ +user = ARGV.first +prompt = '~: ' + +puts "Hi #{user}, I'm the #{$0} script." +puts "I'd like to ask you a few questions." +puts "Do you like me #{user}?" +print prompt +likes = STDIN.gets.chomp + +puts "Where do you live #{user}?" +print prompt +lives = STDIN.gets.chomp + +puts "What kind of computer do you have?" +print prompt +computer = STDIN.gets.chomp + +puts "What's your favorite name for a dog?" +print prompt +dogname = STDIN.gets.chomp + +puts < Date: Sat, 25 Feb 2012 23:45:19 -0500 Subject: [PATCH 15/29] files for ex15 --- ex15.rb | 15 +++++++++++++++ ex15_sample.txt | 3 +++ 2 files changed, 18 insertions(+) create mode 100644 ex15.rb create mode 100644 ex15_sample.txt diff --git a/ex15.rb b/ex15.rb new file mode 100644 index 0000000..5be6005 --- /dev/null +++ b/ex15.rb @@ -0,0 +1,15 @@ +filename = ARGV.first + +prompt = "> " +txt = File.open(filename) + +puts "Here's your file: #{filename}" +puts txt.read() + +puts "I'll also ask you to type it again:" +print prompt +file_again = STDIN.gets.chomp() + +txt_again = File.open(file_again) + +puts txt_again.read() diff --git a/ex15_sample.txt b/ex15_sample.txt new file mode 100644 index 0000000..f4a96d5 --- /dev/null +++ b/ex15_sample.txt @@ -0,0 +1,3 @@ +This is stuff I typed into a file. +It is really cool stuff. +Lots and lots of fun to have in here. From 4900d8fb2783c44948269478949203dfa8c2e6b1 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 25 Feb 2012 23:51:41 -0500 Subject: [PATCH 16/29] lots of extra credit this time, so making sure I'm familiar with it --- ex15-ec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 ex15-ec.rb diff --git a/ex15-ec.rb b/ex15-ec.rb new file mode 100644 index 0000000..a111ec9 --- /dev/null +++ b/ex15-ec.rb @@ -0,0 +1,18 @@ +filename = ARGV.first + +prompt = "> " +txt = File.new(filename) + +puts "Here's your file: #{filename}" +puts txt.read + +puts "I'll also ask you to type it again:" +print prompt +file_again = STDIN.gets.chomp + +txt_again = File.new(file_again) + +puts txt_again.read + +txt.close +txt_again.close From 428e83efd1959b3c9c6cc78c944f911bb2194065 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 25 Feb 2012 23:58:37 -0500 Subject: [PATCH 17/29] file for ex16 --- ex16.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 ex16.rb diff --git a/ex16.rb b/ex16.rb new file mode 100644 index 0000000..3c0f941 --- /dev/null +++ b/ex16.rb @@ -0,0 +1,33 @@ +filename = ARGV.first +script = $0 + +puts "We're going to erase #{filename}." +puts "If you don't want that, hit CTRL-C (^C)." +puts "If you do want that, hit RETURN." + +print "? " +STDIN.gets + +puts "Opening the file..." +target = File.open(filename, 'w') + +puts "Truncating the file. Goodbye!" +target.truncate(target.size) + +puts "Now I'm going to ask you for three lines" + +print "line 1: "; line1 = STDIN.gets.chomp() +print "line 2: "; line2 = STDIN.gets.chomp() +print "line 3: "; line3 = STDIN.gets.chomp() + +puts "I'm going to write these to the file." + +target.write(line1) +target.write("\n") +target.write(line2) +target.write("\n") +target.write(line3) +target.write("\n") + +puts "And finally, we close it." +target.close() From 1b00a0f514c7ace3930457b10f68fface7fc0c15 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 26 Feb 2012 00:03:24 -0500 Subject: [PATCH 18/29] Extra credit for ex16. Must remember the 1.8 incompatibility thing. --- ex16-ec.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 ex16-ec.rb diff --git a/ex16-ec.rb b/ex16-ec.rb new file mode 100644 index 0000000..6aa1efa --- /dev/null +++ b/ex16-ec.rb @@ -0,0 +1,30 @@ +filename = ARGV.first +script = $0 + +puts < Date: Sun, 26 Feb 2012 20:55:09 -0500 Subject: [PATCH 19/29] ex17 --- .gitignore | 1 + ex17.rb | 22 ++++++++++++++++++++++ test.txt | 3 +++ 3 files changed, 26 insertions(+) create mode 100644 ex17.rb create mode 100644 test.txt diff --git a/.gitignore b/.gitignore index b25c15b..a3f37ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *~ +copied.txt diff --git a/ex17.rb b/ex17.rb new file mode 100644 index 0000000..d6b07d7 --- /dev/null +++ b/ex17.rb @@ -0,0 +1,22 @@ +from_file, to_file = ARGV +script = $0 + +puts "Copying from #{from_file} to #{to_file}" + +# we could do these two on one line too, how? +input = File.open(from_file) +indata = input.read() + +puts "The input file is #{indata.length} bytes long" + +puts "Does the output file exist? #{File.exists? to_file}" +puts "Ready, hit RETURN to continue, CTRL-C to abort." +STDIN.gets + +output = File.open(to_file, 'w') +output.write(indata) + +puts "Alright, all done." + +output.close() +input.close() diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..6d66b25 --- /dev/null +++ b/test.txt @@ -0,0 +1,3 @@ +To all the people out there. +I say I don't like my hair. +I need to shave it off. From f23ea036711569222b6fbc4fb33aa2fd9783a961 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 26 Feb 2012 20:57:31 -0500 Subject: [PATCH 20/29] ex17 extra credit #2 --- ex17-ec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 ex17-ec.rb diff --git a/ex17-ec.rb b/ex17-ec.rb new file mode 100644 index 0000000..8ed5ed2 --- /dev/null +++ b/ex17-ec.rb @@ -0,0 +1,23 @@ + +from_file, to_file = ARGV +script = $0 + +puts "Copying from #{from_file} to #{to_file}" + +# we could do these two on one line too, how? +input = File.open(from_file) +indata = input.read() + +puts "The input file is #{indata.length} bytes long" + +puts "Does the output file exist? #{File.exists? to_file}" +puts "Ready, hit RETURN to continue, CTRL-C to abort." +STDIN.gets + +output = File.open(to_file, 'w') +output.write(indata) + +puts "Alright, all done." + +output.close() +input.close() From a295260e52c7b46b0a155a69fbd84f6b7ca20662 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 26 Feb 2012 21:03:25 -0500 Subject: [PATCH 21/29] ex17 extra credit #3 --- ex17-ec.rb | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/ex17-ec.rb b/ex17-ec.rb index 8ed5ed2..b4c5170 100644 --- a/ex17-ec.rb +++ b/ex17-ec.rb @@ -1,23 +1,5 @@ - -from_file, to_file = ARGV -script = $0 - -puts "Copying from #{from_file} to #{to_file}" - -# we could do these two on one line too, how? -input = File.open(from_file) -indata = input.read() - -puts "The input file is #{indata.length} bytes long" - -puts "Does the output file exist? #{File.exists? to_file}" -puts "Ready, hit RETURN to continue, CTRL-C to abort." -STDIN.gets - -output = File.open(to_file, 'w') -output.write(indata) - -puts "Alright, all done." - -output.close() -input.close() +File.open(ARGV.first) do |infile| + File.open(ARGV[1], 'w') do |outfile| + outfile.write(infile.read) + end +end From 8c167a01e54c296a30876770f048c03da9c3bee7 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 26 Feb 2012 21:04:33 -0500 Subject: [PATCH 22/29] ex17 extra credit #3 yet more --- ex17-ec.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/ex17-ec.rb b/ex17-ec.rb index b4c5170..5169d0b 100644 --- a/ex17-ec.rb +++ b/ex17-ec.rb @@ -1,5 +1 @@ -File.open(ARGV.first) do |infile| - File.open(ARGV[1], 'w') do |outfile| - outfile.write(infile.read) - end -end +File.open(ARGV.first) { |i| File.open(ARGV[1], 'w') { |o| o.write(i.read) }} From bb7392a440e764cbe79fffb68220ec18a1891cd6 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 27 Feb 2012 00:22:50 -0500 Subject: [PATCH 23/29] ex18 --- ex18.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 ex18.rb diff --git a/ex18.rb b/ex18.rb new file mode 100644 index 0000000..d7e1b37 --- /dev/null +++ b/ex18.rb @@ -0,0 +1,25 @@ +# this one is like your scripts with argv +def puts_two(*args) + arg1, arg2 = args + puts "arg1: #{arg1}, arg2: #{arg2}" +end + +# ok, that *args is actually pointless, we can just do this +def puts_two_again(arg1, arg2) + puts "arg1: #{arg1}, arg2: #{arg2}" +end + +# this just takes one argument +def puts_one(arg1) + puts "arg1: #{arg1}" +end + +# this one takes no arguments +def puts_none() + puts "I got nothin'." +end + +puts_two("Zed", "Shaw") +puts_two_again("Zed", "Shaw") +puts_one("First!") +puts_none() From 7ea78eb9485aa2bde5a72593cb33ce524dd2ef38 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 27 Feb 2012 09:28:19 -0500 Subject: [PATCH 24/29] ex19 --- ex19.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ex19.rb diff --git a/ex19.rb b/ex19.rb new file mode 100644 index 0000000..7dc1db4 --- /dev/null +++ b/ex19.rb @@ -0,0 +1,21 @@ +def cheese_and_crackers(cheese_count, boxes_of_crackers) + puts "You have #{cheese_count} cheeses!" + puts "You have #{boxes_of_crackers} boxes of crackers!" + puts "Man that's enough for a party!" + puts "Get a blanket." + puts # a blank line +end + +puts "We can just give the function numbers directly:" +cheese_and_crackers(20, 30) + +puts "OR, we can use variables from our script:" +amount_of_cheese = 10 +amount_of_crackers = 50 +cheese_and_crackers(amount_of_cheese, amount_of_crackers) + +puts "We can even do math inside too:" +cheese_and_crackers(10 + 20, 5 + 6) + +puts "And we can combine the two, variables and math:" +cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) From 44942e54fb6695e5ff8667b0e0176bab2748e608 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 21 Mar 2012 21:36:50 -0400 Subject: [PATCH 25/29] Ignoring rbenv file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a3f37ab..2e1099c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *~ copied.txt +.rbenv-version From a9ac349ba948a6a5d2d5e8f3828bfe0527952057 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 21 Mar 2012 22:03:19 -0400 Subject: [PATCH 26/29] ex20 --- ex20.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 ex20.rb diff --git a/ex20.rb b/ex20.rb new file mode 100644 index 0000000..2dc3410 --- /dev/null +++ b/ex20.rb @@ -0,0 +1,35 @@ +input_file = ARGV[0] + +def print_all(f) + puts f.read() +end + +def rewind(f) + f.seek(0, IO::SEEK_SET) +end + +def print_a_line(line_count, f) + puts "#{line_count} #{f.readline()}" +end + +current_file = File.open(input_file) + +puts "First let's print the whole file:" +puts # a blank line + +print_all(current_file) + +puts "Now let's rewind, kind of like a tape." + +rewind(current_file) + +puts "Let's print three lines:" + +current_line = 1 +print_a_line(current_line, current_file) + +current_line = current_line + 1 +print_a_line(current_line, current_file) + +current_line = current_line + 1 +print_a_line(current_line, current_file) From aa308911d4926191e0bdf48104b398b424874e96 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 21 Mar 2012 22:04:36 -0400 Subject: [PATCH 27/29] making ex20 more ruby-ish --- ex20.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ex20.rb b/ex20.rb index 2dc3410..43706be 100644 --- a/ex20.rb +++ b/ex20.rb @@ -1,7 +1,7 @@ -input_file = ARGV[0] +input_file = ARGV.first def print_all(f) - puts f.read() + puts f.read end def rewind(f) @@ -9,7 +9,7 @@ def rewind(f) end def print_a_line(line_count, f) - puts "#{line_count} #{f.readline()}" + puts "#{line_count} #{f.readline}" end current_file = File.open(input_file) @@ -28,8 +28,8 @@ puts "Let's print three lines:" current_line = 1 print_a_line(current_line, current_file) -current_line = current_line + 1 +current_line += 1 print_a_line(current_line, current_file) -current_line = current_line + 1 +current_line += 1 print_a_line(current_line, current_file) From fbb5e7e6977af2136396dd03abdc76e9aeb00ba8 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 22 Mar 2012 19:40:56 -0400 Subject: [PATCH 28/29] ex21 --- ex21.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 ex21.rb diff --git a/ex21.rb b/ex21.rb new file mode 100644 index 0000000..7ade111 --- /dev/null +++ b/ex21.rb @@ -0,0 +1,35 @@ +def add(a, b) + puts "ADDING #{a} + #{b}" + a + b +end + +def subtract(a, b) + puts "SUBTRACTING #{a} - #{b}" + a - b +end + +def multiply(a, b) + puts "MULTIPLYING #{a} * #{b}" + a * b +end + +def divide(a, b) + puts "DIVIDING #{a} / #{b}" + a / b +end + +puts "Let's do some math with just functions!" + +age = add(30, 5) +height = subtract(78, 4) +weight = multiply(90, 2) +iq = divide(100, 2) + +puts "Age: #{age}, Height: #{height}, Weight: #{weight}, IQ: #{iq}" + +# A puzzle for the extra credit, type it in anyway. +puts "Here is a puzzle." + +what = add(age, subtract(height, multiply(weight, divide(iq, 2)))) + +puts "That becomes: #{what} Can you do it by hand?" From 2b0a814a1795c60baf29b95462921b0ec8c7d861 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Fri, 23 Mar 2012 08:51:56 -0400 Subject: [PATCH 29/29] ex24 --- ex24.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 ex24.rb diff --git a/ex24.rb b/ex24.rb new file mode 100644 index 0000000..ea23d2e --- /dev/null +++ b/ex24.rb @@ -0,0 +1,38 @@ +puts "Let's practice everything." +puts "You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs." + +poem = <