Forum: Ruby conversion issues

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Corey K. (Guest)
on 2006-05-12 05:41
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
Guest (Guest)
on 2006-05-12 05:47
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.


>
> 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
Mike S. (Guest)
on 2006-05-12 05:50
(Received via mailing list)
On 11-May-06, at 9:41 PM, corey konrad wrote:

> 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
         from :0

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

--

Mike S. <removed_email_address@domain.invalid>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
Corey K. (Guest)
on 2006-05-12 05:53
>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?

Thanks
Guest (Guest)
on 2006-05-12 05:56
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
Corey K. (Guest)
on 2006-05-12 06:07
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.
Daniel -. (Guest)
on 2006-05-12 06:09
(Received via mailing list)
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
Daniel H. (Guest)
on 2006-05-12 06:12
(Received via mailing list)
On May 12, 2006, at 3:53 AM, corey konrad wrote:

> 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 scanf:

require 'scanf'
scanf("%d") # -> [10]
scanf("%f") # -> [10.0]

-- Daniel
Mike S. (Guest)
on 2006-05-12 06:15
(Received via mailing list)
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.

ratdog:~ mike$ irb
irb(main):001:0> Integer("    0xE84C\n")
=> 59468

I use these in quick and dirty scripts quite often, whether it's good
form I don't know :-)

Mike

--

Mike S. <removed_email_address@domain.invalid>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
Corey K. (Guest)
on 2006-05-12 06:22
Daniel H. wrote:
> On May 12, 2006, at 3:53 AM, corey konrad wrote:
>
>> 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 scanf:
>
> require 'scanf'
> scanf("%d") # -> [10]
> scanf("%f") # -> [10.0]
>
> -- Daniel


hmm i cant find a description of scanf in the documentation for some
reason.
Corey K. (Guest)
on 2006-05-12 06:27
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 S. 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.
>
> ratdog:~ mike$ irb
> irb(main):001:0> Integer("    0xE84C\n")
> => 59468
>
> I use these in quick and dirty scripts quite often, whether it's good
> form I don't know :-)
>
> Mike
>
> --
>
> Mike S. <removed_email_address@domain.invalid>
> http://www.stok.ca/~mike/
>
> The "`Stok' disclaimers" apply.
Guest (Guest)
on 2006-05-12 06:36
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?

There could be:

irb(main):001:0> def getn
irb(main):002:1> foo = gets
irb(main):003:1> foo.to_i
irb(main):004:1> end
=> nil
irb(main):005:0> bar = getn
123
=> 123
irb(main):006:0> bar
=> 123
irb(main):007:0> bar.class
=> Fixnum
irb(main):008:0>


or

irb(main):008:0> def getaray
irb(main):009:1> foo = gets
irb(main):010:1> foo.split(' ')
irb(main):011:1> end
=> nil
irb(main):012:0> bar = getaray
foo bar baz
=> ["foo", "bar", "baz"]
irb(main):013:0> bar
=> ["foo", "bar", "baz"]
irb(main):014:0>


(neither of these is well written, but work for a proof of concept)

>
> Thanks
Daniel H. (Guest)
on 2006-05-12 06:45
(Received via mailing list)
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.

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
Corey K. (Guest)
on 2006-05-12 06:56
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?
>
> There could be:
>
> irb(main):001:0> def getn
> irb(main):002:1> foo = gets
> irb(main):003:1> foo.to_i
> irb(main):004:1> end
> => nil
>
Corey K. (Guest)
on 2006-05-12 07:16
The goal of ruby is to make programming easier and more enjoyable right?
So isnt it important that a language with that as its goal can express
simple programs in a simple and intuitive way? I am a begining
programmer and i find ruby counter intuitive so far and that worries me
and makes me wonder if i am wasting my time with. I want to learn how to
write software. Thats the bigger picture i am in right now. Its not
personal,  I am just trying to get my feet wet here and make sense of
all this stuff.

> 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
Corey K. (Guest)
on 2006-05-12 07:20
i dont know there is always someone who takes something personal in
these chat forums, more so with perl than ruby i just dont understand
the romantic zealotry surrounding computer programming languages i
guess.



>
>> 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
Mike S. (Guest)
on 2006-05-12 07:22
(Received via mailing list)
On 11-May-06, at 10:57 PM, corey konrad wrote:

>>> number or
>>> other data types or do i always have to use gets?
>>
>> There could be:
>>
>> irb(main):001:0> def getn
>> irb(main):002:1> foo = gets
>> irb(main):003:1> foo.to_i
>> irb(main):004:1> end
>> => nil

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

--

Mike S. <removed_email_address@domain.invalid>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
carlos tirado (Guest)
on 2006-05-12 07:28
(Received via mailing list)
On 5/11/06, corey konrad <removed_email_address@domain.invalid> wrote:
> The goal of ruby is to make programming easier and more enjoyable right?

Matz (Ruby's creator) >
No language can be perfect for everyone. I tried to make Ruby perfect
for me, but maybe it's not perfect for you. The perfect language for
Guido van Rossum is probably Python.
http://www.artima.com/intv/rubyP.html
Logan C. (Guest)
on 2006-05-12 07:37
(Received via mailing list)
On May 11, 2006, at 10:43 PM, Daniel H. wrote:

>> will make you cry because its so easy to use, etc etc. I am having a
>   puts "#{gets().to_i + 1} is better"
> end
>
> -- Daniel
>

I agree completely. Java is much better for the my favorite number
program:

% cat favorite_number.java
import java.io.*;

class FavoriteNumber {
   public static void main(String[] args) throws Exception {
     BufferedReader in = new BufferedReader(new InputStreamReader
(System.in));
     String num;
     System.out.print("What's your favorite number? ");
     num = in.readLine( );
     String better_num = (new Integer(Integer.parseInt(num) +
1)).toString();

     System.out.println(better_num + " is better.");
   }
}

See?

And it's so intuitive!
Ryan L. (Guest)
on 2006-05-12 08:02
(Received via mailing list)
Hi Corey,

Ruby wasn't necessarily written to make programming easier and more
enjoyable. It many ways it depends on who is using it. For those of us
coming from programming in languages like Java (look at the example
from Logan, ouch) it is quite refreshing. I'm not sure what your
history in programming is, but if you are new to programming or have
programmed in something much different than Ruby, I'm sure it can be
confusing at first.

Also keep in mind that programming can get very frustrating at times.
I'm sure just about every programmer reading this can remember a time
when they were pounding their keyboards and about to throw their
monitor out the window because of some stupid problem. But when that
problem is solved, or a complicated algorithm works perfectly, it is a
really great feeling. Plus it is nice to have such power of the
computer to make it do anything you want, instead of being a slave to
it and other people's programs.

The truth is, Ruby may not be right for you. That is fine. There is no
good reason for you to program in something that doesn't fit you
(well, unless you get paid a lot to do it ;)

But seriously, if you feel that Ruby doesn't work for you, check out
Python (http://www.python.org/) or maybe Perl (http://www.perl.org/)
or even Java (http://java.sun.com/). There are a lot of programming
languages out there, and it never hurts to give some of them a try.

But for most of us here, Ruby just fits.

Regards,
Ryan
Dave B. (Guest)
on 2006-05-12 08:05
(Received via mailing list)
Hi Corey,

> The goal of ruby is to make programming easier and more enjoyable right?
> So isnt it important that a language with that as its goal can express
> simple programs in a simple and intuitive way? I am a begining
> programmer and i find ruby counter intuitive so far and that worries me
> and makes me wonder if i am wasting my time with. I want to learn how to
> write software. Thats the bigger picture i am in right now. Its not
> personal,  I am just trying to get my feet wet here and make sense of
> all this stuff.

Might it be fair to say that it's actually programming that you're
having difficulty with rather than Ruby at this stage? Chris P.'s
tutorial (which I assume you're using) isn't trying to show you how
awesome Ruby is, it's showing you how to program.

There are probably languages that are simpler for a beginner programmer
(probably QBasic if you can get your hands on it), but Chris P.'s
tutorial focusses on basic concepts a few at a time to get you up to
speed. That means leaving out a bunch of what makes Ruby cool. And once
you have mastered the material in Learning to Program, you will already
be far more productive in Ruby than you'd be after similar time on
Basic.

Nothing's stopping you from learning more than one language so you can
choose between them and compare them more accurately. Indeed, hardly any
of us ruby-talkers use _only_ Ruby, and I doubt there's anyone who would
recommend it. But you might want to save your judgment at least until
you are a programmer.

Cheers,
Dave
Mike H. (Guest)
on 2006-05-12 08:11
(Received via mailing list)
Is there a way we can get the ruby-forum gateway to send each message
twice?
Joost D. (Guest)
on 2006-05-12 08:30
(Received via mailing list)
On Fri, May 12, 2006 at 11:27:29AM +0900, 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.
>

Well, having to explicitly convert between strings and integers in most
cases is a concious design decision. I do still forget about it
sometimes
but luckily, the exceptions make the error easy to find.


Look at it another way:

In perl, you can do this:

"200" + 1  -> 201	(+ = addition operator, forces numerical context)
"200" . 1  -> 2001	(. = string append operator, forces string context)

but in ruby you only have the + operator, so in principle the result of

"200" + 1

and

200 + "1"

could be either 201 or 2001. And I'd sort of expect both cases to give
the same result. In any case, this stuff can lead to really confusing
results.

Personally, I think the way Perl handles this sort of thing is easier
for straight forward text manipulation, BUT since Perl depends on the
operator to determine the type of conversion the number of operators
is fairly large, and it becomes seriously unwieldy if you introduce
more types than simple strings and numbers. See for example the perl
table of operators for perl 6 (still under development):
http://www.ozonehouse.com/mark/blog/code/PeriodicTable.html

Joost.
Tim U. (Guest)
on 2006-05-12 08:42
(Received via mailing list)
> But seriously, if you feel that Ruby doesn't work for you, check out
> Python (http://www.python.org/) or maybe Perl (http://www.perl.org/)
> or even Java (http://java.sun.com/). There are a lot of programming
> languages out there, and it never hurts to give some of them a try.


Noooo. If you are a new programmer learn lisp and smalltalk first. If
somehow you decide that neither one of them are to your liking then
learn python.

You can safely skip perl and java :)
Elliot T. (Guest)
on 2006-05-12 08:48
(Received via mailing list)
On May 11, 2006, at 9:39 PM, Tim U. wrote:

> You can safely skip perl and java :)
yeah. somehow learning C or Java or maybe *most* languages can cause
"fear of parentheses". learn lisp *before* that happens!

don't be afraid:

((())))((()(())))))(()()()()()((((((((((((()))))))))))(((((((()()()()
()()(()()))))))))))()()()(((((((()()))))))(((()))()))))

that wasn't so bad, was it? :)

-- Elliot T.
http://www.curi.us/blog/
Jake McArthur (Guest)
on 2006-05-12 10:56
(Received via mailing list)
What's probably going here is that you just aren't aware of the
general suckiness of most programming languages. I have yet to see
one that I think is perfect for me or completely intuitive for
anybody; Ruby is my favorite though. Really, try doing this stuff in
C, Java, Perl, Python, Lisp, BASIC, PHP, whatever.... Some are easier
to pick up on than others, but they are all extremely difficult to
start with for a complete beginner to programming in general. Just
keep at it (with almost any popular language) and you will get there.
For the record, I think that, despite your difficulties, Ruby is
probably one of the easiest languages for you and most beginners to
start programming with.

- Jake McArthur
Eric H. (Guest)
on 2006-05-12 11:00
(Received via mailing list)
On May 11, 2006, at 8:16 PM, corey konrad wrote:

> right?
> So isnt it important that a language with that as its goal can express
> simple programs in a simple and intuitive way?

Intuitive to matz, yes.

> I am a begining programmer and i find ruby counter intuitive so far
> and that worries me and makes me wonder if i am wasting my time
> with. I want to learn how to write software. Thats the bigger
> picture i am in right now. Its not personal,  I am just trying to
> get my feet wet here and make sense of all this stuff.

You should think more about what you want and then go looking for the
appropriate documentation.  You should also experiment with irb.

Try:

ri Kernel#print
ri Kernel#puts
ri Kernel#gets
ri String#to_i

--
Eric H. - removed_email_address@domain.invalid - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
Eric H. (Guest)
on 2006-05-12 11:03
(Received via mailing list)
On May 11, 2006, at 9:10 PM, Mike H. wrote:

> Is there a way we can get the ruby-forum gateway to send each
> message twice?

Are you smoking crack?  The last thing I need is two copies of the
ruby-forum in my mailbox.

--
Eric H. - removed_email_address@domain.invalid - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
Charlie B. (Guest)
on 2006-05-12 18:26
(Received via mailing list)
On Fri, 2006-05-12 at 13:39 +0900, Tim U. wrote:

>
> Noooo. If you are a new programmer learn lisp and smalltalk first. If
> somehow you decide that neither one of them are to your liking then
> learn python.
>
> You can safely skip perl and java :)
>

That's a very broad statement.  I truly enjoy perl.  It's second only to
ruby in my book.

Charlie B.
http://www.recentrambles.com
Daniel S. (Guest)
on 2006-05-12 19:00
(Received via mailing list)
corey konrad wrote:
> 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

   "foo " + 4           => "foo 4"
   "foo " + 4 + " bar"  => "foo 4 bar"

In reality, all you need to do is this:

   "this is a better number #{i_num}"

When you use the `#{...}' syntax inside a double-quoted (!) string,
#to_s is called on the expression -- and most objects respond to that.


Cheers,
Daniel
Phil H. (Guest)
on 2006-05-12 23:05
(Received via mailing list)
Elliot T. <removed_email_address@domain.invalid> writes:

> don't be afraid:
>
> ((())))((()(())))))(()()()()()((((((((((((()))))))))))(((((((()()()()
> ()()(()()))))))))))()()()(((((((()()))))))(((()))()))))
>
> that wasn't so bad, was it? :)

Not so bad? There are mismatched parentheses in there! That's just not
OK. The pain!

-Phil H.
Logan C. (Guest)
on 2006-05-12 23:23
(Received via mailing list)
On May 12, 2006, at 3:04 PM, Phil H. wrote:

> OK. The pain!
>
> -Phil H.
>

No it only looks that way. He actually wrote a reader macro that
transforms that into valid code, it's hard to see because it's name
is "".
Corey K. (Guest)
on 2006-05-13 02:45
Yeah that is the kind of example i was looking for thanks for that kick
in the ass. Thats what i need to get me excited about ruby. Java
definatly looks more complicated.



> I agree completely. Java is much better for the my favorite number
> program:
>
> % cat favorite_number.java
> import java.io.*;
>
> class FavoriteNumber {
>    public static void main(String[] args) throws Exception {
>      BufferedReader in = new BufferedReader(new InputStreamReader
> (System.in));
>      String num;
>      System.out.print("What's your favorite number? ");
>      num = in.readLine( );
>      String better_num = (new Integer(Integer.parseInt(num) +
> 1)).toString();
>
>      System.out.println(better_num + " is better.");
>    }
> }
>
> See?
>
> And it's so intuitive!
Corey K. (Guest)
on 2006-05-13 02:46
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
>
>    "foo " + 4           => "foo 4"
>    "foo " + 4 + " bar"  => "foo 4 bar"
>
> In reality, all you need to do is this:
>
>    "this is a better number #{i_num}"
>
> When you use the `#{...}' syntax inside a double-quoted (!) string,
> #to_s is called on the expression -- and most objects respond to that.
>
>
> Cheers,
> Daniel
Bira (Guest)
on 2006-05-13 02:58
(Received via mailing list)
On 5/12/06, corey konrad <removed_email_address@domain.invalid> 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?

I tried the following in IRB:

string = "fnord"
puts string.length

It printed out "5" just fine. Is that the problem you were referring to?

(I'm using Ruby 1.8.4 on Linux).
Corey K. (Guest)
on 2006-05-13 03:05
> 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
	from :0
Elliot T. (Guest)
on 2006-05-13 03:14
(Received via mailing list)
you're trying to add a string and an integer. it's like writing:

3 + " dogs"

you should write

3.to_s + " dogs"

On May 12, 2006, at 4:05 PM, corey konrad wrote:

> irb(main):030:0> def name_length
> "what is your first name?"
> --
> Posted via http://www.ruby-forum.com/.
>
>

-- Elliot T.
http://www.curi.us/blog/
Bira (Guest)
on 2006-05-13 03:14
(Received via mailing list)
On 5/12/06, corey konrad <removed_email_address@domain.invalid> wrote:
>
> > 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.

Try this:

p "Your full name has #{full_length} characters."

The #{} evaluates everything inside it and then turns the result into
a string. It's meant just for this sort of thing.
Logan C. (Guest)
on 2006-05-13 03:17
(Received via mailing list)
On May 12, 2006, at 7:05 PM, corey konrad wrote:

> irb(main):030:0> def name_length
> "what is your first name?"
> --
> Posted via http://www.ruby-forum.com/.
>


Ok, puts <anything> works for anything.

str + <anything> does NOT work for anything.

str.length -> gives you a number

a number + number -> gives you a number

str + number -> gives you an error, before puts even get's around to
seeing anything

I would suggest almost _always_ using interpolation. It will give you
what you expect, pretty much everytime. To rewrite what you have
above with string interpolation:
p "your full name has: #{name_length} characters"
Corey K. (Guest)
on 2006-05-13 03:18
oh ok thanks i guess i should read this entire book on the laguage first
before getting snooty and thinking i have everything all figured out,
lol. I tend to do things like that all the time.

Thanks.





Bira wrote:
> On 5/12/06, corey konrad <removed_email_address@domain.invalid> wrote:
>>
>> > 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.
>
> Try this:
>
> p "Your full name has #{full_length} characters."
>
> The #{} evaluates everything inside it and then turns the result into
> a string. It's meant just for this sort of thing.
This topic is locked and can not be replied to.