[Helppp xD] Newbie issue

Discussion in 'Ruby' started by Flaab Mrlinux, Nov 17, 2006.

  1. Hi all!!

    I've declared a method inside the string class that has to answer the
    amount of "X" and "2" chars in a string. The string is ALWAYS gonna be
    15 chars size. The code is the following:

    class String

    # DAME VARIANTES
    def variantes
    @variantes = 0
    15.times do |@cont|
    if (self[@cont] == "2" || self[@cont] == "X")
    @variantes = @variantes + 1
    end
    end

    return @variantes
    end
    end

    cadena = "XX222XX222XX1X2"
    puts cadena.variantes

    And the last line ALWAYS ANSWERS a CERO. Why? Why it doesn't enter the
    if statement inside the method?

    Thx


    --
    Posted via http://www.ruby-forum.com/.
    Flaab Mrlinux, Nov 17, 2006
    #1
    1. Advertising

  2. --------------enig135529D0A634041B75FA5E47
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: quoted-printable

    Flaab Mrlinux wrote:
    > Hi all!!
    >=20
    > I've declared a method inside the string class that has to answer the=20
    > amount of "X" and "2" chars in a string. The string is ALWAYS gonna be =


    > 15 chars size. The code is the following:
    >=20
    > class String
    >=20
    > # DAME VARIANTES
    > def variantes
    > @variantes =3D 0
    > 15.times do |@cont|
    > if (self[@cont] =3D=3D "2" || self[@cont] =3D=3D "X")
    > @variantes =3D @variantes + 1
    > end
    > end
    >=20
    > return @variantes
    > end
    > end
    >=20
    > cadena =3D "XX222XX222XX1X2"
    > puts cadena.variantes
    >=20
    > And the last line ALWAYS ANSWERS a CERO. Why? Why it doesn't enter the =


    > if statement inside the method?
    >=20
    > Thx
    >=20
    >=20


    String#[] returns the integer code of the character at that position.

    So, either use
    =09
    if(self[@cont] =3D=3D ?2 || self[@cont] =3D=3D ?X)

    or
    if(self[@cont..@cont] =3D=3D "2" || self[@cont..@cont] =3D=3D "X")

    Also:

    1. You shouldn't use instance variables when local ones will suffice.
    I.e. use "variantes" and "cont" instead of "@variantes" and "@cont"

    2. Please send code snippets with identifiers in English - it takes a
    little guesswork out of what you're trying to achieve.

    3. Smileys and netspeak are fluff and a little out of place for the
    medium. Cut out the "xD" and use "Thanks" instead of Thx, please.

    David Vallner


    --------------enig135529D0A634041B75FA5E47
    Content-Type: application/pgp-signature; name="signature.asc"
    Content-Description: OpenPGP digital signature
    Content-Disposition: attachment; filename="signature.asc"

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.5 (MingW32)

    iD8DBQFFXcX/y6MhrS8astoRAjVpAJwNfCSv422RY97S95l1bkBKRDZOsQCfZIYP
    bcaz45d1Dx0wi4OEdTVTRRQ=
    =XDuT
    -----END PGP SIGNATURE-----

    --------------enig135529D0A634041B75FA5E47--
    David Vallner, Nov 17, 2006
    #2
    1. Advertising

  3. Re: Newbie issue

    Check the manual

    str[0] will return a numeric representation of the character
    what You need is str[0,1] one character starting from the first
    position.

    Try this:

    class String
    def variantes
    @variantes = 0
    Range.new(0, 14).each { |i|
    if (self[i,1] == "2" || self[i,1] == "X")
    @variantes = @variantes + 1
    end
    }
    return @variantes
    end
    end

    str = "XXXXX22222XXXXX"
    puts str.variantes

    Dmitry
    Dmitry Buzdin, Nov 17, 2006
    #3
  4. Flaab Mrlinux

    Matthew Moss Guest

    Now that you got a couple answers why your comparisons didn't work,
    time to suggest a better method.

    class String
    def variantes
    count "2X"
    end
    end
    Matthew Moss, Nov 17, 2006
    #4
  5. Re: Newbie issue

    Matthew Moss wrote:
    > Now that you got a couple answers why your comparisons didn't work,
    > time to suggest a better method.
    >
    > class String
    > def variantes
    > count "2X"
    > end
    > end


    Wow THANKS i´m amazed.... =D That's pretty better!

    I'm newbie at ruby didn't know it had such a simple way to do this.

    Thanks a lot

    --
    Posted via http://www.ruby-forum.com/.
    Flaab Mrlinux, Nov 17, 2006
    #5
  6. Flaab Mrlinux

    Guest

    Hi --

    On Fri, 17 Nov 2006, David Vallner wrote:

    > 3. Smileys and netspeak are fluff and a little out of place for the
    > medium. Cut out the "xD" and use "Thanks" instead of Thx, please.


    Oh, I think we do OK despite the occasional smiley :) Let's err on
    the side of making people feel welcome unless they do something
    clearly troll-like.

    Thx --


    David

    --
    David A. Black |
    Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
    DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
    [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
    [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
    , Nov 17, 2006
    #6
    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. .NET Developer

    Newbie client side JavaScript debugging issue

    .NET Developer, Aug 2, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    342
    .NET Developer
    Aug 3, 2003
  2. aacool
    Replies:
    4
    Views:
    340
    aacool
    Feb 17, 2004
  3. roger beniot

    newbie navigation/form re-draw issue...

    roger beniot, Apr 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    355
    Chris Moore
    Apr 10, 2004
  4. My and I

    Beginer need helppp!!

    My and I, Aug 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    484
    clintonG
    Aug 24, 2004
  5. Srini
    Replies:
    11
    Views:
    954
    Arne Vajhøj
    Jun 1, 2008
Loading...

Share This Page