The error is p018arrays.rb:37:in '[]=' can't convert Float into String
<type error> from p018arrays.rb:37:in '<main>'
This is the code I am using:
# p018arrays.rb
# Arrays
# Empty array
var1 = []
# Array index starts from 0
puts var1[0]
# An array holdling a single number
var2 = [5]
puts var2[0]
# An array holding two strings
var3 = ['Hello', 'Goodbye']
puts var3[0]
puts var3[1]
flavour = 'mango'
# An array whose elements are pointing
# To three objects - a float, a string and an array
var4 = [80.5, flavour, [true, false]]
puts var4[2]
# A trailing comma is ignored
name = ['Satish', 'Talim', 'Ruby', 'Java']
puts name[0]
puts name[1]
puts name[2]
puts name[3]
# The next one outputs nil
# nil is Ruby's way of saying nothing
puts name[4]
# We can add anything!
puts name = 'Prune'
puts name[4]
# We can add anything!
name[5] = 4.33
puts name[5]
# We can add an array to an array
name[6] = [1, 2, 3]
puts name[6]
# Some methods on arrays
newarr = [45, 23, 1, 90]
puts newarr.sort
puts newarr.length
puts newarr.first
puts newarr.last
=begin
Method each (iterator) - extracts each element into lang
do end is a block of code
we shall talk about blocks soon,
variable lang refers to each item in the array as it goes through the
loop
=end
languages = ['Pune', 'Mumbai', 'Bangalore']
languages.each do |lang|
puts 'I love ' + lang + '!'
puts 'Don\'t you?'
end
# delete an entry in the middle and shift the remaining entries
languages.delete('Mumbai')
languages.each do |lang|
puts 'I love ' + lang + '!'
puts 'Don\'t you?'
end
···
--
Posted via http://www.ruby-forum.com/.