-
Notifications
You must be signed in to change notification settings - Fork 0
/
Appearance2-UseParaInYourClasses.rb
87 lines (51 loc) · 2.06 KB
/
Appearance2-UseParaInYourClasses.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#Using Shoes display methods in your classes
#
#
# You have a functioning Ruby program, and now you want to make it a Proper Program with Clickboxes And Stuff.
# But it's not working
#
# You changed all your puts to paras, but you keep getting "method para not found for class" errors
#
# This is because "para" is a sort of method which affects the Shoes app object - "do para to the app" i.e. "show my stuff"
# You get an error because the method para doesn't exist for Arrays, or for classes you created
#
# (Its all explained in the Manual under Hello!/Rules, but here's a practical example of fixing the problem:
#Here we have a Ruby custom method, which displays only strings from an array:
=begin
class Array
def displaynames
self.each do |ea|
if ea.class == String
puts ea
else
#its a number, do nothing
end
end
end
end
=end
#this doesn't work in Shoes. Here's how to make it work:
class Array
def displaynames(shoes) #the method now takes an argument, which will be the Shoes app object
self.each do |ea|
if ea.class == String
shoes.para ea #"shoes.para" tells the shoes app object to do para, i.e. show your content.
#WRONG: para ea
#WRONG: puts ea
#WRONG: Shoes::App::para ea
else
#its a number, do nothing
end
end
end
end
Shoes.app do
mixedarray = ["Red",1,"blue",2,"green",3]
mixedarray.displaynames(self) #this gives your custom method "self", which is always the app object.
#this is different from Ruby, slightly. It's explained in the manual
end
#This is one little example
#For a bigger program, simply go through and do these three steps every time you want to call a shoes method not in the body of Shoes.app:
#1. Make your method take an argument(shoes)
#2. When you call the method, pass in the argument self
#3. When you use shoes lingo outside of shoes, always write "shoes.alert" or "shoes.para" rather than just "alert" or "para"