.gcd method in ruby 1.8.7

Discussion in 'Ruby' started by hazal Ates, Sep 8, 2010.

  1. hazal Ates

    hazal Ates Guest

    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"
    --
    Posted via http://www.ruby-forum.com/.
     
    hazal Ates, Sep 8, 2010
    #1
    1. Advertising

  2. hazal Ates

    hazal Ates Guest

    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"


    note : its not work in irb , too.
    --
    Posted via http://www.ruby-forum.com/.
     
    hazal Ates, Sep 8, 2010
    #2
    1. Advertising

  3. 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
     
    Stefano Crocco, Sep 8, 2010
    #3
  4. hazal Ates

    hazal Ates Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
     
    hazal Ates, Sep 8, 2010
    #4
  5. On Wednesday 08 September 2010, hazal Ates wrote:
    > |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


    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
     
    Stefano Crocco, Sep 8, 2010
    #5
  6. hazal Ates

    hazal Ates Guest

    > 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)
    --
    Posted via http://www.ruby-forum.com/.
     
    hazal Ates, Sep 9, 2010
    #6
  7. hazal Ates

    hazal Ates Guest

    > 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)



    ok i just solve it by writing like :

    Rational (26),(65) -------> 2/5
    --
    Posted via http://www.ruby-forum.com/.
     
    hazal Ates, Sep 9, 2010
    #7
  8. On Thursday 09 September 2010, hazal Ates wrote:
    > |> 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
     
    Stefano Crocco, Sep 9, 2010
    #8
  9. hazal Ates

    Quintus Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Am 09.09.2010 14:43, schrieb Stefano Crocco:
    > On Thursday 09 September 2010, hazal Ates wrote:
    >> |> 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
    >
    >


    marvin@ikarus:~$ irb
    irb(main):001:0> RUBY_DESCRIPTION
    => "ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]"
    irb(main):002:0> Rational (6 , 10)
    SyntaxError: (irb):2: syntax error, unexpected ',', expecting ')'
    Rational (6 , 10)
    ^
    from /opt/rubies/ruby-1.9.2-p0/bin/irb:12:in `<main>'
    irb(main):003:0> exit
    marvin@ikarus:~$ irb18 -rrational
    irb(main):001:0> RUBY_DESCRIPTION
    => "ruby 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux]"
    irb(main):002:0> Rational (6, 10)
    (irb):2: warning: don't put space before argument parentheses
    => Rational(3, 5)
    irb(main):003:0> exit
    marvin@ikarus:~$

    Vale,
    Marvin
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.10 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

    iEYEARECAAYFAkyI3EQACgkQDYShvwAbcNluVQCeJPljE4VOJkODCPHu/p7iQkOZ
    cwwAn3YG2Zuht7dUkLV8XRS71fqgiAST
    =Bnh3
    -----END PGP SIGNATURE-----
     
    Quintus, Sep 9, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. adrin

    GCD of polynomials

    adrin, Jan 5, 2005, in forum: C++
    Replies:
    7
    Views:
    3,099
    Buster
    Jan 6, 2005
  2. Replies:
    3
    Views:
    492
    David Hilsee
    May 15, 2005
  3. lovecreatesbeauty

    How to write a small graceful gcd function?

    lovecreatesbeauty, Jul 15, 2006, in forum: C Programming
    Replies:
    73
    Views:
    1,592
    ozbear
    Jul 26, 2006
  4. sathyashrayan

    simple GCD, help

    sathyashrayan, Nov 18, 2006, in forum: C Programming
    Replies:
    7
    Views:
    351
    Spiros Bousbouras
    Nov 18, 2006
  5. gamo

    A subroutine for gcd

    gamo, Jul 17, 2006, in forum: Perl Misc
    Replies:
    6
    Views:
    148
    Michele Dondi
    Jul 21, 2006
Loading...

Share This Page