H
hazal Ates
in my code i use .gcd method to find greatest common divisor of two int.
like i= 12.gcd 44
puts i
but it says "not defined method"
like i= 12.gcd 44
puts i
but it says "not defined method"
hazal said:in my code i use .gcd method to find greatest common divisor of two int.
like i= 12.gcd 44
puts i
but it says "not defined method"
|in my code i use .gcd method to find greatest common divisor of two int.
|
|like i= 12.gcd 44
|puts i
|
|but it says "not defined method"
Stefano said:Without the "like" before i= (which I assume doesn't belong to your
program
since it produces a syntax error) your code works correctly for me with
ruby
1.8.7-p302.
Stefano
|Stefano Crocco wrote:
|> On Wednesday 08 September 2010, hazal Ates wrote:
|>> |in my code i use .gcd method to find greatest common divisor of two
|>> |int.
|>> |
|>> |like i= 12.gcd 44
|>> |puts i
|>> |
|>> |but it says "not defined method"
|>
|> Without the "like" before i= (which I assume doesn't belong to your
|> program
|> since it produces a syntax error) your code works correctly for me with
|> ruby
|> 1.8.7-p302.
|>
|> Stefano
|
|thank you, but of course i wrote the code without "like".
|irb output as below ;
|irb(main):001:0> 12.gcd 44
|NoMethodError: undefined method `gcd' for 12:Fixnum
| from (irb):1
| from :0
which is
part of the standard library. To make your program work you simply have
to
require it. The reason it worked for me is that this file is
automatically
required on my system when starting irb.
I hope this helps
Stefano
ratio.rb:1: syntax error, unexpected ',', expecting ')'ruby ratio.rb
Exit code: 1
when i wrote : Rational ( 26,65 ) it gives syntax error
ratio.rb:1: syntax error, unexpected ',', expecting ')'
Rational (6 , 10)
^
normally it must give : (2/5)
|> You're right. In ruby 1.8.7 the gcd method is defined in rational.rb,
|> which is
|> part of the standard library. To make your program work you simply have
|> to
|> require it. The reason it worked for me is that this file is
|> automatically
|> required on my system when starting irb.
|>
|> I hope this helps
|>
|> Stefano
|
|thanks but now another problem :
|
|when i wrote : Rational ( 26,65 ) it gives syntax error
|
|>ruby ratio.rb
|
|ratio.rb:1: syntax error, unexpected ',', expecting ')'
|Rational (6 , 10)
| ^
|
|>Exit code: 1
|
|normally it must give : (2/5)
This is strange. It works for me. An attempt I'd do is to remove the space
between Rational and the parentheses. Ruby behaviour when you put a space
before a parentheses can be unintuitive (if calling ruby with the -w switch,
it should give a warning about it).
Stefano
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.