Skip to content

Commit 3a2d901

Browse files
committed
ruby programs
0 parents  commit 3a2d901

File tree

401 files changed

+17131
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

401 files changed

+17131
-0
lines changed

2009-bookofruby.pdf

2.93 MB
Binary file not shown.

ch1/1helloworld.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
puts 'hello world'

ch1/2helloname.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
print('Enter your name: ' )
2+
name = gets()
3+
puts( "Hello #{name}" )

ch1/3string_eval.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def showname
2+
return "Fred"
3+
end
4+
5+
puts "Hello #{showname}"
6+
puts( "\n\t#{(1+2) * 3}\nGoodbye" )

ch1/4calctax.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The Book of Ruby - http://www.sapphiresteel.com
2+
# Calculate tax on a fixed sum
3+
4+
subtotal = 100.00
5+
taxrate = 0.175 # one zero only before decimal point
6+
tax = subtotal * taxrate
7+
puts "Tax on $#{subtotal} is $#{tax}, so grand total is $#{subtotal+tax}"

ch1/5taxcalculator.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# The Book of Ruby - http://www.sapphiresteel.com
2+
# Prompt user for a sum then calculate tax on that sum
3+
4+
taxrate = 0.175
5+
print "Enter price (ex tax): "
6+
s = gets
7+
subtotal = s.to_f
8+
if (subtotal < 0.0) then
9+
subtotal = 0.0
10+
end
11+
tax = subtotal * taxrate
12+
puts "Tax on $#{subtotal} is $#{tax}, so grand total is $#{subtotal+tax}"

ch1/6dogs.rb

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# The Book of Ruby - http://www.sapphiresteel.com
2+
# Create classes and use instance variables such as @myname
3+
4+
class Dog
5+
def set_name( aName )
6+
@myname = aName
7+
end
8+
9+
def get_name
10+
return @myname
11+
end
12+
13+
def talk
14+
return 'woof!'
15+
end
16+
end
17+
18+
19+
class Cat
20+
def set_name( aName )
21+
@myname = aName
22+
end
23+
24+
def get_name
25+
return @myname
26+
end
27+
28+
def talk
29+
return 'miaow!'
30+
end
31+
end
32+
33+
# --- Create instances (objects) of the Dog and Cat classes
34+
mydog = Dog.new
35+
yourdog = Dog.new
36+
mycat = Cat.new
37+
yourcat = Cat.new
38+
someotherdog = Dog.new
39+
40+
# --- Name them
41+
mydog.set_name( 'Fido' )
42+
yourdog.set_name( 'Bonzo' )
43+
mycat.set_name( 'Tiddles' )
44+
yourcat.set_name( 'Flossy' )
45+
46+
47+
# --- Get their names and display them
48+
# Dogs
49+
puts(mydog.get_name)
50+
puts(yourdog.get_name)
51+
# hmmm, but what happens here if the dog has no name?
52+
puts(someotherdog.get_name)
53+
# Cats
54+
puts(mycat.get_name)
55+
puts(yourcat.get_name)
56+
57+
# --- Ask them to talk
58+
puts(mydog.talk)
59+
puts(yourdog.talk)
60+
puts(mycat.talk)
61+
puts(yourcat.talk)

ch1/7treasure.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# The Book of Ruby - http://www.sapphiresteel.com
2+
# define a class and create some objects
3+
4+
5+
class Thing
6+
def set_name( aName )
7+
@name = aName
8+
end
9+
10+
def get_name
11+
return @name
12+
end
13+
end
14+
15+
class Treasure
16+
def initialize( aName, aDescription )
17+
@name = aName
18+
@description = aDescription
19+
end
20+
21+
def to_s # override default to_s method
22+
"The #{@name} Treasure is #{@description}\n"
23+
end
24+
end
25+
26+
thing1 = Thing.new
27+
thing1.set_name( "A lovely Thing" )
28+
puts thing1.get_name
29+
30+
t1 = Treasure.new("Sword", "an Elvish weapon forged of gold")
31+
t2 = Treasure.new("Ring", "a magic ring of great power")
32+
puts t1.to_s
33+
puts t2.to_s
34+
# The inspect method lets you look inside an object
35+
puts "Inspecting 1st treasure: #{t1.inspect}"

ch1/8to_s.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# The Book of Ruby - http://www.sapphiresteel.com
2+
# Show string representations of various objects
3+
# using the to_s method
4+
5+
class Treasure
6+
def initialize( aName, aDescription )
7+
@name = aName
8+
@description = aDescription
9+
end
10+
# This time we won't override to_s so the Treasure object
11+
# will use the default to_s method...
12+
end
13+
14+
15+
t = Treasure.new( "Sword", "A lovely Elvish weapon" )
16+
puts(" Class.to_s")
17+
puts(Class.to_s)
18+
puts(" Object.to_s")
19+
puts(Object.to_s)
20+
puts(" String.to_s")
21+
puts(String.to_s)
22+
puts(" 100.to_s")
23+
puts(100.to_s)
24+
puts(" Treasure.to_s")
25+
puts(Treasure.to_s)
26+
puts(" t.to_s")
27+
puts(t.to_s)
28+
puts(" t.inspect")
29+
puts(t.inspect)

ch1/comments.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# this is a comment
2+
3+
puts( "hello" ) # this is also a comment
4+
5+
=begin
6+
This is a
7+
multiline
8+
comment
9+
=end
10+
11+
puts( "goodbye" )

ch1/p.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The Book of Ruby - http://www.sapphiresteel.com
2+
3+
class Treasure
4+
def initialize( aName, aDescription )
5+
@name = aName
6+
@description = aDescription
7+
end
8+
9+
def to_s # override default to_s method
10+
"The #{@name} Treasure is #{@description}\n"
11+
end
12+
end
13+
14+
15+
a = "hello"
16+
b = 123
17+
c = Treasure.new( "ring", "a glittery gold thing" )
18+
19+
p( a )
20+
p( b )
21+
p( c )

ch1/variables.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# The Book of Ruby - http://www.sapphiresteel.com
2+
3+
localvar = "hello"
4+
$globalvar = "goodbye"
5+
6+
def amethod
7+
localvar = 10
8+
puts( localvar )
9+
puts( $globalvar )
10+
end
11+
12+
def anotherMethod
13+
localvar = 500
14+
$globalvar = "bonjour"
15+
puts( localvar )
16+
puts( $globalvar )
17+
end
18+
19+
20+
amethod
21+
puts
22+
anotherMethod
23+
puts
24+
amethod
25+
puts
26+
puts( localvar )
27+
puts( $globalvar )

ch10/1blocks.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The Book of Ruby - http://www.sapphiresteel.com
2+
3+
puts('Block #1')
4+
3.times do |i|
5+
puts( i )
6+
end
7+
8+
puts('Block #2')
9+
3.times { |i|
10+
puts( i )
11+
}
12+
13+
# Here's an example that avoids using a block
14+
def aMethod( i )
15+
puts( i )
16+
end
17+
18+
puts('Method Call')
19+
for i in 0..2
20+
aMethod( i )
21+
end

ch10/2blocks.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# The Book of Ruby - http://www.sapphiresteel.com
2+
3+
puts('Block #3' )
4+
b3 = [1,2,3].collect{|x| x*2}
5+
puts( b3.inspect )
6+
7+
puts('Block #4' )
8+
b4 = ["hello","good day","how do you do"].collect{|x| x.capitalize }
9+
puts( b4.inspect )
10+
11+
puts('Block #5' )
12+
b5 = ["hello","good day","how do you do"].each{|x| x.capitalize }
13+
puts(b5.inspect)
14+
15+
puts('Block #6' )
16+
b6 = ["hello","good day","how do you do"].each{|x| x.capitalize! }
17+
puts(b6.inspect)
18+
19+
puts('---Block #7---' )
20+
newstr = ''
21+
a = "hello world".split(//).each{ |x| newstr << x.capitalize
22+
puts(newstr)
23+
}
24+
puts("a.inspect=#{a.inspect}")
25+
puts("newsstr='#{newstr}'")
26+
27+
puts('---Block #8---' )
28+
newstr = ''
29+
a = "hello world".each_byte{|x| newstr << (x.chr).capitalize
30+
puts(newstr)
31+
}
32+
puts("a.inspect=#{a.inspect}")
33+
puts("newsstr='#{newstr}'")

ch10/3blocks.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# The Book of Ruby - http://www.sapphiresteel.com
2+
3+
puts( '--- Proc.new ---' )
4+
a = Proc.new{|x| x = x*10; puts(x) }
5+
a.call(100)
6+
7+
puts('----Block #1---' )
8+
b = lambda{|x| x = x*10; puts(x) }
9+
b.call(100)
10+
11+
puts('---Block #2---' )
12+
c = proc{|x| x.capitalize! }
13+
c1 = c.call( "hello" )
14+
puts( c1 )
15+
16+
puts('---Block #3---' )
17+
d = lambda{|x| x.capitalize! }
18+
d1 = ["hello","good day","how do you do"].each{ |s| d.call(s)}
19+
puts(d1.inspect)

ch10/4blocks.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# The Book of Ruby - http://www.sapphiresteel.com
2+
3+
# using 'yield' to execute a block
4+
5+
def aMethod
6+
puts('--- In aMethod ---' )
7+
yield
8+
end
9+
10+
aMethod{ puts( "Good morning" ) }
11+
12+
13+
def caps( anarg )
14+
puts('--- In caps method ---' )
15+
yield( anarg )
16+
end
17+
18+
caps( "a lowercase string" ){ |x| x.capitalize! ; puts( x ) }
19+
20+
puts( "And now a block within a block..." )
21+
# a block within a block
22+
["hello","good day","how do you do"].each{
23+
|s|
24+
caps( s ){ |x| x.capitalize!
25+
puts( x )
26+
}
27+
}
28+
29+

0 commit comments

Comments
 (0)