Skip to content

Commit

Permalink
adding exercises ex01.rb through ex21.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelakey committed Nov 23, 2011
1 parent 7b1c422 commit f7d3ed7
Show file tree
Hide file tree
Showing 37 changed files with 1,026 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rbenv-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.9.2-p290
10 changes: 10 additions & 0 deletions ex01.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env ruby

puts "Hello World!"
puts "Hello Again"
puts "I like typing this."
puts "This is fun."
puts 'Yay! Pringing.'
puts "I'd much rather you 'not'."
puts 'I "said" do not touch this.'

11 changes: 11 additions & 0 deletions ex02.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# A comment, this is so you can read you rprogram 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:
# print "This won't run."

puts "This will run."

26 changes: 26 additions & 0 deletions ex03.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby

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

26 changes: 26 additions & 0 deletions ex03_5.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby

puts "I will now count my chickens:"

puts "Hens", 25.0 + 30.0 / 6.0
puts "Roosters", 100.0 - 25.0 * 3.0 % 4.0

puts "Now I will count the eggs:"

puts 3.0 + 2.0 + 1.0 - 5.0 + 4.0 % 2.0 - 1.0 / 4.0 + 6.0

puts "Is it true that 3 + 2 < 5 - 7?"

puts 3.0 + 2.0 < 5.0 - 7.0

puts "What is 3 + 2?", 3.0 + 2.0
puts "What is 5 - 7?", 5.0 - 7.0

puts "Oh, that's why it's False."

puts "How about some more."

puts "Is it greater?", 5.0 > -2.0
puts "Is it greater or equal?", 5.0 >= -2.0
puts "Is it less or equal?", 5.0 <= -2.0

19 changes: 19 additions & 0 deletions ex04.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env ruby

cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers - car_pool_capacity
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."


21 changes: 21 additions & 0 deletions ex05.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env ruby

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
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 right

puts "If I add %d, %d, and %d I get %d." % [my_age, my_height, my_weight, my_age + my_height + my_weight]

29 changes: 29 additions & 0 deletions ex06.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby

# string inside string 1
x = "There are #{10} types of people."
binary = "binary"
do_not = "don't"
# string inside string 2
y = "Those who know #{binary} and those who #{do_not}."

puts x
puts y

# string inside string 3 and 4
puts "I said: #{x}."
puts "I also said: '#{y}'."

hilarious = false

#string inside string 5
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


37 changes: 37 additions & 0 deletions ex07.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env ruby

# These 3 lines print stuff
puts "Mary had a little lamb."
puts "Its fleece was white as %s." % 'snow'
puts "And everywhere that Mary went."

# This line prints a period . ten times
puts "." * 10 #what'd that do?

# These
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 puts instead of print here. change it to puts
# and see what happens.

# Print prints all of the lines in one big line
# Puts puts each line on a separate line

print end1 + end2 + end3 + end4 + end5 + end6
print end7 + end8 + end9 + end10 + end11 + end12
print "Bingdong Dillydong"

# this just is polite use of the termina, try removing it
puts

15 changes: 15 additions & 0 deletions ex08.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby

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 coudl type up right.",
"But it didn't sing.",
"So I said goodnight."
]

15 changes: 15 additions & 0 deletions ex09.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby

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 three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5 or 6.
PARAGRAPH

18 changes: 18 additions & 0 deletions ex10.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby

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

12 changes: 12 additions & 0 deletions ex11.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env ruby

print "How old are you? "
age = gets.chomp()
print "How tall are you? "
height = gets.chomp()
print "How much do you weigh? "
weight = gets.chomp()

puts "So, you're #{age} old, #{height} tall and #{weight} heavy."


13 changes: 13 additions & 0 deletions ex12.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby

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-885901"
puts f.content_encoding # []
puts f.last_modified # Thu Dec 05 02:45:02 UTC 2002
end

11 changes: 11 additions & 0 deletions ex13.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby

first, second, third, fourth = 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}"
puts "Your fourth variable is: #{fourth}"


24 changes: 24 additions & 0 deletions ex13_3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env ruby

#first, second, third, fourth = 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}"
#puts "Your fourth variable is: #{fourth}"

puts "Enter your 1st variable: "
first = gets.chomp()

puts "Enter your 2nd variable: "
second = gets.chomp()

puts "Enter your 3rd variable: "
third = gets.chomp()

puts "Enter your 4th variable: "
fourth = gets.chomp()

puts "These are your variables #{first} #{second} #{third} #{fourth}"

25 changes: 25 additions & 0 deletions ex14.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env ruby

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

33 changes: 33 additions & 0 deletions ex15.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env ruby

# Take the first agrument at the command line and assign it to the variable "filename"
filename = ARGV.first

# Set the variable "prompt" to a carat
prompt = "> "
# We call the open method on the class File. The argument is the contents of the variable "filename".
# I think what's happening here is that the class File is being called upon to open "filename" and assign it to the variable "txt"
# That's not what's happening. When I "puts txt" I get this #<File:0x10016ac78>
# It looks like txt is being assigned the location of the file or something like that
txt = File.open(filename)

# We're printing the contents of the variable "filename"
puts "Here's your file: #{filename}"
# We "call a function" on the variable "txt". So does that make "txt" a class? WTF? This is awesome but WTF?
# We call the function "read" with no arguments on txt and we puts that to stdout
puts txt.read()

## Puts some text
#puts "Type the filename again:"
## Puts a prompt
#print prompt
## Take the user's input, chomp i.e. remove any new line or return or whatever at the end and assign it to the variable "file_again"
#file_again = STDIN.gets.chomp()
#
## file_again is a variable containing the name of our file
## We're going to assign a file object to txt_again?
#txt_again = File.open(file_again)
#
## We read our file object and puts it to stdout
#puts txt_again.read()

21 changes: 21 additions & 0 deletions ex15_1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env ruby

# Take the first agrument at the command line and assign it to the variable "filename"
filename = ARGV.first

print "I'm gonna let you finish but first I just wanna say this: "
puts File.atime(filename)

# We call the open method on the class File. The argument is the contents of the variable "filename".
# I think what's happening here is that the class File is being called upon to open "filename" and assign it to the variable "txt"
# That's not what's happening. When I "puts txt" I get this #<File:0x10016ac78>
# It looks like txt is being assigned the location of the file or something like that
txt = File.open(filename)

# We're printing the contents of the variable "filename"
puts "Below this line I'm gonna print the contents of this file here: #{filename}"
# We "call a function" on the variable "txt". So does that make "txt" a class? WTF? This is awesome but WTF?
# We call the function "read" with no arguments on txt and we puts that to stdout
puts txt.read()

txt.close()
Loading

0 comments on commit f7d3ed7

Please sign in to comment.