Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

loops and arrays

  • Loading branch information...
commit bf6a77a01de79b4d5ccdc1d587d7b48964ed6bb6 1 parent fdde209
@chronicole authored
Showing with 33 additions and 0 deletions.
  1. +33 −0 ex32.rb
View
33 ex32.rb
@@ -0,0 +1,33 @@
+the_count = [1,2,3,4,5]
+fruits = ['apples', 'oranges', 'pears', 'apricots']
+change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
+
+# this first kind of for-loop goes through an array
+for number in the_count
+ puts "This is count #{number}"
+end
+
+# same as above, but using a block instead
+fruits.each do |fruit|
+ puts "A fruit of type: #{fruit}"
+end
+
+# also we can go through mixed arrays too
+for i in change
+ puts "I got #{i}"
+end
+
+# we can also build arrays, first start with an empty one
+elements = []
+
+# then use a range object to do 0 to 5 counts
+for i in (0..5)
+ puts "Adding #{i} to the list."
+ # push is a function that arrays understand
+ elements.push(i)
+end
+
+# now we can puts them out too
+for i in elements
+ puts "Element was: #{i}"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.