joshua_cheek
-
Ruby,
pasted
on Oct 26:
|
#Learning Ruby chapter 2
#blocks: iterate through something
pacific = ["Washington" , "Oregon" , "California" ]
pacific.each do |state|
puts state
end
def gimme
if block_given?
yield
puts "I have a block!"
else
puts "I'm blockless!"
end
end
gimme
gimme do
print "Do I have a block? "
end
gimme { print "What about now? " }
#procs
count = Proc.new { [1,2,3,4,5].each do |i| print i end; puts }
your_proc = lambda { puts "Lurch: 'You rang?'" }
my_proc = proc{ puts "Morticia: 'Who was at the door, Lurch?'" }
puts count.class, your_proc.class , my_proc.class
count.call
your_proc.call
my_proc.call
#symbols
name = "josh 1"
name2 = name.to_sym
name3 = name2.id2name
puts name.inspect
puts name2.inspect
puts name3.inspect
puts name == name3
puts Symbol.all_symbols.length
#inheritence
class Hello
def howdy
"Hello, world!"
end
end
class Goodbye < Hello
def solong
"Goodbye , world."
end
end
friendly = Goodbye.new
puts "1 inheritance: #{friendly.howdy}"
puts "2 inheritance: #{friendly.solong}"
#reserved words
BEGIN{ puts "3 I PRINT FIRST!!" }
END{ puts "4 I PRINT LAST!!" }
alias :puts_alias :puts
puts_alias "5 alias"
puts "6 and: %s" % (true and false) #and , true , false
puts "7 or: %s" % (true or false) #or , true , false
for i in 0...1000
break if i > 8
print i , " "
end
puts "break @ 8"
#class , def
puts "9 defined? #{defined? friendly}"
print "10 defined? " , defined? some_undefined_variable , "\n"
#do , else , elsif , end
#ensure -> always executes at block termination; use after last rescue
#I couldn't get it to work
#module, sort of an abstract template that can be inherited
#it cannot be instantiated, ie kernel is a module, it defines
#puts , sprintf , etc
#next , nil , not (same as !) , or (same as ||)
i=0
while i<6
i+=1
redo if i<11 #causes iteration of the loop to be re-executed (kind of like next)
end
puts "#{i} redo"
#rescue (catches errors) , retry (repeats a method call outside of rescue)
i = 0
begin #starts a block
10 / i
puts "13 success -> divide by one"
rescue #catch error from begin
puts "12 fail -> divide by zero"
i = 1
retry #go back to begin
end #ends a block
#return , self (current object) , super , then
#undef (makes a method in current class undefined)
#until , when , while , yield
puts "14 this file is named #{__FILE__}"
puts "15 this is line number #{__LINE__}"
=begin
this is all a comment
puts "16 this is in the comments"
=end
#kind_of? , class , type casting
x = 100
puts "17 x.kind_of? Integer #{x.kind_of? Integer}"
puts "18 x.class #{x.class}"
puts "19 x.to_f #{x.to_f}"
#variables
abcde = 123
puts "20 local variable: #{defined?(abcde)}"
class VariableTest
@@class_var = 3 #spans all instances of this class
def initialize(n)
@instance_var = n #scope is only this instance
end
def var_test
puts "21 @@ is defined as #{defined? @@class_var}"
puts "22 @ is defined as #{defined? @instance_var}"
puts "#{$global_var} $ is defined as #{defined? $global_var}"
end
end
$global_var = 23
a = VariableTest.new(9)
a.var_test
#constants are in all caps (by convention, they can still be fucked with, so consider freezing them)
#parallel assignment
a , b , c = 24 , "parallel" , "assignment"
puts "%d %s %s" % [a,b,c]
#array shit
thoreau = "If a man does not keep pace with his companions, perhaps it is because he hears a different drummer."
puts "25 substring: #{thoreau[37..46]}" #start index through end index
puts "26 last several characters: #{thoreau[-8..-2]}"
#individual characters
puts "27 ?a=#{?a} , ?z=#{?z}"
#regular expression commands
# ^ matches tbe beginning of a line
# $ matches teh end of a line
# \w matches a word character
# [...] matches any character in the brackets
# [^...] matches any characters not in the brackets
# * matches zero or more occurrences of the previous regexp
# + matches one or more occurrences of the previous regexp
# ? matches zero or one occurrences of the previous regexp
hamlet = "The slings and arrows of outrageous fortune"
puts "28 regexp: #{hamlet.scan(/\w+/).inspect}" #matches one or more word characters and places in an array
#math test
x = 0
puts "#{20 + 3*(x+=3)} assignment returns it's value"
x = 0
if x.zero?
puts "30 zero? method on #{x} is true"
else
puts "30 zero? method on #{x} is false"
end
#methods / functions
def hello
puts "31 method is defined"
end
begin
hello #call hello method
rescue
puts "32 method is undefined" #never accessed
end
undef hello #undefine the hello method
begin
hello #call the hello method
rescue
puts "33 method is undefined"
end
#last character of method
# ? returns boolean
puts "34 1.eql? 1 => #{1.eql? 1}"
# ! changes the value of an object
a = [3,1,5]
a.sort
puts "35 not destructive: #{a.inspect}"
a.sort!
puts "36 destructive: #{a.inspect}"
# = sets a value, I can't think of any examples yet
#default arguments for functions
def repeat( word = "aBCDe " , times = 3 )
word*times
end
puts "37 sending values: #{repeat("josh ",2)}"
puts "38 not sending values1: #{repeat("josh")}"
puts "39 not sending values2: #{repeat}"
#variable number of arguments
def num_args( *args )
args.length.to_s + " argument" + (args.length == 1 ? ": " : "s: ") + args.inspect
end
puts "40 variable arguments: #{num_args(1,2,3,"abc", :abc , ?a ,2.3)}"
#object id and aliasing methods
def joy
"hello world"
end
alias joy2 joy
puts "41 alias outputs: #{joy} #{joy2}"
puts "42 same id? joy=#{joy.object_id} joy2=#{joy2.object_id}"
|
Output:
|
3 I PRINT FIRST!!
Washington
Oregon
California
I'm blockless!
Do I have a block? I have a block!
What about now? I have a block!
Proc
Proc
Proc
12345
Lurch: 'You rang?'
Morticia: 'Who was at the door, Lurch?'
"josh 1"
:"josh 1"
"josh 1"
true
1025
1 inheritance: Hello, world!
2 inheritance: Goodbye , world.
5 alias
6 and: false
7 or: true
0 1 2 3 4 5 6 7 8 break @ 8
9 defined? local-variable
10 defined? nil
11 redo
12 fail -> divide by zero
13 success -> divide by one
14 this file is named t.rb
15 this is line number 109
17 x.kind_of? Integer true
18 x.class Fixnum
19 x.to_f 100.0
20 local variable: local-variable
21 @@ is defined as class variable
22 @ is defined as instance-variable
23 $ is defined as global-variable
24 parallel assignment
25 substring: companions
26 last several characters: drummer
27 ?a=97 , ?z=122
28 regexp: ["The", "slings", "and", "arrows", "of", "outrageous", "fortune"]
29 assignment returns it's value
30 zero? method on 0 is true
31 method is defined
33 method is undefined
34 1.eql? 1 => true
35 not destructive: [3, 1, 5]
36 destructive: [1, 3, 5]
37 sending values: josh josh
38 not sending values1: joshjoshjosh
39 not sending values2: aBCDe aBCDe aBCDe
40 variable arguments: 7 arguments: [1, 2, 3, "abc", :abc, 97, 2.3]
41 alias outputs: hello world hello world
42 same id? joy=537773760 joy2=537773740
4 I PRINT LAST!!
|
|