CS 330 Lecture 4 – Ruby
February 1, 2016 by Chris Johnson. Filed under
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
cs330, lectures, spring 2016.
Agenda
- what ?s
- program this
- examples
- calculate an aspect ratio
- list rhymes
- Advent of Code #1
- merging two files
TODO
- If your Ruby expertise is minimal, check out tryruby.org. No 1/4 sheet.
Note
Today we switch to another language for writing scripts: Ruby. We won’t hit in full detail, just enough to be able to move around in it. Really, we’re learning just enough to move on to our next topic: regular expressions.
Ruby is just a programming language. We don’t want to get caught up in superficial details. So, let’s begin our discussion today with a little segment I like to call Program This:
You are an oppressive office manager. Each year you record in a comma-separated file how many typos you and your employees make in their correspondence. For instance, file 2013 looks like this:
pendleton.patty,27
tater.dick,0
dollar.bill,52
keys.charlotte,2
...
File 2014 looks similar. And 2015. Sometimes employees leave. Sometimes you hire new ones. Sometimes.
To produce some graphs for your organization’s centennial report, you want to merge all these files into one:
id,2013,2014,...
pendleton.patty,27,22,...
tater.dick,0,0,...
dollar.bill,52,,...
keys.charlotte,2,1,...
todian.gus,,5,...
...
When employees leave or join, their missing entries are left blank. With a neighbor, write pseudocode to accomplish this merge. Don’t worry about syntax, but do express your loops, conditionals, calls to standard functions, etc. Test your pseudocode!
We’ll implement a possible solution to this in Ruby after we do some simpler examples.
Code
aspect
#!/usr/bin/env ruby
puts ARGV[0].to_f / ARGV[1].to_f
rhymes
#!/usr/bin/env ruby
require 'open-uri'
require 'json'
url = "http://rhymebrain.com/talk?function=getRhymes&word=#{ARGV[0]}"
json = open(url).read
rhymes = JSON.parse(json)
rhymes.each do |rhyme|
puts rhyme['word'] if rhyme['score'] >= 300
end
advent
#!/usr/bin/env ruby
directions = File.read('advent.txt')
# puts directions
ups = directions.count('(')
downs = directions.count(')')
puts ups - downs