joshua_cheek
-
Ruby,
pasted
on Oct 26:
|
#Learning Ruby chapter 9
#single inheritance
#modules = interfaces from Java
class Hello
def initialize( name )
@name = name
end
def hello_world
puts "1 Hello, " + @name + "!"
end
end
hi = Hello.new( "world" )
hi.hello_world
#you can always add to a class, they are always open
class Array
def array_of_ten
(1..10).to_a
end
end
a = Array.new
ten = a.array_of_ten
puts "2 #{ten.inspect}"
#getters and setters
class Horse
def name #getter
@name
end
def name=( value ) #setter
@name = value
end
end
h = Horse.new
h.name = "Jimbo" #set
puts "3 #{h.name}" #get
#each class works with the Module named class
#this makes available attr , attr_reader , attr_writer , and attr_accessor
class Dog
attr :bark , true
end
class Dog2
attr :bark
end
puts "4 #{(Dog.instance_methods - Object.instance_methods).inspect}"
puts "5 #{(Dog2.instance_methods - Object.instance_methods).inspect}"
fido = Dog.new
fido.bark = "woof!"
puts "6 #{fido.bark}"
class Dog3
attr_reader :bark #getter
attr_writer :bark #setter
end
puts "7 #{(Dog3.instance_methods - Object.instance_methods).inspect}"
class Gaits
attr_accessor :walk , :trot , :canter
end
puts "8 #{(Gaits.instance_methods.sort - Object.instance_methods ).inspect }"
#class variable = static variable in Java
class Repeat
@@total = 0
def initialize( string , times )
@string = string
@times = times
end
def repeat
@@total += @times
return @string * @times
end
def total
"Total times , so far: " + @@total.to_s
end
end
data = Repeat.new( "a " , 8 )
data2 = Repeat.new( "b " , 5 )
data3 = Repeat.new( "c " , 2 )
puts "9 #{data.repeat }"
puts "10 #{data.total }"
puts "11 #{data2.repeat}"
puts "12 #{data2.total }"
puts "13 #{data3.repeat}"
puts "14 #{data3.total }"
#class method = static method in Java
#inheritance is accomplished with < operator
class Name
attr_accessor :given_name , :family_name
end
class Address < Name
attr_accessor :street , :city , :state , :country
end
a = Address.new
puts "15 #{a.respond_to?( :given_name )}"
puts "16 #{(Address.instance_methods.sort - Object.instance_methods ).inspect }"
#modules
module Dice
#virtual roll of a pair of dice
def roll
r_1 = rand(6)
r_2 = rand(6)
r1 = r_1 > 0 ? r_1 : 1
r2 = r_2 > 0 ? r_2 : 6
total = r1 + r2
printf( "17 You rolled %d and %d (%d).\n" , r1 , r2 , total )
total
end
end
class Game
include Dice
end
g = Game.new
g.roll
#static methods in modules
module Binary
def Binary.to_bin( num )
sprintf( "%08b" , num )
end
end
puts "18 #{Binary.to_bin( 123 )}"
#public , private , protected
class Names
#public by default
def initialize( a )
end
private
def this_is_private
end
protected
def this_is_protected
end
end
|
Output:
|
1 Hello, world!
2 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3 Jimbo
4 ["bark", "bark="]
5 ["bark"]
6 woof!
7 ["bark", "bark="]
8 ["canter", "canter=", "trot", "trot=", "walk", "walk="]
9 a a a a a a a a
10 Total times , so far: 8
11 b b b b b
12 Total times , so far: 13
13 c c
14 Total times , so far: 15
15 true
16 ["city", "city=", "country", "country=", "family_name", "family_name=", "given_name", "given_name=", "state", "state=", "street", "street="]
17 You rolled 3 and 1 (4).
18 01111011
|
|