Archive for October, 2008

Notes from class 2 (20081015)

Wednesday, October 22nd, 2008


NOTES

* use irb all the time, it will help your Ruby skills become strong, like Luke learning to use the Force
- http://en.wikipedia.org/wiki/Interactive_Ruby_Shell
- http://www.ruby-lang.org/en/documentation/quickstart/ - http://www.ruby-lang.org/en/documentation/quickstart/
- http://errtheblog.com/posts/24-irb-mix-tape (don't get sidetracked on the rails, script/console is irb with the rails app loaded--still irb)

* ASCII # => character set Ruby 1.8 uses by default
"Ruby programs are written in 7-bit ASCI I, Kanji (using EUC or SJ I S), or UTF- 8. If a code set other than 7-bit ASCII is used, the KCODE option must be set appropriately, ..." -- Programming Ruby, page 302

* "", '' or double-quote-double-quote, single-quote-single-quote # => both are true, they don't equal false, however "".empty? or ''.empty? (String.empty?) can be used to force a boolean value

* :symbol # commonly used hash value for method calls, live forever (once instantiated)
a blog post on the topic: http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols

* if/elsif/ statements
Gotcha: the first variable will be assigned regardless of the second expression being false, for example:

# logic: var1 will be changed from nil to "dude" because it is not dependent on the second expression whatsoever
# you can see where logic would fail if the value of var1 was determined by the value of the condition
var1 = nil
var2 = nil
var3 = nil
condition = 1
from_elsewhere = 2
if (var1 = "dude" if 1 == 1) && (var2 = "awesome" if condition == from_elsewhere)
var3 = "rad"
end
puts "var1: #{var1}" # => "dude\n"
puts "var2: #{var2}" # => nil
puts "var2: #{var3}" # => nil

# logic: the value of var3 is only changed if both var1 and var2 are evaluated as not false or don't return false
var1 = nil
var2 = nil
var3 = nil
condition = 1
from_elsewhere = 1
if (var1 = "dude" if 1 == 1) && (var2 = "awesome" if condition == from_elsewhere)
var3 = "rad"
end
puts "var1: #{var1}" # => "dude\n"
puts "var2: #{var2}" # => "awesome\n"
puts "var3: #{var3}" # => "rad\n"

* Case see "Case Expressions" page 92 in Programming Ruby second edition (1.8 not 1.9) http://www.pragprog.com/titles/ruby/programming-ruby
case target
when comparison [, comparison ]… [ then | : ]
body
when comparison [, comparison ]… [ then | : ]
body

[ else
body ]
end

* tip for command line, or one liners
; or semi-colon to terminate lines, so:

(1..10) do |number|
puts number
end

# which can also be written in on one line using the semicolon, however this style is not as welcomed when shared with others:
(1..10) do |number|; puts number; end

* |n| are called “goal posts”, they are used for assigning a single generic variable to represent each item when iterating over a set, such as:
["awesome, "dude", "rad"].each {|n| puts n} # puts n three times, once for each item in the array
# the first time n == “awesome”, the second time n == “dude”, and the third time n == “rad”

# the same result can also be achieved like:
puts ["awesome, "dude", "rad"][0]
puts ["awesome, "dude", "rad"][1]
puts ["awesome, "dude", "rad"][2]

# the same result can also be achieved like:
variable_name = ["awesome, "dude", "rad"]
puts variable_name[0]
puts variable_name[1]
puts variable_name[2]

* RTM policy
When you have a question, are stuck, need help with Ruby, have an OS issue related to completing your assignments or learning about Ruby, please follow the following steps. If you’re lazy, too busy, or have better reasons than to use steps (1..5), please start at set 4 and ask your question instead of getting behind on your work, just be prepared to get some responses referring to self help options (don’t take offense), and be upfront about your reasons for skipping to step 5.

1. Test
- Work it out in irb to make sure you issue is not just invalid syntax, an undefined method, etc.
- write a test (will be covered later in the course)

2. Look for answer in the documentation, here a few of many available sources:
- ri via command line: $ ri Class#method (tip: http://rubylearning.com/satishtalim/ruby_ri_tool.html)
- book: Programming Ruby second edition (1.8 not 1.9) http://www.pragprog.com/titles/ruby/programming-ruby (tip: use the PDF version for text search)
- http://www.ruby-lang.org/en/documentation/ (many sources) or http://www.ruby-doc.org/

3. Ask on irc
Just connect to irc.freenode.net and go to #seattle.rb or #ruby-lang.
- http://www.zenspider.com/pipermail/ruby/2003-May/000874.html

4. Ask on a Ruby mailing list:
- UW Ruby: http://zenspider.com/mailman/listinfo/uw-ruby (signup)
- Seattle.rb: http://www.zenspider.com/Languages/Ruby/Seattle/List.html
- Ruby Talk: http://blade.nagaokaut.ac.jp/ruby/ruby-talk/index.shtml

5. Search
- google (you can try http://awesome-r.com–a little ruby search I whipped up)
- search the mailing list using google: “site: http://zenspider.com/pipermail/uw-ruby/ instances”, examplee
http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fzenspider.com%2Fpipermail%2Fuw-ruby%2F+instances

6. Email
- The instructor Ryan Davis, Aaron Patterson
- me, Riggs (Hello!), the TA (callme@1800aweso.me)
tip: if your question is about specific code, please provide the code in question, and a description of what you are trying to achieve with the provided code

7. Ask (in person)
- during class time after the lecture
- come to the user Seattle User group (bring your laptop or you will be bored), http://www.zenspider.com/Languages/Ruby/Seattle/

* textmate shortcut keys: command + “r” will run the code on the current page (saved or not)

* multiple line equations vs single line evaluation, general practices:
- single line: use a single character (optional, can be more chars), and {} / braces: (1..100).each {|n| puts n} # notice “do” is not used
- multiple line: use “do”, no braces, a word (optional, can be a single char):
(1..100).each do |num|
puts num
end #<-- remember to close the door on your way out

---

IMPORTANT URLS

* Class info
http://www.zenspider.com/~ryand/ruby/

* Homework
http://zenspider.com/~ryand/ruby/class

* Mailing list
http://zenspider.com/mailman/listinfo/uw-ruby (signup)
http://zenspider.com/pipermail/uw-ruby/ (archive)

* Quick ref
http://zenspider.com/Languages/Ruby/QuickRef.html

---

BOOKS

These books should be requirements unless you have an equivalent and efficient method of looking up information regarding the Ruby language:

* Programming Ruby second edition (1.8 not 1.9), by Dave Thomas, http://www.pragprog.com/titles/ruby/programming-ruby
This is a necessary resource for even the most leet Ruby programmer--if not to look stuff up, then to provide reference to others.

* Learn to Program by Chris Pines, http://www.pragprog.com/titles/fr_ltp/learn-to-program
Don't be dissuaded by the name if you already know how to program, in my opinion, the title should be, "Learn to Program in Ruby". You can work trough the simple command line exercises on a Sunday to acquire the basics, leaving you prepared and ready to move on to the really cool stuff the Ruby language has to offer.

* The Ruby Programming Language, By David Flanagan, Yukihiro Matsumoto, http://oreilly.com/catalog/9780596516178/

Autotest with images dot files

Wednesday, October 22nd, 2008

 

 

autotest_images_dotfiles

Oops!

Wednesday, October 22nd, 2008

Here is a better one.

homework1

Homework As Zipfile

Wednesday, October 22nd, 2008

Here is your homework as a zipfile.

homework