Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

keep on truckin

  • Loading branch information...
commit 54f89d5b8c1328ff8226fbe7efdeec378301241b 1 parent a152a2f
@jholton authored
Showing with 88 additions and 0 deletions.
  1. +23 −0 ex38.rb
  2. +65 −0 ex39.rb
View
23 ex38.rb
@@ -0,0 +1,23 @@
+ten_things = "Apples Oranges Crows Telephone Light Sugar"
+
+puts "Wait there's not 10 things in that list, let's fix that."
+
+stuff = ten_things.split(' ')
+more_stuff = %w(Day Night Song Frisbee Corn Banana Girl Boy)
+
+while stuff.length != 10
+ next_one = more_stuff.pop()
+ puts "Adding: #{next_one}"
+ stuff.push(next_one)
+ puts "There's #{stuff.length} items now."
+end
+
+puts "There we go: #{stuff}"
+
+puts "Let's do some things with stuff."
+
+puts stuff[1]
+puts stuff[-1] # whoa! fancy
+puts stuff.pop()
+puts stuff.join(' ') # what? cool!
+puts stuff.values_at(3,5).join('#') # super stellar!
View
65 ex39.rb
@@ -0,0 +1,65 @@
+# create a mapping of state to abbreviation
+states = {
+ 'Oregon' => 'OR',
+ 'Florida' => 'FL',
+ 'California' => 'CA',
+ 'New York' => 'NY',
+ 'Michigan' => 'MI'
+}
+
+# create a basic set of states and some cities in them
+cities = {
+ 'CA' => 'San Francisco',
+ 'MI' => 'Detroit',
+ 'FL' => 'Jacksonville'
+}
+
+# add some more cities
+cities['NY'] = 'New York'
+cities['OR'] = 'Portland'
+
+# puts out some cities
+puts '-' * 10
+puts "NY State has: ", cities['NY']
+puts "OR State has: ", cities['OR']
+
+# puts some states
+puts '-' * 10
+puts "Michigan's abbreviation is: ", states['Michigan']
+puts "Florida's abbreviation is: ", states['Florida']
+
+# do it by using the state then cities dict
+puts '-' * 10
+puts "Michigan has: ", cities[states['Michigan']]
+puts "Florida has: ", cities[states['Florida']]
+
+# puts every state abbreviation
+puts '-' * 10
+for state, abbrev in states
+ puts "%s is abbreviated %s" % [state, abbrev]
+end
+
+# puts every city in state
+puts '-' * 10
+for abbrev, city in cities
+ puts "%s has the city %s" % [abbrev, city]
+end
+
+# now do both at the same time
+puts '-' * 10
+for state, abbrev in states
+ puts "%s state is abbreviated %s and has city %s" % [
+ state, abbrev, cities[abbrev]]
+end
+
+puts '-' * 10
+# if it's not there you get nil
+state = states['Texas']
+
+if not state
+ puts "Sorry, no Texas."
+end
+
+# get a city with a default value
+city = cities['TX'] || 'Does Not Exist'
+puts "The city for the state 'TX' is: %s" % city
Please sign in to comment.
Something went wrong with that request. Please try again.