Add 'lrthw-remnants/' from commit '2b0a814a1795c60baf29b95462921b0ec8c7d861'

git-subtree-dir: lrthw-remnants
git-subtree-mainline: f2380eef05
git-subtree-split: 2b0a814a17
This commit is contained in:
Dan Buch 2013-01-09 23:47:30 -05:00
commit 01e5a4e45f
29 changed files with 517 additions and 0 deletions

3
lrthw-remnants/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*~
copied.txt
.rbenv-version

7
lrthw-remnants/ex1.rb Normal file
View File

@ -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.'

15
lrthw-remnants/ex10.rb Normal file
View File

@ -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 = <<MY_HEREDOC
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
MY_HEREDOC
puts tabby_cat
puts persian_cat
puts backslash_cat
puts fat_cat

8
lrthw-remnants/ex11.rb Normal file
View File

@ -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."

10
lrthw-remnants/ex12.rb Normal file
View File

@ -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 # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/>
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

9
lrthw-remnants/ex13.rb Normal file
View File

@ -0,0 +1,9 @@
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}"
print "And your fourth is?: "
puts "That's right, it's #{STDIN.gets.chomp}"

28
lrthw-remnants/ex14-ec.rb Normal file
View File

@ -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 <<MESSAGE
Alright, so you said #{likes} about liking me.
You live in #{lives}. Not sure where that is.
And you have a #{computer} computer. Nice.
You also said that your favorite name for a
dog is #{dogname}. Works for me.
MESSAGE

22
lrthw-remnants/ex14.rb Normal file
View File

@ -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 <<MESSAGE
Alright, so you said #{likes} about liking me.
You live in #{lives}. Not sure where that is.
And you have a #{computer} computer. Nice.
MESSAGE

18
lrthw-remnants/ex15-ec.rb Normal file
View File

@ -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

15
lrthw-remnants/ex15.rb Normal file
View File

@ -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()

View File

@ -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.

30
lrthw-remnants/ex16-ec.rb Normal file
View File

@ -0,0 +1,30 @@
filename = ARGV.first
script = $0
puts <<EOF
We're going to erase #{filename}.
If you don't want that, hit CTRL-C (^C).
If you do want that, hit RETURN.
EOF
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}\n#{line2}\n#{line3}\n")
puts "And finally, we close it."
target.close

33
lrthw-remnants/ex16.rb Normal file
View File

@ -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()

View File

@ -0,0 +1 @@
File.open(ARGV.first) { |i| File.open(ARGV[1], 'w') { |o| o.write(i.read) }}

22
lrthw-remnants/ex17.rb Normal file
View File

@ -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()

25
lrthw-remnants/ex18.rb Normal file
View File

@ -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()

21
lrthw-remnants/ex19.rb Normal file
View File

@ -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)

9
lrthw-remnants/ex2.rb Normal file
View File

@ -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."

35
lrthw-remnants/ex20.rb Normal file
View File

@ -0,0 +1,35 @@
input_file = ARGV.first
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 += 1
print_a_line(current_line, current_file)
current_line += 1
print_a_line(current_line, current_file)

35
lrthw-remnants/ex21.rb Normal file
View File

@ -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?"

38
lrthw-remnants/ex24.rb Normal file
View File

@ -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 = <<MULTI_LINE_STRING
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
MULTI_LINE_STRING
puts "--------------"
puts poem
puts "--------------"
five = 10 - 2 + 3 - 6
puts "This should be five: #{five}"
def secret_formula(started)
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
end
start_point = 10000
beans, jars, crates = secret_formula(start_point)
puts "With a starting point of: #{start_point}"
puts "We'd have #{beans} beans, #{jars} jars, and #{crates} crates."
start_point = start_point / 10
puts "We can also do that this way:"
puts "We'd have %s beans, %s jars, and %s crates." % secret_formula(start_point)

23
lrthw-remnants/ex3.rb Normal file
View File

@ -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

15
lrthw-remnants/ex4.rb Normal file
View File

@ -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."

18
lrthw-remnants/ex5.rb Normal file
View File

@ -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]

20
lrthw-remnants/ex6.rb Normal file
View File

@ -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

25
lrthw-remnants/ex7.rb Normal file
View File

@ -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

12
lrthw-remnants/ex8.rb Normal file
View File

@ -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."
]

14
lrthw-remnants/ex9.rb Normal file
View File

@ -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 <<PARAGRAPH
There's something going on here.
With the PARAGRAPH thing
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
PARAGRAPH

3
lrthw-remnants/test.txt Normal file
View File

@ -0,0 +1,3 @@
To all the people out there.
I say I don't like my hair.
I need to shave it off.