Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > fixnum problem plz help

Reply
Thread Tools

fixnum problem plz help

 
 
Guy Dols
Guest
Posts: n/a
 
      03-03-2009
hy

i am new to ruby i am trieng to get some skils but i have a problem when
i use a fixnum :

years = ask("")
days = years * 365
puts days


my answer is
22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222

but i should be 730

i have an screenshot as an attachment plz help me
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Michael Malone
Guest
Posts: n/a
 
      03-03-2009
Guy Dols wrote:
> hy
>
> i am new to ruby i am trieng to get some skils but i have a problem when
> i use a fixnum :
>
> years = ask("")
> days = years * 365
> puts days
>
>
> my answer is
> 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222
>
> but i should be 730
>
> i have an screenshot as an attachment plz help me
>

Please paste your ask() Method. That appears to be where the problem is.

================================================== =====================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
================================================== =====================


 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      03-03-2009
Guy Dols <(E-Mail Removed)> writes:

> hy
>
> i am new to ruby i am trieng to get some skils but i have a problem when
> i use a fixnum :
>
> years = ask("")
> days = years * 365
> puts days
>
>
> my answer is
> 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222
>
> but i should be 730



I think you are lying. Twice.

First, the answer is probably not what you're telling but rather:

22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 222222222222222

Then it should NOT be 730, but what you get.

This is because for all we know, ask("") is returning "2" and 365
times "2" is a string containing 365 2s. At least in such a inferior
language as Ruby.



In more strongly typed programming languages such as Common Lisp,
you'd get an error:

C/USER[929]> (* "2" 365)

*** - *: "2" is not a number



> i have an screenshot as an attachment plz help me


Good for you!


--
__Pascal Bourguignon__
 
Reply With Quote
 
Michael Malone
Guest
Posts: n/a
 
      03-03-2009
Pascal J. Bourguignon wrote:
> Guy Dols <(E-Mail Removed)> writes:
>
>
>> hy
>>
>> i am new to ruby i am trieng to get some skils but i have a problem when
>> i use a fixnum :
>>
>> years = ask("")
>> days = years * 365
>> puts days
>>
>>
>> my answer is
>> 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222
>>
>> but i should be 730
>>

>
>
> I think you are lying. Twice.
>
> First, the answer is probably not what you're telling but rather:
>
> 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 222222222222222
>
> Then it should NOT be 730, but what you get.
>
> This is because for all we know, ask("") is returning "2" and 365
> times "2" is a string containing 365 2s. At least in such a inferior
> language as Ruby.
>
>

Unsuccessful troll is unsuccessful.
>
> In more strongly typed programming languages such as Common Lisp,
> you'd get an error:
>
> C/USER[929]> (* "2" 365)
>
> *** - *: "2" is not a number
>
>
>
>
>> i have an screenshot as an attachment plz help me
>>

>
> Good for you!
>
>
>



================================================== =====================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
================================================== =====================


 
Reply With Quote
 
Ken Bloom
Guest
Posts: n/a
 
      03-03-2009
On Tue, 03 Mar 2009 16:28:48 -0500, Guy Dols wrote:

> hy
>
> i am new to ruby i am trieng to get some skils but i have a problem when
> i use a fixnum :
>
> years = ask("")
> days = years * 365
> puts days
>
>
> my answer is
>

22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222
>
> but i should be 730
>
> i have an screenshot as an attachment plz help me


Ask is returning the string "2" instead of the number 2.
When you use the multiplication operator on a string, it creates a longer
string with that many copies of the string concatenated back to back, so
instead of multiplying the number 2 by 365 to give 730, you got a string
containing exactly 365 twos.

years=ask("").to_i

--Ken

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
 
Reply With Quote
 
Tom Cloyd
Guest
Posts: n/a
 
      03-04-2009
Pascal J. Bourguignon wrote:
> Guy Dols <(E-Mail Removed)> writes:
>
>
>> hy
>>
>> i am new to ruby i am trieng to get some skils but i have a problem when
>> i use a fixnum :
>>
>> years = ask("")
>> days = years * 365
>> puts days
>>
>>
>> my answer is
>> 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222
>>
>> but i should be 730
>>

>
>
> I think you are lying. Twice.
>
> First, the answer is probably not what you're telling but rather:
>
> 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 22222222222222222222222222222222222222222222222222 222222222222222
>
> Then it should NOT be 730, but what you get.
>
> This is because for all we know, ask("") is returning "2" and 365
> times "2" is a string containing 365 2s. At least in such a inferior
> language as Ruby.
>
>
>
> In more strongly typed programming languages such as Common Lisp,
> you'd get an error:
>
> C/USER[929]> (* "2" 365)
>
> *** - *: "2" is not a number
>
>
>
>
>> i have an screenshot as an attachment plz help me
>>

>
> Good for you!
>
>
>

And for the benefits, such as they are, of strong typing, we get to a
whole other level of detail to attend to. No thanks. Did that for a time
with Pascal. I prefer the mindless grace of waterfowl.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< http://www.velocityreviews.com/forums/(E-Mail Removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~


 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      03-04-2009
Pascal J. Bourguignon wrote:

> This is because for all we know, ask("") is returning "2" and 365
> times "2" is a string containing 365 2s. At least in such a inferior
> language as Ruby.
>
>
>
> In more strongly typed programming languages such as Common Lisp,


Typo. Should be Commune Lisp.

> you'd get an error:
>
> C/USER[929]> (* "2" 365)
>
> *** - *: "2" is not a number


Commune Lisp:
(* "2" 9)
*** ERROR!!! ***

Awk:
"2" * 9
18

Ruby:
"2" * 9
"222222222"

Now, which is the lowest-level language?
 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      03-04-2009
Pascal J. Bourguignon wrote:
> In more strongly typed programming languages such as Common Lisp,
> you'd get an error:
>
> C/USER[929]> (* "2" 365)
>
> *** - *: "2" is not a number


This is nothing to do with "strongly typed". Ruby's concept of the
difference between a string and an integer is just as strict as LISP's.

Rather, Ruby provides semantics to the '*' operator/function for strings
which LISP doesn't.

You can get exactly the behaviour you desire by _removing_ some Ruby
functionality.

irb(main):001:0> String.class_eval { undef_method(:*) }
=> String
irb(main):002:0> "2" * 365
NoMethodError: undefined method `*' for "2":String
from (irb):2
from :0
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Thomas Preymesser
Guest
Posts: n/a
 
      03-04-2009
2009/3/4 William James <(E-Mail Removed)>:
> Pascal J. Bourguignon wrote:
>
>> This is because for all we know, ask("") is returning "2" and 365
>> times "2" is a string containing 365 2s. =A0At least in such a inferior
>> language as Ruby.
>>
>>
>>
>> In more strongly typed programming languages such as Common Lisp,

>
> Typo. =A0Should be Commune Lisp.
>
>> you'd get an error:
>>
>> C/USER[929]> (* "2" 365)
>>
>> *** - *: "2" is not a number

>
> Commune Lisp:
> =A0(* "2" 9)
> =A0*** ERROR!!! ***
>
> Awk:
> =A0"2" * 9
> =A018
>
> Ruby:
> =A0"2" * 9
> =A0"222222222"


C:
printf("%d\n",'2'*9);
450


--=20
Thomas Preymesser
(E-Mail Removed)
http://thopre.googlepages.com/
http://thopre.wordpress.com/
Sent from: Berlin Berlin Deutschland.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Float Arithmetic: Return A Fixnum When Float == Fixnum MaggotChild Ruby 6 12-02-2009 04:28 AM
Why Fixnum===Fixnum is false? Heesob Park Ruby 5 05-14-2009 12:31 AM
plz help me in C/C++ Logic plz mrkanuganti@gmail.com C++ 1 11-24-2006 10:23 AM
Re: plz help!!! plz plz plz plzplzplz help the noob alkzy Microsoft Certification 0 10-31-2004 10:04 PM
Computer Problems... Plz Plz Plz HELP ME..... Nick Computer Support 0 06-04-2004 08:50 PM



Advertisments