# \[Helppp xD\] Newbie issue

**URL:** <https://www.ruby-forum.com/t/helppp-xd-newbie-issue/76308>\
**Category:** Ruby\
**Created:** [November 17, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/helppp-xd-newbie-issue/76308 "2006-11-17T14:56:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 17, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/helppp-xd-newbie-issue/76308/1 "2006-11-17T14:56:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 17, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/helppp-xd-newbie-issue/76308/2 "2006-11-17T15:26:14Z")

</div>

Flaab M. wrote:

> ```
> @variantes = 0
> 
> ```
> 
> 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

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

So, either use

```
if(self[@cont] == ?2 || self[@cont] == ?X)

```

or  
if(self[@cont…@cont] == “2” || self[@cont…@cont] == “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 V.

---

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 17, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/helppp-xd-newbie-issue/76308/3 "2006-11-17T15:32:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 17, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/helppp-xd-newbie-issue/76308/4 "2006-11-17T16:00:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 17, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/helppp-xd-newbie-issue/76308/5 "2006-11-17T19:11:26Z")

</div>

Matthew M. 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

---

<div class="post-metadata">

**Author:** ![flaab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/dbc845/32.png) [@flaab](https://www.ruby-forum.com/u/flaab)\
**Post date:** [November 17, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/helppp-xd-newbie-issue/76308/6 "2006-11-17T19:31:06Z")

</div>

Hi –

On Fri, 17 Nov 2006, David V. wrote:

> 1. 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
