learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often...

91
learning to walk in shoes brian hogan [email protected] twitter: bphogan 1

Upload: others

Post on 08-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

learning to walkin shoes

brian [email protected]: bphogan

1

Page 2: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

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

Page 3: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

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.

Page 4: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

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.

Page 5: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

_why_ ruby?

• Highly dynamic

• Very high level

• 100% object oriented

• 100% open-source

5

It’s also incredibly easy to learn.

Page 6: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

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.

Page 7: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

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.

Page 8: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

Ruby is dynamically typed

8

Anyone know what dynamically typed is?

What’s the opposite of dynamically typed?

Page 9: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

don’t confuse that with weak typed!

9

Page 10: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

a type can be defined explicitly

10

Page 11: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

or implicitly

11

Page 12: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

...but a string and a number can’t just be

mashed together without help.

12

Page 13: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

bad

13

You’ll get a runtime error because you can’t add a string to a number

Page 14: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

better

14

The to_s method converts many types of objects to strings.

Page 15: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

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.

Page 16: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

you’re going to see#{}

a lot.

16

Page 17: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

ruby has simple logic

17

Page 18: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

defining methods

18

def is so much nicer than function.

Page 19: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

ruby has some simple rules

19

Page 20: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

everything isan object

20

There’s no such thing as a primitive in Ruby. Even a string or an integer is an object.

Page 21: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

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.

Page 22: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

everything evaluatesto TRUE exceptnil and FALSE.

22

1 is true. 0 is not false.

Page 23: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

everything is an object.everything is a class.

classes can be boring or awesome.

23

Page 24: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

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.

Page 25: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

awesome.

25

attr_accessor creates variables, getters, and setters.

Page 26: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

as you write rubyyou often see codethat writes code.

26

Metaprogramming!

Page 27: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

arrays and hashes are important. Pay attention.

27

They’re everywhere.

Page 28: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

arrays

28

Square brackets denote hashes.

Page 29: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

hash

29

Curly braces and => are for hashes.

Page 30: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

whenever you see

=>

you’re dealing with a hash.

30

Hashrocket.

Page 31: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

methods that take hashes as parameters

are very, very common.

31

Page 32: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

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.

Page 33: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

now let’stake a walk

around the Block

33

Blocks are like closures or anonymous fuctions

Page 34: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

blocks let us loop

34

We can use them for looping instead of having to track an array or hash position.

Page 35: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

so, a regular class...

35

Page 36: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

...can be improved with blocks

36

This is a standard pattern in Ruby classes.

Page 37: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

37

Obligatory LOLCAT is obligatory!

Page 38: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

make constructors accept hashes...

38

Page 39: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

...and use a block toiterate over keys and

values to assign instance variables.

39

Page 40: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

this technique makes writing DSLs

easy.

40

Page 41: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

no. wrong.

41

Page 42: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

DomainSpecific

Language

42

Page 43: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

ruby has “sugar” tomake it easy to read.

43

Parenthesis are optional in many cases, especially when it’s obvious.

Page 44: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

oh yeah...

= is assignment.

== is equality.

44

Page 45: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

ok now you’re warmed up.

45

Page 46: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

but you have to Tie your Shoes

before you can run!

46

you’re supposed to BOO!

Page 47: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

47

and no, this doesn’t excuse the bad joke.

Page 48: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

Shoes is a DSL.

48

Page 49: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

a Shoes app is a ruby object.

49

Page 50: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

your code lives inside of this object using blocks.

50

Page 51: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

it’s super easy.

51

Page 52: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

REALLY super easy.

52

Page 53: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

so, basic stuff.

53

Page 54: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

Shoes.app

54

Page 55: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

background

55

Page 56: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

background

56

Page 57: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

backgrounds can usehex codes too.

57

Page 58: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

layout works likeyou wish HTML and CSS works

58

Page 59: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

stacks

59

Page 60: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

all sub elements stack.

60

Page 61: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

flows

61

Page 62: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

all sub elements flow

62

Page 63: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

combine stacksand flows

to make complexlayouts

63

Page 64: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

two column with header

64

Page 65: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

the Shoes window is also

a flow.

65

Page 66: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

simple text

66

Page 67: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

edit_line

67

Page 68: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

edit_line

great for passwordz

68

Page 69: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

edit_box

69

Page 70: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

list_box

arrays and hashes are everywhere.

70

Page 71: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

radio

71

Page 72: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

check

72

Page 73: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

use instance variablesto reference controls.

73

Page 74: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

grabbing and showing input

74

Use the .text method to grab the text from the textbox.

Page 75: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

any object is fair game.even stacks and floats

75

Page 76: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

append to a stack

76

The @names section contains the names we add, and names get added when we click the button.

Page 77: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

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.

Page 78: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

Windows

78

Page 79: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

windows arelike new apps.

79

So instance variables are isolated.

Page 80: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

each screenis a url.

80

Page 81: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

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.

Page 82: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

shoes canplay music

and movies...

82

The video player plays mp3s, oggs, avis, movs, flvs and more.

Page 83: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

load images...

83

Page 84: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

...and open URLs in browsers.

84

Page 85: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

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.

Page 86: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

shoes can installlibraries for you.

86

When the app starts, these libraries get installed to the user’s home directory.

Page 87: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

you can alsoaccess OS stuff.

87

Page 88: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

88

Accessing standard dialogs and the clipboard is pretty simple.

Page 89: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

demos

89

Page 90: learning to walk in shoesblog.napcs.com/wp-content/uploads/2009/05/tccc_vi_shoes.pdf · you often see code that writes code. 26 Metaprogramming! arrays and hashes are important. Pay

http://github.com/napcs/shoes_demo_apps/

90

Either check it out with git or click the download link and unzip the package.