CS 330 Lecture 4 – Ruby
Agenda
- what ?s
- language features
- Ruby via examples
TODO
- Walk through RegexOne.
- On a 1/4 sheet, draft some regular expressions that match the following:
- Lines ending with a hyphenated word.
- Words with an internal uppercase letter.
- Lines lacking a semi-colon at their close. Don’t match lines that have a semi-colon followed by whitespace.
- Instances of the identifier
i
. Don’t match other occurrences ofi
.
Perry’s Stages of Cognitive Development
- Duality: I believe actions and beliefs are either right or wrong, and these labels are administered by certain trusted Authorities. We shut our eyes and ears to those labeled wrong; they deserve punishment and misfortune. Example: I’d use an Android phone if I were a monkey. But I’m not. Thus, this iPhone.
- Multiplicity: We are all Authorities, labels of right and wrong are purely arbitrary, and you who listen to others are all a bunch of sheep. I’m going to do what I think is right, and you can do your thing. Anyone that says otherwise is a dualist prig. Example: A phone is a phone. I had to steal this iPhone, but they were overpriced and I had just spent my paycheck on a new transmission.
- Relativism: Not all Authorities are equal and not all are right about everything. Some are charlatans, some are hypocrites, some have reasonable motivations but produce inconsistent outcomes, some are right about area X but grossly inexperienced with area Y, some are nobler and more consistent than others. Example: iPhones are great for people who can afford them and have a mind for design. Android phones are great for the developing world and tinkerers.
- Commitment: Based on what I’ve seen of the world, I trust much of what Authorities A, B, and C have to say. But I will keep evaluating new ones that come along, and careful weigh what A, B, and C say. Example: I like my iPhone. Those new devices from Motorola have some awesome features, though. I hope Apple copies them soon and there’s no lawsuit.
Code
aspect.rb
#!/usr/bin/env ruby
print 'Width: '
width = gets.to_f
print 'Height: '
height = gets.to_f
aspect_ratio = width / height
puts "Aspect ratio: #{aspect_ratio}"
spacer.rb
#!/usr/bin/env ruby
puts ARGV
sum = 0
# for i in 0...ARGV.size
# sum += File.stat(ARGV[i]).size
# end
ARGV.each do |item|
sum += File.stat(item).size
end
puts sum
rhymes.rb
#!/usr/bin/env ruby
require 'open-uri'
require 'json'
json = open("http://rhymebrain.com/talk?function=getRhymes&word=#{ARGV[0]}").read
rhymes = JSON.parse(json)
rhymes.each do |rhyme|
puts rhyme['word']
end
Haiku
on Perryian commitment
He is not the one
But I will still marry him
And then he will be
He is not the one
But I will still marry him
And then he will be