mith digital dialogues: intro to programming for humanists (with ruby)

30
A Humanist's Introduction to Programming (with Ruby) Wayne Graham & Joe Gilbert MITH, Oct. 19, 2010

Upload: joegilbert

Post on 13-Jul-2015

573 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

A Humanist's Introduction to Programming

(with Ruby)

  

Wayne Graham & Joe GilbertMITH, Oct. 19, 2010

Page 2: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

What does that mean?

(We're still figuring it out.)

Page 3: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Why teach programming to humanities scholars?

Page 4: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Jean Bauer

Early American Foreign Service Database

Page 5: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Mapping Taqwacore: the Kominas

Page 6: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Why program?

• Express complex logic or relationships• Perform computations• Do things that take a long time or are difficult for humans to do (counting, comparing, repeating, graphing)

Page 7: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Why program?  "when you don't create things,   you become defined by your    tastes rather than ability. your   tastes only narrow & exclude   people. so create."    why the lucky stiff (@_why)

Page 8: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

What is a programming language?

• An artificial language with a limited purpose

• A means of expressing computations (math) and algorithms (logic) 

Page 9: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

What is a programming language?..like human languages in some ways!

• Syntax (form)

• Semantics (meaning)o signs/words (variables, symbols, numbers, strings)

o expressionso narrative ("flow," decisions, conditions, loops)o complex entities (methods, structures, & objects)

Page 10: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Why Ruby?

• General purpose• Object-oriented• Usable locally or over the web• English-like syntax and useful built-in features• "Fun" to write

Other commonly used languages: C, C++, Java, PHP, Python, JavaScript, ActionScript

Page 11: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

 

"...trying to make Ruby natural, not simple." Yukihiro Matsumoto aka "Matz"

Page 12: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Why not Ruby?

• Not as easy to run on the web as PHP (or JavaScript)

• Used less often than PHP, and major platforms (WordPress, Drupal, Omeka) use PHP

• Ruby isn't Rails

• Object-oriented languages are conceptually difficult to grasp

Page 13: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

What we will cover

• What kinds of information do we use in programs?

• How can we store and reuse information?

• How can we describe collections of information?

• What operations can we perform with information?

• How can we repeat steps?

• What if we only want to do things under certain conditions?

Page 14: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

What we won't cover

• Building a database driven website

• Design patterns

• Testing code

• Inheritance

• Software design

• Modules

• Mixins

• Procs

• Lambdas

• Coercion (Monkey Patching)

Page 15: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Try it out!

irb 

http://www.ruby-lang.org/

http://tryruby.org/

http://rubyinstaller.org/

Page 16: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

We work with a few basic types of information

Numbers like 1, 4000, -33.3

Text, including characters, words, sentences, and paragraphs

Page 17: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Types of informationnumbers, letters, boolean values

integers:4, 1040, -55, 9999

floating-point numbers:1.1, 0.444, 9999.0001, -3.33

text (strings):"a", 'cat', "The quick brown fox jumped over the lazy dogs.", '8 keys', '7’

boolean (yes or no?):true, false

Page 18: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Variables"words" that hold information

 > the_lonliest_number = 1=> 1 > truth = "beauty"=> "beauty"

Page 19: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Collections

Page 20: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Collectionstext, numbers...collections? Array:> presidents = ["Jefferson", "Madison", "Monroe"]=> ["Jefferson", "Madison", "Monroe"] presidents[0] = "Jefferson", presidents[1] = "Madison", etc. Associative array ("hash"): 

> states = {"VA" => "Virginia", "MD" => "Maryland"}=> {"VA" => "Virginia", "MD" => "Maryland"}

states["VA"] = "Virginia", states["MD"] = "Maryland"

Page 21: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Operatorsarithmetic and moremy_variable = 5my_other_variable = "hi" > my_variable + 2 => 7 > my_variable * 3=> 15 > my_other_variable + " there!"=> "hi there!" > presidents = presidents + ["Washington"]=> ["Jefferson", "Madison", "Monroe", "Washington"] > presidents = presidents - ["Washington"]=> ["Jefferson", "Madison", "Washington"]

What happens with presidents * 2?

Page 22: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Type castingduck typing and type casting

• Quotes are meaningful > my_favorite_number = '2'=> "2" > my_favorite_number + 2TypeError: can't convert Fixnum into String > my_favorite_number.to_i + 2=> 4

Page 23: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Special Symbols math operators

+ addition (and concatenation)- subtraction/ division* multiplication

assignment operators 

= assign a value 

+= add, then assign a value 

other operatorsequivalence, non-equivalence, boolean (and, or, etc.)

Page 24: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Printing things to the screen

puts "Doctor Who" puts tardis puts doctors[0]

puts doctors["David Tennant"] puts "My favorite episode is " + best_episode

Page 25: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Do something!

Store your street address, city, state, and zip code in variables (or even better, a hash!), then print them in the usual format:

Joseph Gilbert160 McCormick RoadCharlottesville, VA 22902

address = {'name' => 'Joseph Gilbert', 'street' => '160 McCormick Road', 'city' => 'Charlottesville', 'state' => 'VA', 'zip' => '22902'}puts address['name']puts address['street']puts address['city'] + ', ' + address['state'] + ' ' + address['zip']

Page 26: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Repeating yourself 

> puts presidents[0]Jefferson=>nil

> puts presidents[1]Madison=> nil

> puts presidents[2]Monroe=> nil

this isn't fun or efficient!

Page 27: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Repetition: "iterating" with each

> presidents.each do |president| > puts president> endJeffersonMadisonMonroe=> ["Jefferson", "Madison", "Monroe"]

Page 28: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

.eachfor hashes

states = {"VA" => "Virginia", "MD" => "Maryland"}

states.each do |code, state| puts code.to_s + " is the postal code for " + state.to_send

Page 29: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Do Something, pt. 2

Create a collection of these authors and the year they passed away; print the collection in the following format:

Charles Dickens passed away in 1870.

Charles Dickens, 1870William Thackeray, 1863Anthony Trollope, 1882Gerard Manley Hopkins, 1889

authors = {"Charles Dickens" => "1870", "William Thackeray" => "1863", "Anthony Trollope" => "1882", "Gerard Manley Hopkins" => "1889"}

authors.each do |author, year|  puts author.to_s + " passed away in " + year.to_send

Page 30: MITH Digital Dialogues: Intro to Programming for Humanists (with Ruby)

Resources• Learn to Program

http://pine.fm/LearnToProgram/

• Why's Poignant Guide to Rubyhttp://mislav.uniqpath.com/poignant-guide/

• Ruby Documentationhttp://ruby-doc.org/core/

• “Pick-axe Book” http://ruby-doc.org/docs/ProgrammingRuby/ 

• Teach Me to Codehttp://www.teachmetocode.com

• RubyTu.be

• Rubylearning.com