Cheered to soon enumerable woos

hi,

what i’d like to do is let my each method yield aal instance variables
from my class
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…

Eelco

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.

class Acupunt
include Enumerable

attr_reader :afk, :name,:chname, :locatie, :aard, :actie

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 C. schreef:

On Tue, Feb 21, 2012 at 14:03, Catsquotl removed_email_address[email protected] wrote:

def search str
self.include? str

Maybe “self.include?(str) || actie.include?(str)”?

-Dave

I tried to rewrite your code to make it more readable for me.

My question is:

Where do you call .each ?

Also, if you use yield, you should combine it with “yield foo if
block_given?”

class Test

include Enumerable

attr_reader :afk
attr_reader :name
attr_reader :chname
attr_reader :locatie
attr_reader :aard
attr_reader :actie

def initialize(i)
set_instance_variables(i)
end

def set_instance_variables(i)
@afk = i[0]
@name = i[1]
@chname = i[2]
@locatie = i[3]
@aard = i[4]
@actie = i[5…-1]
end

def each
yield “#{@afk}”
yield “#{@name}”
yield “#{@chname}”
yield “#{@locatie}”
yield “#{@aard}”
yield “#{@actie}”
end

def search(i)
self.include? i
end

end

ac = Test.new( [‘bl’, ‘witte vloed’, ‘tingwell’, ‘6cun vanaf’,
‘Luo’,‘klaart hitte’,‘2de’,‘en een 3de’])
puts ac.search ‘bl’ # This yields true.
puts ac.search ‘hitte’ # This yields false, but I want it to be true.

Catsquotl wrote in post #1048098:

class Acupunt
include Enumerable

attr_reader :afk, :name,:chname, :locatie, :aard, :actie

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 C. schreef:

maybe just overwrite include method like this:

def include?(pattern)
each do |word|
retrun true if word =~ /pattern/
end
false
end

On Tue, Feb 21, 2012 at 12:52, Catsquotl [email protected] wrote:

My instance_variables however are mostly string objects with one Array. the
array holds one or more strings

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 Feb 21, 2012, at 11:03 , Catsquotl wrote:

def each
yield “#{@afk}”
yield “#{@name}”
yield “#{@chname}”
yield “#{@locatie}”
yield “#{@aard}”
yield “#{@actie}”
end

def each(&block)
[@afk, @name, @chname, @locatie, @aard, @actie].flatten.each(&block)
end

Hi,

I can’t seem to get my head around what is happening in the code-snippet
below.
What i think is happening is this.

each is called with a block.
this block iterates over each element of the flattened array.

But then?
what does the last call to each(&block) on line 2 do?

when called with a block
say self.each{|n| puts n}
nothing seems to happen and the flattened array is returned.

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.

call me stupid
Eelco

On Thu, Feb 23, 2012 at 9:58 AM, Catsquotl [email protected] wrote:

What i think is happening is this.

each is called with a block.
this block iterates over each element of the flattened array.

The block does nothing by itself. The block is an anonymous callback
function.

But then?
what does the last call to each(&block) on line 2 do?

when called with a block
say self.each{|n| puts n}
nothing seems to happen and the flattened array is returned.

Actually “self” should be returned conventionally. But that is just a
detail which has nothing to do with iteration.

I can’t really believe that nothing happens. Let’s just try this in
IRB:

irb(main):001:0> class X
irb(main):002:1> attr_accessor :a, :b, :c
irb(main):003:1> def each(&block)
irb(main):004:2> [a,b,c].flatten.each(&block)
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> x = X.new
=> #<X:0x202d2bc8>
irb(main):008:0> x.a = 1
=> 1
irb(main):009:0> x.b = 2
=> 2
irb(main):010:0> x.c = 3
=> 3
irb(main):011:0> x.each {|n| puts n}
1
2
3
=> [1, 2, 3]

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

Thank you,

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

On Fri, Feb 24, 2012 at 1:41 PM, Catsquotl [email protected] wrote:

Thank you,

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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs