Using Predicates to look at an array

Hi all,

I have an array of shipping_type’s being returned, and I want to see
what is in there. In the past I have done:

 shipping_type.include?(Cart::SHIPPING_TYPE_REGULAR).should be_true

This works, but looks really ugly… It just doesn’t roll of the
tongue very well. I then looked up the use of Predicates, which I
had been using, but hadn’t realized:

 shipping_type.should be_include(Cart::SHIPPING_TYPE_REGULAR)

This works, however the syntac of be_include looks very odd… Is
there any way to do:

shipping_type.should_include(Cart::SHIPPING_TYPE_REGULAR)

How are other folks looking at the contents of an Array? Am I going
around bassackwards on this?

Eric

On 10/4/07, Eric P. [email protected] wrote:

 shipping_type.should be_include(Cart::SHIPPING_TYPE_REGULAR)

You were close on this one, it should just be

shipping_type.should include(Cart::SHIPPING_TYPE_REGULAR)

It’s a special predicate just for working with arrays.

Cheers,
/Nick

On 10/4/07, Eric P. [email protected] wrote:

 shipping_type.should be_include(Cart::SHIPPING_TYPE_REGULAR)

This is what I usually do; I agree the syntax of be_include looks odd,
as is
true for some other predicates.

OTOH, it’s hard to find a formulation that works well for all
predicates.
For instance:
domain.should_valid doesn’t look as good as domain.should be_valid
domain.should_include(x) looks better than domain.should be_include(x)

Rspec could support both, which means you could select the one that
“looks”
right, I guess. That might lead to inconsistent usage. I can live with
the
current approach as well, looks odd, but it’s not a serious problem for
me.

  • Geoffrey

On 10/4/07, Geoffrey W. [email protected] wrote:

domain.should_include(x) looks better than domain.should be_include(x)

Rspec could support both, which means you could select the one that “looks”
right, I guess. That might lead to inconsistent usage. I can live with the
current approach as well, looks odd, but it’s not a serious problem for me.

As Nick points out in this thread, rspec supports:

collection.should include(some_item)

Also, you can define your own predicate matchers very easily using the
predicate_matchers collection:

predicate_matchers[:be_able_to_cook] = [:can_cook?]

chef.should be_able_to_cook
=> passes if chef.can_cook?

Cheers,
David

On Oct 4, 2007, at 12:46 PM, Nick S. wrote:

tongue very well. I then looked up the use of Predicates, which I
had been using, but hadn’t realized:

 shipping_type.should be_include(Cart::SHIPPING_TYPE_REGULAR)

You were close on this one, it should just be

shipping_type.should include(Cart::SHIPPING_TYPE_REGULAR)

It’s a special predicate just for working with arrays.

Isn’t it a general predicate for all foo? (boolean) methods?

class Object
def foo?
true
end
end

Object.new.should be_foo

Doesn’t that work, as well?

Scott

On 10/4/07, Scott T. [email protected] wrote:

be_true

Object.new.should be_foo

Doesn’t that work, as well?

Sure it does, but be_include sounds icky. That’s the point of this
thread, I believe.

Then it is a matter of changing your code or adding a .autotest like so:

Autotest.add_hook :run do |at|
at.exceptions = /generated_output/
end

This allows you to ignore directories that match a regexp.
Unfortunately, there is no
hook in Autotest to allow you to ignore single file regexp which I
need to ignore flymake.rb
files when running emacs

Is there a way to have Autotest ignore a directory? I am working on a
project that involves code generation and some spec stub files are
created. This causes Autotest to run over and over. Ideally I would
like to be able to say something like
autotest --ignore /generated_output
or have an entry in my .autotest file that told autotest to always
ignore that directory.

Thank you,
Matt M.

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