[Helppp xD] Newbie issue

F

Flaab Mrlinux

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
 
D

David Vallner

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

Flaab said:
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--
 
D

Dmitry Buzdin

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
 
M

Matthew Moss

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
 
F

Flaab Mrlinux

Matthew said:
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
 
D

dblack

Hi --

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 | (e-mail address removed)
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
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top