["apple", "banana", "cherry"]
Try this in IRB:
fruits = ["apple", "banana", "cherry"] fruits[1]
Did you get the result you expected?
Why or why not?
When counting,
humans start at one,
but computers start at zero.
So the first item in an array is number zero, not number one.
Try this:
fruits[99]
Did you get the result you expected?
Why or why not?
fruits.last fruits.first fruits.reverse fruits.shuffle
fruits.join
fruits.join(" and ")
Note that to_s doesn't work right on arrays:
fruits.to_s puts fruits.to_s
fruits.each do |fruit| puts fruit end
each is like while for arraysfruits.each do means "for each item inside this array, do this"|fruit| means "put the current item into a variable named fruit"puts fruit means "print out the value of this variable"end means "we're done with the loop now" :-)Given this array:
fruits = ["apple", "banana", "cherry"]
write a program that prints:
yrrehc ananab elppa
The [] operator works for assignment as well.
fruits[0] = "Apricot" fruits[1] = "Blueberry" puts fruits.first
The include? method checks if an object is inside an array or not.
fruits.include? "apple" true fruits.include? "pizza" false
I'd like you to refactor your old hello.rb program to use the include? method to check if someone is your enemy.
/
#