A Humanist's Introduction to Programming (with Ruby) Wayne Graham & Joe Gilbert MITH, Oct. 19, 2010
Jul 13, 2015
A Humanist's Introduction to Programming
(with Ruby)
Wayne Graham & Joe GilbertMITH, Oct. 19, 2010
What does that mean?
(We're still figuring it out.)
Why teach programming to humanities scholars?
Jean Bauer
Early American Foreign Service Database
Mapping Taqwacore: the Kominas
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)
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)
What is a programming language?
• An artificial language with a limited purpose
• A means of expressing computations (math) and algorithms (logic)
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)
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
"...trying to make Ruby natural, not simple." Yukihiro Matsumoto aka "Matz"
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
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?
What we won't cover
• Building a database driven website
• Design patterns
• Testing code
• Inheritance
• Software design
• Modules
• Mixins
• Procs
• Lambdas
• Coercion (Monkey Patching)
Try it out!
irb
http://www.ruby-lang.org/
http://tryruby.org/
http://rubyinstaller.org/
We work with a few basic types of information
Numbers like 1, 4000, -33.3
Text, including characters, words, sentences, and paragraphs
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
Variables"words" that hold information
> the_lonliest_number = 1=> 1 > truth = "beauty"=> "beauty"
Collections
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"
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?
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
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.)
Printing things to the screen
puts "Doctor Who" puts tardis puts doctors[0]
puts doctors["David Tennant"] puts "My favorite episode is " + best_episode
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']
Repeating yourself
> puts presidents[0]Jefferson=>nil
> puts presidents[1]Madison=> nil
> puts presidents[2]Monroe=> nil
this isn't fun or efficient!
Repetition: "iterating" with each
> presidents.each do |president| > puts president> endJeffersonMadisonMonroe=> ["Jefferson", "Madison", "Monroe"]
.eachfor hashes
states = {"VA" => "Virginia", "MD" => "Maryland"}
states.each do |code, state| puts code.to_s + " is the postal code for " + state.to_send
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
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