Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
merrua committed Jul 17, 2014
0 parents commit b6bc9ad
Show file tree
Hide file tree
Showing 58 changed files with 1,125 additions and 0 deletions.
1 change: 1 addition & 0 deletions #ex16_readingwriting.txt#
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Exercise 16 Reading and Writign Files
25 changes: 25 additions & 0 deletions BookInStock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

class BookInStock
attr_reader :isbn
attr_accessor :price

def isbn=(isbn)
@isbn = isbn
end

def price=(price)
@price = price
end

def initialize(isbn, price)
raise(ArgumentError, "ISBN should not be empty") if isbn==''
raise ArgumentError.new("Price should not be empty") if price == ''
@isbn = isbn
@price = Float(price)
raise ArgumentError.new("Price has to be bigger than 0") if price <= 0
end

def price_as_string
return sprintf("$%2.2f", price)
end
end
37 changes: 37 additions & 0 deletions binary_multiple_of_4.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

def hello(name)
hellostring="Hello, "
return hellostring += name
end



def starts_with_consonant?(s)
if /[aeiouAEIOU*]/.match(s[0])
return false
elsif /0-9*/.match(s[0])
return false
elsif /[^a-zA-Z0-9.*]/.match(s)
return false
elsif /^$/.match(s)
return false
else
return true
end
end

def binary_multiple_of_4?(s)
is = s.to_i
if s == "0"
return true
elsif /[a-zA-Z^$3-9*]/.match(s)
return false
else
if /^[10]*00$/.match(s) && is % 2 == 0
return true
else
return false
end
end
end

10 changes: 10 additions & 0 deletions ex1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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.'
puts "Look another line."
puts 'Look another line.'
# puts 'this makes the line a comment'
19 changes: 19 additions & 0 deletions ex10_cat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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

poppy_cat = "This string has a quote: \". As you can see, it is escaped"

puts poppy_cat
11 changes: 11 additions & 0 deletions ex11_questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# chomp removes newline is a string method
# gets returns a string

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} years old, #{height} tall and #{weight} heavy."
12 changes: 12 additions & 0 deletions ex11_questions.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Exercise 11: Asking Questions
# chomp removes newline is a string method
# gets returns a string

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} years old, #{height} tall and #{weight} heavy."
20 changes: 20 additions & 0 deletions ex12_libraries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Exercise 12: Libraries
# require runs another file
# wont require the same file twice
# good if you just want to use a module

# include adds all the methods from another file
# allows you to extend classes with other modules

# load lets you execute methods

require 'open-uri'

open("http://www.ruby-lang.org/en") do |f|
f.each_line {|line| p line}
puts f.base_uri # <URI::HTTP:0x40e6df2 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
19 changes: 19 additions & 0 deletions ex13_parameters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Exercise 13: Parameters, Unpacking, Variables
# ARGV is the argument variable
# ARGV is a constant. It holds the arguments you pass to your ruby script

# Line 1 "unpacks" ARGV so that, rather than holding all the arguments
# it gets assigned to three variables you can work with first, second and third
# The name of the script itself is stored in a special variable
# $0 which we dont need to unpack.
# This may look strange, but "unpack" is probally the best word to describe
# what it does, it just says "Take whatever is in ARGV, unpack it and
# assign it to all of these variables on the left in order."


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}"

10 changes: 10 additions & 0 deletions ex13_parameters2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
first, second, third = ARGV

puts "The script is called: #{$0}"
print "We are using gets.chomp. to get variables"

var = STDIN.gets.chomp()
puts
puts "Your first variable is: #{first}"
puts "Your second variable is: #{second}"
puts "Your third variable is: #{third}"
12 changes: 12 additions & 0 deletions ex13_parameters2.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Exercise 13-b: Parameters, Unpacking, Variables

first, second, third = ARGV

puts "The script is called: #{$0}"
print "We are using gets.chomp. to get variables"

var = STDIN.gets.chomp()
puts
puts "Your first variable is: #{first}"
puts "Your second variable is: #{second}"
puts "Your third variable is: #{third}"
23 changes: 23 additions & 0 deletions ex14_prompt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

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
24 changes: 24 additions & 0 deletions ex14_prompt.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Exercise 14: Prompting And Passing

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 changes: 18 additions & 0 deletions ex15_readingfiles.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# exercise 15: reading files
# ruby.learncodethehardway.org

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()
3 changes: 3 additions & 0 deletions ex15_sample.txt
Original file line number Diff line number Diff line change
@@ -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.
36 changes: 36 additions & 0 deletions ex16_readingwriting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

filename = ARGV.first
raise RuntimeError.new "No input file" unless ARGV.count > 0

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()
37 changes: 37 additions & 0 deletions ex16_readingwriting.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# exercise 16, reading and writing files

filename = ARGV.first
raise RuntimeError.new "No input file" unless ARGV.count > 0

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()
16 changes: 16 additions & 0 deletions ex17_morefiles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

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()
17 changes: 17 additions & 0 deletions ex17_morefiles.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Exercise 17. More Files

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()
8 changes: 8 additions & 0 deletions ex2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 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."
9 changes: 9 additions & 0 deletions ex2.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# A comment, this is so you can read you 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."
Loading

0 comments on commit b6bc9ad

Please sign in to comment.