Can't convert Float into String

A

Anthony Ob

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
 
R

Rahul Kumar

Anthony said:
puts name = 'Prune'
puts name[4]
# We can add anything!
name[5] = 4.33


you have reassigned name to a string. Now you are inputting a float into
it.
That line is an error.

See this (from irb):

ruby-1.9.2-head > name = "avcddf"
=> "avcddf"
ruby-1.9.2-head > name[4]
=> "d"
ruby-1.9.2-head > name[4]=0.3
TypeError: can't convert Float into String
from (irb):67:in `[]='
 
R

Rahul Kumar

On second thought, if you are actually trying to stuff a float into a
String,
you would do this.

name = "Jack"
name[2] = 4.55.to_s
 
Y

Y. NOBUOKA

The String object, "Prune", is substituted for variable [name] in the
33rd line.

[33rd line currently] puts name = 'Prune'

I think, however, what you want to do is to add the String object to the array.
So, you should change the 33rd line as follows.

[33rd line after changing] puts name[4] = 'Prune'


2010/10/2 Anthony Ob said:
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
 
B

Brian Candler

Anthony said:
puts name = 'Prune'

That assigns 'Prune' to name (so name is no longer pointing to an Array,
it's pointing to a String), then prints the value. It's the same as:

name = 'Prune'
puts name

Maybe meant:

name[4] = 'prune'

or perhaps

puts name == 'Prune'

(which would have shown 'false' without changing the array)
puts name[4]

Given that name is a String at this point, that prints character number
4 (the 5th character). This will print the byte code 101 in Ruby 1.8,
and the string "e" in ruby 1.9
# We can add anything!
name[5] = 4.33

But you can't set the nth character of a string to be a floating point
value.
 
A

Anthony Ob

Brian said:
Anthony said:
puts name = 'Prune'

That assigns 'Prune' to name (so name is no longer pointing to an Array,
it's pointing to a String), then prints the value. It's the same as:

name = 'Prune'
puts name

Maybe meant:

name[4] = 'prune'

or perhaps

puts name == 'Prune'

(which would have shown 'false' without changing the array)
puts name[4]

Given that name is a String at this point, that prints character number
4 (the 5th character). This will print the byte code 101 in Ruby 1.8,
and the string "e" in ruby 1.9
# We can add anything!
name[5] = 4.33

But you can't set the nth character of a string to be a floating point
value.






Updated code, got rid of the e:




# 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!
name = 'Pune'
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top