< :the previous in number
^ :the list in numerical order
> :the next in number
P :the previous (in thread)
N :the next (in thread)
|<:the top of this thread
>|:the next thread
^ :the parent (reply-to)
_:the child (an article replying to this)
>:the elder article having the same parent
<:the youger article having the same parent
---:split window and show thread lists
| :split window (vertically) and show thread lists
~ :close the thread frame
.:the index
..:the index of indices
Nick Bo wrote:
> irb
> irb(main):001:0> string ="cat cat fox"
> => "cat cat fox"
> irb(main):002:0> stringArray = string.split(" ")
> => ["cat", "cat", "fox"]
> irb(main):003:0> string.count(stringArray[0])
> => 6
>
> obviously what i want it to do is to count how many instances of cat
> there are in a string by using an array. Reason for this is i want to
> be able to push the value of the count into a new array and make a hash
> using (word, count) and then be able to print the word and count and
> then do a method which will print the word and some kind of delimeter
> that will express how many times cat showed up so...
>
> cat| ##
>
> or something like that but first thing is first why wont it let me count
> how many instances of cat were in the string using an array? and why
> does it tell me 6?
String#count counts characters, not strings. You're asking how many c's,
a's, and t's there are in "cat cat fox" and the answer is 6. 2 c's, 2
a's, and 2 t's.
----------------------------------------------------------- String#count
str.count([other_str]+) => fixnum
------------------------------------------------------------------------
Each other_str parameter defines a set of characters to count. The
intersection of these sets defines the characters to count in str.
Any other_str that starts with a caret (^) is negated. The
sequence c1--c2 means all characters between c1 and c2.
a = "hello world"
a.count "lo" #=> 5
a.count "lo", "o" #=> 2
a.count "hello", "^l" #=> 4
a.count "ej-m" #=> 4
--
RMagick: http://rmagick.rubyforge.org/