what i'd like to do is let my each method yield aal instance variables include enumerable to have the ability to test if self.include? yields true or not..
My instance_variables however are mostly string objects with one Array. the array holds one or more strings
I cannot get .include? to work propperly. it wil return false even though the array members hold the substring.
anyone?
say each is
def each
yield "#{var1}"
yield "#{var2}"
end
where var2 is an Array
def search str
self.inlude? str
end
the search method always returns false even if var2 holds the str.
str is not always the whole string member of the array could be just a part of it. say one word from a multi word string..
Firstly, post a complete small program which demonstrates your problem -
something we can actually run and see exactly the same as you do. And
copy-paste it exactly (or post a URL to pastebin or similar)
And secondly: include? will test each member you yield individually. If
the member you yield is an array, then it will only match another
identical array.
Aha! There's your problem. By default, the array is just going to
get checked to see if it, itself, equals the string you're looking
for. You have to override #search to also look inside the array.
-Dave
···
On Tue, Feb 21, 2012 at 12:52, Catsquotl <Catsquotl@gmail.com> wrote:
My instance_variables however are mostly string objects with one Array. the
array holds one or more strings
def initialize(arr)
set_instance_variables arr
end
def set_instance_variables arr
@afk=arr[0]
@name=arr[1]
@chname=arr[2]
@locatie=arr[3]
@aard=arr[4]
@actie=arr[5..-1]
end
def each
yield "#{@afk}"
yield "#{@name}"
yield "#{@chname}"
yield "#{@locatie}"
yield "#{@aard}"
yield "#{@actie}"
end
def search str
self.include? str
end
end
ac = Acupunt.new(["bl", "witte vloed", "tingwell", "6cun vanaf", "Luo","klaart hitte","2de","en een 3de"])
ac.search "bl" #This yields true
ac.search "hitte" #this yields false, but I want it to be true
Op 21-2-2012 19:24, Brian Candler schreef:
···
Firstly, post a complete small program which demonstrates your problem -
something we can actually run and see exactly the same as you do. And
copy-paste it exactly (or post a URL to pastebin or similar)
And secondly: include? will test each member you yield individually. If
the member you yield is an array, then it will only match another
identical array.
Now please show your code which does not do anything.
Can anyone say what's going on?
I've read up on blocks, procs and lambda's for the last few hours, but I
can't seem to get to grips with the logic.
The block is simply forwarded to the second call of #each. Before
that #flatten just ensures that nested Arrays are expanded and every
element of them is visited individually.
def each(&block)
[@afk, @name, @chname, @locatie, @aard, @actie].flatten.each(&block)
end
Btw. that method should conventionally return "self" at the end.
Kind regards
robert
···
On Thu, Feb 23, 2012 at 9:58 AM, Catsquotl <Catsquotl@gmail.com> wrote:
After reading your post i took another look at what i was trying to do.
As i am just playing with different ide's, different irb setups and Rspec testing. I missed what exactly was happening.
I had hoped to be able to use include? to return true even on a substring. It does not do so in Arrays.
Also i wanted my search method to return either true or false, but with the each(&block) coded as it is
the search method will return the entire array defined in each
rendering my result.should == true or false useless...
Anyway I settled on the following.
def each(&block)
[@afk, @name, @chname, @locatie, @aard, @actie].flatten.each(&block)
end
def search str
self.each do|n|
if n.match(str) then
return true
end
end
end
I do have the notion that search could be changed to return true or false, but this way i can test whether it is true or not_true.
And the class does what it needs to do...
Eelco
···
Btw. that method should conventionally return "self" at the end.
After reading your post i took another look at what i was trying to do.
As i am just playing with different ide's, different irb setups and Rspec
testing. I missed what exactly was happening.
Please post a http://sscce.org/ otherwise people have a hard time
providing help. We can't read minds.
I had hoped to be able to use include? to return true even on a substring.
It does not do so in Arrays.
Also i wanted my search method to return either true or false, but with the
each(&block) coded as it is
the search method will return the entire array defined in each
rendering my result.should == true or false useless...
Anyway I settled on the following.
def each(&block)
[@afk, @name, @chname, @locatie, @aard, @actie].flatten.each(&block)
end
There is still self missing at the end of this method.
def search str
self.each do|n|
if n.match(str) then
return true
end
end
end
There is Enumerable#any?
def search str
any? {|n| n.match(str)}
end
I do have the notion that search could be changed to return true or false,
but this way i can test whether it is true or not_true.
And the class does what it needs to do...
Do not rely on "true" and "false" used as boolean values. Rather
directly evaluate the value in a boolean context.
Cheers
robert
···
On Fri, Feb 24, 2012 at 1:41 PM, Catsquotl <Catsquotl@gmail.com> wrote: