confused as to the behavior of this ruby fragment - ruby

Sorry to ask this but I really need to get this done. I'd like to be able to pass in a string and strip out the stop_words. I have the following:
class Query
def self.normalize term
stop_words=["a","big","array"]
term.downcase!
legit=[]
if !stop_words.include?(term)
legit << term
end
return legit
end
def self.check_parts term
term_parts=term.split(' ')
tmp_part=[]
term_parts.each do |part|
t=self.normalize part
tmp_part << t
end
return tmp_part
end
end
I would think that this would return only terms that are not in the stop_words list but I'm getting back either an empty array or an array of the terms passed in. Like this:
ruby-1.9.2-p290 :146 > Query.check_parts "here Is my Char"
=> [[], [], [], ["char"]]
ruby-1.9.2-p290 :147 >
What am I doing wrong?
thx in advance

If you just want to filter out the terms and get an array of downcased words, it is simple.
module Query
StopWords = %w[a big array]
def self.check_parts string; string.downcase.split(/\s+/) - StopWords end
end
Query.check_parts("here Is my Char") # => ["here", "is", "my", "char"]

Why do you want the result as an array I don't know but
term_parts=term.split(' ')
term_parts.reject { |part| stop_words.include?(part) }
You could write what you expect.
By the way, you have an array for array because
def self.check_parts term
term_parts=term.split(' ')
tmp_part=[] # creates an array
term_parts.each do |part|
t=self.normalize part # normalize returns an empty array
# or one of only one element (a term).
tmp_part << t # you add an array into the array
end
return tmp_part
end

Related

Calling specific element from array not returning (Ruby)

I can't tell what's wrong with my code:
def morse_code(str)
string = []
string.push(str.split(' '))
puts string
puts string[2]
end
What I'm expecting is if I use "what is the dog" for str, I would get the following results:
=> ["what", "is", "the", "dog"]
=> "the"
But what I get instead is nil. If I do string[0], it just gives me the entire string again. Does the .split function not break them up into different elements? If anyone could help, that would be great. Thank you for taking the time to read this.
Your code should be :
def morse_code(str)
string = []
string.push(*str.split(' '))
puts string
p string[2]
end
morse_code("what is the dog" )
# >> what
# >> is
# >> the
# >> dog
# >> "the"
str.split(' ') is giving ["what", "is", "the", "dog"], and you are pushing this array object to the array string. Thus string became [["what", "is", "the", "dog"]]. Thus string is an array of size 1. Thus if you want to access any index like 1, 2 so on.., you will get nil. You can debug it using p(it calls #inspect on the array), BUT NOT puts.
def morse_code(str)
string = []
string.push(str.split(' '))
p string
end
morse_code("what is the dog" )
# >> [["what", "is", "the", "dog"]]
With Array, puts works completely different way than p. I am not good to read MRI code always, thus I take a look at sometime Rubinious code. Look how they defined IO::puts, which is same as MRI. Now look the specs for the code
it "flattens a nested array before writing it" do
#io.should_receive(:write).with("1")
#io.should_receive(:write).with("2")
#io.should_receive(:write).with("3")
#io.should_receive(:write).with("\n").exactly(3).times
#io.puts([1, 2, [3]]).should == nil
end
it "writes nothing for an empty array" do
x = []
#io.should_receive(:write).exactly(0).times
#io.puts(x).should == nil
end
it "writes [...] for a recursive array arg" do
x = []
x << 2 << x
#io.should_receive(:write).with("2")
#io.should_receive(:write).with("[...]")
#io.should_receive(:write).with("\n").exactly(2).times
#io.puts(x).should == nil
end
We can now be sure that, IO::puts or Kernel::puts behaves with array just the way, as Rubinious people implemented it. You can now take a look at the MRI code also. I just found the MRI one, look the below test
def test_puts_recursive_array
a = ["foo"]
a << a
pipe(proc do |w|
w.puts a
w.close
end, proc do |r|
assert_equal("foo\n[...]\n", r.read)
end)
end

turn array of strings into a sentence with ruby

So i need to turn an array of strings into a sentence, capitalize the first word and add a period at the end. I have looked everywhere and found bits and pieces but nothing as specific as my problem.
What i tried so far:
array1 = ["this", "is", "my", "first", "post"]
def sentence_maker (array)
array.join(' ')
end
It makes a sentence but i can't figure out how to make the first word capitalized while keeping the others in lower case and add a "." at the end of the sentence. Any help would be appreciated.
You could do as below :
array1 = ["this", "is", "my", "first", "post"]
def sentence_maker (array)
array.join(' ').capitalize << "."
end
sentence_maker(array1)
# => "This is my first post."
How I would do it:
array1 = ["this", "is", "my", "first", "post"]
def sentence_maker(array)
string = array.join(' ')
string.capitalize!
string << '.'
end
puts sentence_maker(array1)
#=> "This is my first post."
See: http://www.ruby-doc.org/core-1.9.3/String.html
The simple way to do this would be to use the capitalize method, but note that “case conversion is effective only in ASCII region”:
"école".capitalize
# => "école"
If this is likely to be an issue you should look into using something like the Unicode Utils gem:
require 'unicode_utils'
UnicodeUtils.titlecase("école")
# => "École"
So your complete method might look something like:
def sentence_maker (array)
array[0] = UnicodeUtils.titlecase(array[0])
array.join(' ') << '.'
end
(This is a bit different from the other answers because titlecase changes the first letter of each word in the string, which we don’t want in this case. Also note this modifies array which you might not want, so you”d have to structure the code differently if that were the case.)

Return array of hashes based on hash element match

I am wondering how one would search through an array of hashes and return a value based on a search string. For example, #contacts contains the hash elements: :full_name, :city, and :email. The variable #contacts (I guess it would be an array) contains three entries (perhaps rows). Below is the code I have so far to conduct a search based on :city value. However it's not working. Can anyone give me an idea what's going on?
def search string
#contacts.map {|hash| hash[:city] == string}
end
You should use select instead of map:
def search string
#contacts.select { |hash| hash[:city] == string }
end
In your code you tried to map (or transform) your array using a block, which yields boolean values. map takes a block and invokes the block for each element of self, constructing a new array containing elements returned by the block. As the result, you got an array of booleans.
select works similar. It takes a block and iterates over the array as well, but instead of transforming the source array it returns an array containing elements for which the block returns true. So it's a selection (or filtering) method.
In order to understand the difference between these two methods it's useful to see their example definitions:
class Array
def my_map
[].tap do |result|
self.each do |item|
result << (yield item)
end
end
end
def my_select
[].tap do |result|
self.each do |item|
result << item if yield item
end
end
end
end
Example usage:
irb(main):007:0> [1,2,3].my_map { |x| x + 1 }
[2, 3, 4]
irb(main):008:0> [1,2,3].my_select { |x| x % 2 == 1 }
[1, 3]
irb(main):009:0>
You can try this:
def search string
#contacts.select{|hash| h[:city].eql?(string) }
end
This will return an array of hashes which matches string.

array modified in the loop causes bug with the output ruby

I have a code that places anagrams into an array of arrays. (which contain anagrams)
but somewhere i made a bug and the first values do not output as arrays but just as strings
I am using the << operator to push one array into the other
the code is not that complicated but i cannot find a bug
def combine_anagrams(words)
indexes = []
anagrams = []
words.each{|word|
if(word.is_a? String )
first_word = word.downcase.chars.sort.join
words.each{|second_word|
if(second_word.is_a? String)
if(first_word == second_word.downcase.chars.sort.join)
indexes << words.index(second_word)
end
end
}
indexes.each{|index| anagrams << words[index] }
words.reject!.with_index {|el, idx| indexes.include?(idx)}
words << anagrams # i replaced words with an array all_anagrams
indexes = []
anagrams = []
end
}
return words
end
puts combine_anagrams([ 'cars','for', 'potatoes', 'racs', 'four','scar', 'creams', 'scream'] ).inspect
outputs
["for", "four", ["cars", "racs", "scar"], ["potatoes"], ["creams", "scream"]]
if i switch the order of "cars" and "for" in the input i get
["cars", "racs", "scar", ["for"], ["potatoes"], ["four"], ["creams", "scream"]]
Whats going on here
Sorry for the messy code im just begging to learn ruby
I created an additional variable all_anagrams = [] to store the array of all anagrams
when i output the array onto the sreen i get all the values except the "for" and "four" for some reason those never get send to all_anagrams
probably because i shorten the array when i am in the loop and those values get skipped over?
However i dont know how to deal with this problem.
the output of all_anagrams is
[["cars", "racs", "scar"], ["potatoes"], ["creams", "scream"]]
What you need is introduce a new array to store anagrams before you blank it, lets call it valid_anagrams. Right now you're pushing that in words. And as Fredrick pointed out you're modifying words while iterating over it. Its not good and to avoid that you keep a clone of words called words_clone and reject items from it instead. Following code should work -
def combine_anagrams(words)
indexes, anagrams, valid_anagrams = [], [], []
words_clone = words.clone # creating a clone of words
words.each do |word|
if(word.is_a? String )
first_word = word.downcase.chars.sort.join
words.each do |second_word|
if(second_word.is_a? String)
if(first_word == second_word.downcase.chars.sort.join)
indexes << words.index(second_word)
end
end
end
indexes.each{|index| anagrams << words[index] }
# reject from words_cloned instead of words
words_clone.reject!.with_index {|el, idx| indexes.include?(idx)}
# insert anagrams into valid_anagrams array. In your code you inserted it in words array
valid_anagrams << anagrams unless valid_anagrams.include?(anagrams)
indexes, anagrams = [], []
end
end
# return valid_anagrams array
return valid_anagrams
end

What purpose does counts serve? Line 7

I am wondering what purpose does the counts variable serve, the one right before the last end?
# Pick axe page 51, chapter 4
# Count frequency method
def count_frequency(word_list)
counts = Hash.new(0)
for word in word_list
counts[word] += 1
end
counts #what does this variable actually do?
end
puts count_frequency(["sparky", "the", "cat", "sat", "on", "the", "mat"])
The last expression in any Ruby method is the return value for that method. If counts were not at the end of the method, the return value would be the result of the for loop; in this case, that's the word_list array itself:
irb(main):001:0> def count(words)
irb(main):002:1> counts = Hash.new(0)
irb(main):003:1> for word in words
irb(main):004:2> counts[word] += 1
irb(main):005:2> end
irb(main):006:1> end
#=> nil
irb(main):007:0> count %w[ sparky the cat sat on the mat ]
#=> ["sparky", "the", "cat", "sat", "on", "the", "mat"]
Another way someone might write the same method in 1.9:
def count_frequency(word_list)
Hash.new(0).tap do |counts|
word_list.each{ |word| counts[word]+=1 }
end
end
Though some people consider using tap like this to be an abuse. :)
And, for fun, here's a slightly-slower-but-purely-functional version:
def count_frequency(word_list)
Hash[ word_list.group_by(&:to_s).map{ |word,array| [word,array.length] } ]
end
Ruby doesn't require you to use the return statement to return a value in a method. The last line evaluated in the method will be returned if an explicit return statement is omitted.
It provides the return value for the function; it's how the result (which is stored in that variable) is transmitted back to the caller (i.e., the line of code at the end.) The last expression evaluated in a Ruby function is used as the return value.
Counts is a dictionary, i.e. it is an associative map of keys to values.
In this case, the words are the keys, the values are the number of occurrences.
The dictionary is returned from the function count_frequency

Resources