what is going on here, i dont understand why i keep getting conversion
errors. i am making a conversion from a string to and integer i dont
understand why it isnt working.
irb(main):036:0> def favorite_number
irb(main):037:1> puts "what is your favorite number?"
irb(main):038:1> s_num = gets.chomp
irb(main):039:1> i_num = s_num.to_i
irb(main):040:1> i_num = i_num+1
irb(main):041:1> puts "this is a better number" + i_num
irb(main):042:1> end
=> nil
irb(main):043:0> favorite_number
what is your favorite number?
5
TypeError: can't convert Fixnum into String
from (irb):41:in `+'
from (irb):41:in `favorite_number'
from (irb):43
what is going on here, i dont understand why i keep getting conversion
errors. i am making a conversion from a string to and integer i dont
understand why it isnt working.
because it needs to be a string when you print it.
irb(main):036:0> def favorite_number
irb(main):037:1> puts "what is your favorite number?"
irb(main):038:1> s_num = gets.chomp
irb(main):039:1> i_num = s_num.to_i
irb(main):040:1> i_num = i_num+1
irb(main):041:1> puts "this is a better number" + i_num
puts "this is a better number" + i_num.to_s
···
irb(main):042:1> end
=> nil
irb(main):043:0> favorite_number
what is your favorite number?
5
TypeError: can't convert Fixnum into String
from (irb):41:in `+'
from (irb):41:in `favorite_number'
from (irb):43
from :0
what is going on here, i dont understand why i keep getting conversion
errors. i am making a conversion from a string to and integer i dont
understand why it isnt working.
irb(main):036:0> def favorite_number
irb(main):037:1> puts "what is your favorite number?"
irb(main):038:1> s_num = gets.chomp
irb(main):039:1> i_num = s_num.to_i
irb(main):040:1> i_num = i_num+1
irb(main):041:1> puts "this is a better number" + i_num
irb(main):042:1> end
=> nil
irb(main):043:0> favorite_number
what is your favorite number?
5
TypeError: can't convert Fixnum into String
from (irb):41:in `+'
from (irb):41:in `favorite_number'
from (irb):43
from :0
Consider:
ratdog:~ mike$ irb
irb(main):001:0> puts "is this 5?" + 5
TypeError: can't convert Fixnum into String
from (irb):1:in `+'
from (irb):1
you might consider puts "this is a better number" + i_num.to_s, or using "this is a better number #{i_num}"
Hope this helps,
Mike
···
On 11-May-06, at 9:41 PM, corey konrad wrote:
from :0
irb(main):041:1> puts "this is a better number" + i_num
...
TypeError: can't convert Fixnum into String
from (irb):41:in `+'
from (irb):41:in `favorite_number'
from (irb):43
from :0
This happens because String#+ call #to_str on its argument -- so when you're using `+' to concatenate a string and another object, that object has to respond to #to_str, and not just #to_s.
class Numeric
# this isn't recommended, though
def to_str
to_s
end
end
what is going on here, i dont understand why i keep getting conversion
errors. i am making a conversion from a string to and integer i dont
understand why it isnt working.
because it needs to be a string when you print it.
(well, actually when you concatenate prior to printing)
···
irb(main):041:1> puts "this is a better number" + i_num
i just have a question i am not trying to be a smart ass either but
one thing i find strange is that the puts means put string, its a string
method, now when i use the length method to get the amount of characters
in a word it works fine....yet to put the length i get an error: cant
convert from fixnum to string problem....i have to convert the integer
returned to string even though i am using a string method? Shoulndt
string conversion be implied in the puts method since thats what people
are going to use it for or would that cause problems somehow?
···
This happens because String#+ call #to_str on its argument -- so when
you're using `+' to concatenate a string and another object, that object
has to respond to #to_str, and not just #to_s.
class Numeric
# this isn't recommended, though
def to_str
to_s
end
end
I wonder if this would work... I'm at work with no ruby
def favorite_number
puts "what is your favorite number?"
puts "this is a better number #{gets.chomp.to_i + 1 }"
end
···
On 5/12/06, Guest <pat.eyler@gmail.com> wrote:
Guest wrote:
> corey konrad wrote:
>> what is going on here, i dont understand why i keep getting conversion
>> errors. i am making a conversion from a string to and integer i dont
>> understand why it isnt working.
>
> because it needs to be a string when you print it.
(well, actually when you concatenate prior to printing)
>> irb(main):041:1> puts "this is a better number" + i_num
>
> puts "this is a better number" + i_num.to_s
what is going on here, i dont understand why i keep getting conversion
errors. i am making a conversion from a string to and integer i dont
understand why it isnt working.
because it needs to be a string when you print it.
(well, actually when you concatenate prior to printing)
irb(main):041:1> puts "this is a better number" + i_num
puts "this is a better number" + i_num.to_s
I dont know is there an easier way to do that? i mean i am a begining
programmer but i am really finding it difficult to join in the hype
about this language so far. Alot of times ruby is described as being
elegant i just havnt gotten that impression yet. It seems to just do
things in different ways than other languages for teh sake of being
different and thats all. If i am wrong then no big deal like i said i am
a beginging programmer but that is the impressio i am getting from it so
far.
It printed out "5" just fine. Is that the problem you were referring to?
(I'm using Ruby 1.8.4 on Linux).
···
On 5/12/06, corey konrad <0011@hush.com> wrote:
i just have a question i am not trying to be a smart ass either but
one thing i find strange is that the puts means put string, its a string
method, now when i use the length method to get the amount of characters
in a word it works fine....yet to put the length i get an error: cant
convert from fixnum to string problem....i have to convert the integer
returned to string even though i am using a string method? Shoulndt
string conversion be implied in the puts method since thats what people
are going to use it for or would that cause problems somehow?
how about this cin>> = num, lol i dont know ruby seems like a trade off
to me so far. i am just trying to understand all this hype about it. I
mean every book i am reading says its the best most awesome easiest to
use, just like using natural language, its elegant and beautiful and it
will make you cry because its so easy to use, etc etc. I am having a
hard time understanding that. If its true i would like to see it at some
point.
Mike Stok wrote:
···
On 11-May-06, at 9:53 PM, corey konrad wrote:
because it needs to be a string when you print it.
oh ok that makes sense, isnt there a getn method for getting number or
other data types or do i always have to use gets?
You can use something like:
num = Integer(STDIN.gets) rescue nil
or
num = Float(STDIN.gets) rescue nil
which do conversions and deal with trailing garbage.
Is there a way we can get the ruby-forum gateway to send each message twice?
corey konrad wrote:
···
Guest wrote:
Guest wrote:
corey konrad wrote:
what is going on here, i dont understand why i keep getting conversion errors. i am making a conversion from a string to and integer i dont understand why it isnt working.
because it needs to be a string when you print it.
(well, actually when you concatenate prior to printing)
irb(main):041:1> puts "this is a better number" + i_num
puts "this is a better number" + i_num.to_s
I dont know is there an easier way to do that? i mean i am a begining programmer but i am really finding it difficult to join in the hype about this language so far. Alot of times ruby is described as being elegant i just havnt gotten that impression yet. It seems to just do things in different ways than other languages for teh sake of being different and thats all. If i am wrong then no big deal like i said i am a beginging programmer but that is the impressio i am getting from it so far.
It printed out "5" just fine. Is that the problem you were referring to?
actually this one, i have to use + full_name.to_s + to avoid the error i
just found that strange. i mean i am using a string method the to_s
should be implicit shouldnt it? i dont know.
irb(main):030:0> def name_length
irb(main):031:1> p "what is your first name?"
irb(main):032:1> first = gets.chomp
irb(main):033:1> p "what is your last name?"
irb(main):034:1> last = gets.chomp
irb(main):035:1> full_length = first.length + last.length
irb(main):036:1> p "your full name has: " + full_length + "characters"
irb(main):037:1> end
=> nil
irb(main):038:0> name_length
"what is your first name?"
corey
"what is your last name?"
konrad
TypeError: can't convert Fixnum into String
from (irb):36:in `+'
from (irb):36:in `name_length'
from (irb):38
Yes, I am too currently looking for a language worthy of my "what is your favorite number" program. I guess ruby is just 260,000 lines of hype.
def favorite_number
print "what is your favorite number? "
puts "#{gets().to_i + 1} is better"
end
-- Daniel
···
On May 12, 2006, at 4:27 AM, corey konrad wrote:
how about this cin>> = num, lol i dont know ruby seems like a trade off
to me so far. i am just trying to understand all this hype about it. I
mean every book i am reading says its the best most awesome easiest to
use, just like using natural language, its elegant and beautiful and it
will make you cry because its so easy to use, etc etc. I am having a
hard time understanding that. If its true i would like to see it at some
point.
That depends on how you want to handle errors, with this implementation of getn
garbage input gets turned into 0 (which you may or may not want to happen.)
You don't need the STDIN, or the rescue, so you can pick whichever you deem appropriate for your situation:
num = gets.to_i # decimals only, bad input => 0
num = Integer(gets) # handles 0x, 0, 0b prefixes, bad input => ArgumentError
num = Integer(gets) rescue nil # as above but bad input => nil
And scanf is very useful for a raft of more complex cases (where you might want a C style scanf
What is appropriate depends on what you are trying to do.
Mike
···
On 11-May-06, at 10:57 PM, corey konrad wrote:
yeah i like your way alot better than num = Integer(STDIN.gets) rescue
nil
Guest wrote:
corey konrad wrote:
because it needs to be a string when you print it.
oh ok that makes sense, isnt there a getn method for getting number or
other data types or do i always have to use gets?