spoiling the youth with ruby (euruko 2010)

68
Spoiling The Youth With Ruby EURUKO 2010 Karel Minařík

Upload: karel-minarik

Post on 13-May-2015

4.290 views

Category:

Technology


1 download

DESCRIPTION

Slides from EURUKO 2010 talk about sharing experiences from teaching introductory courses to programming.

TRANSCRIPT

Page 1: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling The Youth With RubyEURUKO 2010

Karel Minařík

Page 2: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Karel Minařík

→ Independent web designer and developer („Have Ruby — Will Travel“)

→ Ruby, Rails, Git and CouchDB propagandista in .cz

→ Previously: Flash Developer; Art Director; Information Architect;… (see LinkedIn)

→ @karmiq at Twitter

→ karmi.cz

Page 3: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

I have spent couple of last years introducing spoiling humanities students to with the basics of PR0GR4MM1NG.(As a PhD student)

I’d like to share why and how I did it.

And what I myself have learned in the process.

Page 4: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

I don’t know if I’m right.

Page 5: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

But a bunch of n00bz was able to:

‣ Do simple quantitative text analysis (count number of pages, etc)

‣ Follow the development of a simple Wiki web application and write code on their own

‣ Understand what $ curl ‐i ‐v http://example.com does‣ And were quite enthusiastic about it

5 in 10 have „some experience“ with HTML

1 in 10 have „at last minimal experience“ with programming (PHP, C, …)

Page 6: Spoiling The Youth With Ruby (Euruko 2010)

ΓΝΩΘΙ ΣΕΑΥΤΟN

Socrates

Page 7: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Socrates is guilty of spoling the youth (ἀδικεῖν τούϛ τε νέουϛ

διαφθείροντα) and not acknowledging the gods that the city does, but some other new divinities (ἓτερα δὲ δαιμόνια καινά).

— Plato, Apology, 24b9

Page 8: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Some of our city DAIMONIA:

Students should learn C or Java or Lisp…

Web is for hobbyists…

You should write your own implementation of quick sort…

Project management is for „managers“…

UML is mightier than Zeus…

Design patterns are mightier than UML…

Test-driven development is some other new divinity…

Ruby on Rails is some other new divinity…

NoSQL databases are some other new divinities…

(etc ad nauseam)

Page 9: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Programming isa specific way of thinking.

Page 10: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Why Teach Programming (to non-programmers)?1

Page 11: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

„To use a tool on a computer, you need do little more than

point and click; to create a tool, you must understand the

arcane art of computer programming“

— John Maeda, Creative Code

Page 12: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Literacy

(reading and writing)

Page 13: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Page 14: Spoiling The Youth With Ruby (Euruko 2010)
Page 15: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Page 16: Spoiling The Youth With Ruby (Euruko 2010)

Complexity

Page 17: Spoiling The Youth With Ruby (Euruko 2010)

„Hack“

Page 18: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

File.read('pride_and_prejudice.txt'). split(' '). sort. uniq

Literacy

To most of my students, this:

is an ultimate, OMG this is soooooo cool hack

Although it really does not „work“ that well. That’s part of the explanation.

Page 19: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Ruby as an „Ideal” Programming Language?2

Page 20: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

There are only two hard things in Computer Science: cache invalidation and naming things.

— Phil Karlton

Page 21: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

The limits of my language mean the limits of my world(Die Grenzen meiner Sprache bedeuten die Grenzen meiner Welt)

— Ludwig Wittgenstein, Tractatus Logico-Philosophicus 5.6

Page 22: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

We use Ruby because it’s…

Expressive

Flexible and dynamic

Not tied to a specific paradigm

Well designed (cf. Enumerable)

Powerful(etc ad nauseam)

Ruby as an „Ideal” Programming Language?

Page 23: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

All those reasons are validfor didactic purposes as well.

Page 24: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Of course… not only Ruby…

Ruby as an „Ideal” Programming Language?

Page 25: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

www.headfirstlabs.com/books/hfprog/

Ruby as an „Ideal” Programming Language?

Page 27: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

5.times do  print "Hello. "end

Ruby as an „Ideal” Programming Language?

Page 28: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Let’s start with the basics...

Ruby as an „Ideal” Programming Language?

Page 29: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

An algorithm is a sequence of well defined and finite instructions. It starts from an initial state and terminates in an end state.

— Wikipedia

Page 30: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

1. Pour oil in the pan2. Light the gas3. Take some eggs4. ...

Algorithms and kitchen recipes

Page 31: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

SIMPLE ALGORITHM EXAMPLE

Finding the largest number from unordered list

1. Let’s assume, that the first number in the list is the largest.

2. Let’s look on every other number in the list, in succession. If it’s larger then previous number, let’s write it down.

3. When we have stepped through all the numbers, the last number written down is the largest one.

http://en.wikipedia.org/wiki/Algorithm#Example

Page 32: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

FORMAL DESCRIPTION IN ENGLISH

Input: A non‐empty list of numbers LOutput: The largest number in the list L

largest ← L0for each item in the list L≥1, do  if the item > largest, then

    largest ← the itemreturn largest

Finding the largest number from unordered list

Page 33: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

DESCRIPTION IN PROGRAMMING LANGUAGE

1 #include <stdio.h>2 #define SIZE 113 int main()4 {5   int input[SIZE] = {1, 5, 3, 95, 43, 56, 32, 90, 2, 4, 19};6   int largest = input[0];7   int i;8   for (i = 1; i < SIZE; i++) {9     if (input[i] > largest)10       largest = input[i];11   }12   printf("Largest number is: %d\n", largest);13   return 0;14 }

C

Finding the largest number from unordered list

Page 34: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

DESCRIPTION IN PROGRAMMING LANGUAGE

1 class MaxApp {2   public static void main (String args[]) {3     int[] input = {1, 5, 3, 95, 43, 56, 32, 90, 2, 4, 19};4     int largest = input[0];5     for (int i = 0; i < input.length; i++) {6       if (input[i] > largest)7         largest = input[i];8     }9     System.out.println("Largest number is: " + largest + "\n");10   }11 }

Java

Finding the largest number from unordered list

Page 35: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

DESCRIPTION IN PROGRAMMING LANGUAGE

1 input = [1, 5, 3, 95, 43, 56, 32, 90, 2, 4, 19]2 largest = input.first3 input.each do |i|4   largest = i if i > largest5 end6 print "Largest number is: #{largest} \n"

Ruby

Finding the largest number from unordered list

Page 36: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

1 input = [1, 5, 3, 95, 43, 56, 32, 90, 2, 4, 19]2 largest = input.first3 input.each do |i|4   largest = i if i > largest5 end6 print "Largest number is: #{largest} \n"

largest ← L0for each item in the list L≥1, do if the item > largest, then largest ← the itemreturn largest

Finding the largest number from unordered list

Page 37: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

What can we explain with this example?

‣ Input / Output‣ Variable‣ Basic composite data type: an array (a list of items)

‣ Iterating over collection‣ Block syntax‣ Conditions‣ String interpolation

# find_largest_number.rb

input = [3, 6, 9, 1]

largest = input.shift

input.each do |i|

largest = i if i > largest

end

print "Largest number is: #{largest} \n"

Page 38: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

That’s... well, basic...

Page 39: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

  # Function definition:

  def max(input)

    largest = input.shift

    input.each do |i|

      largest = i if i > largest

    end

    return largest

  end

  # Usage:

  puts max( [3, 6, 9, 1] )

  # => 9

The concept of a function (method)…

BUT, YOU CAN EXPLAIN ALSO…

Page 40: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

… the concept of polymorphydef max(*input)  largest = input.shift  input.each do |i|    largest = i if i > largest  end  return largestend

# Usageputs max( 4, 3, 1 )puts max( 'lorem', 'ipsum', 'dolor' )puts max( Time.mktime(2010, 1, 1),          Time.now,          Time.mktime(1970, 1, 1) )

puts max( Time.now, 999 ) #=> (ArgumentError: comparison of Fixnum with Time failed)

BUT, YOU CAN EXPLAIN ALSO…

Page 41: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

… that you’re doing it wrong — most of the time :)

# Enumerable#maxputs [3, 6, 9, 1].max

BUT, YOU CAN EXPLAIN ALSO…

Page 42: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Pick an example and stick with it

Switching contexts is distractingWhat’s the difference between “ and ‘ quote?

What does the @ mean in a @variable ?

„OMG what is a class and an object?“

WHAT I HAVE LEARNED

Page 43: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

"hello".reverse

[1, 14, 7, 3].max

["banana", "lemon", "ananas"].size

["banana", "lemon", "ananas"].sort

["banana", "lemon", "ananas"].sort.last

["banana", "lemon", "ananas"].sort.last.capitalize

5.times do

  print "Hello. "

end

Interactive Ruby console at http://tryruby.org

Page 44: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Great „one-click“  installer for Windows

Page 45: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Great resources, available for free

www.pine.fm/LearnToProgram (first edition)

Page 46: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Like, tooootaly excited!

Why’s (Poignant) Guide To Ruby

Page 47: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

def sum(list) total = 0 for i in 0..list.size-1 total = total + list[i] end totalend

def sum(list) total = 0 list.each do |item| total += item end totalend

def test_sum_empty sum([]) == 0end

# ...

def sum(list) total = 0 list.each{|i| total += i} totalend

def sum(list) list.inject(0){|a,b| a+b}end

class Array def sum inject{|a,b| a+b} endend

describe "Enumerable objects should sum themselves" do

it 'should sum arrays of floats' do [1.0, 2.0, 3.0].sum.should == 6.0 end

# ...end

# ...

Ruby will grow with you

1

2

3

4

5

6

7

www.entish.org/wordpress/?p=707

(„The Evolution of a Ruby Programmer“)

Page 48: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

If you’re teaching/training, try to learn something you’re really bad at.

‣ Playing a musical instrument‣ Drawing‣ Dancing‣ Martial arts

‣ …

It gives you the beginner’s perspective

WHAT I HAVE LEARNED

Page 49: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Web as a Platform3

Page 50: Spoiling The Youth With Ruby (Euruko 2010)

www.shoooes.net

2009

Page 52: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

But wait! Almost all of us are doing web applications today.

Page 53: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Why web is a great platform?

Transparent: view-source all the waySimple to understandSimple and free development toolsLow barrier of entryExtensive documentationRich platform (HTML5, „jQuery“, …)Advanced development platforms (Rails, Django, …)Ubiquitous(etc ad nauseam)

Page 54: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

All those reasons are validfor didactic purposes as well.

Page 55: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

http://github.com/stunome/kiwi/

CODED LIVE IN CLASS, THEN CLEANED UP AND PUT ON GITHUB

Page 56: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Why a Wiki?

Well known and understood piece of software with minimal and well defined feature set (www.c2.com/cgi/wiki?WikiPrinciples)

Used on a daily basis (Wikipedia)

Feature set could be expanded based on individual skills

Page 57: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Sinatra.rb

www.sinatrarb.com

2010

Page 58: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Sinatra.rb

Why choose Sinatra?

Expose HTTP! GET / → get("/") { ... }

Simple to install and run $ ruby myapp.rb

Simple to write „Hello World“ applications

Page 59: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

$ curl --include --verbose http://www.example.com

* About to connect() to example.com port 80 (#0)* Trying 192.0.32.10... connected* Connected to example.com (192.0.32.10) port 80 (#0)> GET / HTTP/1.1...< HTTP/1.1 200 OK...< <HTML><HEAD> <TITLE>Example Web Page</TITLE>...* Closing connection #0

Expose HTTP

Page 60: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Expose HTTP

# my_first_web_application.rb

require 'rubygems'require 'sinatra'

get '/' do "Hello, World!"end

get '/time' do Time.now.to_send

Page 61: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

First „real code“ — saving pages

http://github.com/stunome/kiwi/commit/33fb87

 .gitignore       |    2 ++ application.rb   |   22 ++++++++++++++++++++++ views/form.erb   |    9 +++++++++ views/layout.erb |   12 ++++++++++++ 4 files changed, 45 insertions(+), 0 deletions(‐)

Page 62: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Rubyhttp://github.com/stunome/kiwi/commit/33fb87

Refactoring code to OOP (Page class)

 application.rb |    4 +++‐ kiwi.rb        |    7 +++++++ 2 files changed, 10 insertions(+), 1 deletions(‐)

Page 63: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

www.pivotaltracker.com/projects/74202

Teaching real-world processes and tools

Page 64: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Teaching real-world processes and tools

http://github.com/stunome/kiwi/

Page 65: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

The difference between theory and practice is bigger in practice then in theory.

(Jan L. A. van de Snepscheut or Yogi Berra, paraphrase)

Page 66: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

Automated testing and test-driven development

Cucumber

Deployment (Heroku.com)

What I had no time to cover (alas)

Page 67: Spoiling The Youth With Ruby (Euruko 2010)

Spoiling the Youth With Ruby

A common curriculum for teaching Ruby(for inspiration, adaptation, discussion, …)

Code shared on GitHub

TeachingRuby (RailsBridge) (http://teachingkids.railsbridge.org)

Try Camping (http://github.com/judofyr/try-camping)

http://testfirst.org ?

What would be great to have

Page 68: Spoiling The Youth With Ruby (Euruko 2010)

Questions!