learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often...
Transcript of learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often...
brian who?
2
So, who is this guy?Visit http://www.napcs.com/http://www.snippetstash.com/http://www.feelmyskills.com/http://www.webdesignfordevelopers.com
Shoes?
3
A talk about shoes would be completely inappropriate at a code camp. So we’re not talking about these kinds of shoes. Although, a search for shoes on Google will turn up lots of stuff like this.
OMG! SHOES!
x-platform GUI in Ruby!4
We’re talking about Shoes, a cross-platform framework for making GUI apps, using the Ruby language.
_why_ ruby?
• Highly dynamic
• Very high level
• 100% object oriented
• 100% open-source
5
It’s also incredibly easy to learn.
data types in ruby
6
One of the nice things about Ruby is that it strives to be easy to read. Here, we’re declaring some variables and setting values.
variablesConstants
Local variable
Instance variable
7
Like most languages, you’ve got constants, local variables, and instance variables. The convention in Ruby is to use all capital letters for constants, but anything that starts with a capital letter is considered constant.
You can actually change values of constants at runtime, but you will get a warning about that.
Instance variables get the @ sign.
Ruby is dynamically typed
8
Anyone know what dynamically typed is?
What’s the opposite of dynamically typed?
don’t confuse that with weak typed!
9
a type can be defined explicitly
10
or implicitly
11
...but a string and a number can’t just be
mashed together without help.
12
bad
13
You’ll get a runtime error because you can’t add a string to a number
better
14
The to_s method converts many types of objects to strings.
awesome.
#{} embeds an expression in a string,
converting the result to a string!
15
You can embed any expression this way and it will automatically cast the value to a string, even if it’s a null value.
you’re going to see#{}
a lot.
16
ruby has simple logic
17
defining methods
18
def is so much nicer than function.
ruby has some simple rules
19
everything isan object
20
There’s no such thing as a primitive in Ruby. Even a string or an integer is an object.
every statement returns a value.
21
With methods, if it’s not explicit with a return, then the return value is the value of the last statement.
everything evaluatesto TRUE exceptnil and FALSE.
22
1 is true. 0 is not false.
everything is an object.everything is a class.
classes can be boring or awesome.
23
booooooring
24
The “java” way. This is on the way out because it’s stupid. Here we’re declaring instance variables, getters, and setters. Essentially creating properties.
awesome.
25
attr_accessor creates variables, getters, and setters.
as you write rubyyou often see codethat writes code.
26
Metaprogramming!
arrays and hashes are important. Pay attention.
27
They’re everywhere.
arrays
28
Square brackets denote hashes.
hash
29
Curly braces and => are for hashes.
whenever you see
=>
you’re dealing with a hash.
30
Hashrocket.
methods that take hashes as parameters
are very, very common.
31
this method takes one parameter.
32
Only one parameter which is a hash. Hashes are amazing because you can actually send the parameters into a method in any order, and it’s nicely self-documenting. No referencing the api to figure out which parameter is the width.
now let’stake a walk
around the Block
33
Blocks are like closures or anonymous fuctions
blocks let us loop
34
We can use them for looping instead of having to track an array or hash position.
so, a regular class...
35
...can be improved with blocks
36
This is a standard pattern in Ruby classes.
37
Obligatory LOLCAT is obligatory!
make constructors accept hashes...
38
...and use a block toiterate over keys and
values to assign instance variables.
39
this technique makes writing DSLs
easy.
40
no. wrong.
41
DomainSpecific
Language
42
ruby has “sugar” tomake it easy to read.
43
Parenthesis are optional in many cases, especially when it’s obvious.
oh yeah...
= is assignment.
== is equality.
44
ok now you’re warmed up.
45
but you have to Tie your Shoes
before you can run!
46
you’re supposed to BOO!
47
and no, this doesn’t excuse the bad joke.
Shoes is a DSL.
48
a Shoes app is a ruby object.
49
your code lives inside of this object using blocks.
50
it’s super easy.
51
REALLY super easy.
52
so, basic stuff.
53
Shoes.app
54
background
55
background
56
backgrounds can usehex codes too.
57
layout works likeyou wish HTML and CSS works
58
stacks
59
all sub elements stack.
60
flows
61
all sub elements flow
62
combine stacksand flows
to make complexlayouts
63
two column with header
64
the Shoes window is also
a flow.
65
simple text
66
edit_line
67
edit_line
great for passwordz
68
edit_box
69
list_box
arrays and hashes are everywhere.
70
radio
71
check
72
use instance variablesto reference controls.
73
grabbing and showing input
74
Use the .text method to grab the text from the textbox.
any object is fair game.even stacks and floats
75
append to a stack
76
The @names section contains the names we add, and names get added when we click the button.
Clearing a stack
77
Stacks and flows can be cleared as well. Clear can take a block which you can use to place new content in the stack or flow.
Windows
78
windows arelike new apps.
79
So instance variables are isolated.
each screenis a url.
80
urls and methods
81
We can use a nice MVC-style structure here. Notice that we can wrap blocks in methods so we can easily call them.
shoes canplay music
and movies...
82
The video player plays mp3s, oggs, avis, movs, flvs and more.
load images...
83
...and open URLs in browsers.
84
you can use any gemwith Shoes.
85
There are hundreds of libraries out there for doing pretty much anything. Twitter, Youtube, Flickr, Last.fm, and anything in between.
shoes can installlibraries for you.
86
When the app starts, these libraries get installed to the user’s home directory.
you can alsoaccess OS stuff.
87
88
Accessing standard dialogs and the clipboard is pretty simple.
demos
89
http://github.com/napcs/shoes_demo_apps/
90
Either check it out with git or click the download link and unzip the package.
twitter: bphogan
http://www.napcs.com/
91