Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie question about return value of == operator

26 views
Skip to first unread message

Blue Hand Talking

unread,
Apr 9, 2012, 1:20:26 PM4/9/12
to
I wonder if someone could explain how the '==' comparison operator
is being used below,
not quite getting it, or even the general flow.

Properties of Class Order:

attr_accessible :line_items
has_many :line_items
accepts_nested_attributes_for :line_items




def compute_order(order)
matched_line_items = order.line_items.select do |line_item|
line_item.product.tax_category == rate.tax_category
end

line_items_total = matched_line_items.sum(&:total)
round_to_two_places(line_items_total * rate.amount)
end


Would 'matched_line_items' be an array of product objects with
their tax_category value set from this operation?

If so, how is this happening? Seems that the comparison would be
returning true or false.

Also, here is Product#tax_category method:

def tax_category
if self[:tax_category_id].nil?
TaxCategory.where(:is_default => true).first
else
TaxCategory.find(self[:tax_category_id])
end
end

Thanks,

Jet

Robert Klemme

unread,
Apr 9, 2012, 4:05:35 PM4/9/12
to
On 04/09/2012 07:20 PM, Blue Hand Talking wrote:
> I wonder if someone could explain how the '==' comparison operator
> is being used below,
> not quite getting it, or even the general flow.
>
> Properties of Class Order:
>
> attr_accessible :line_items
> has_many :line_items
> accepts_nested_attributes_for :line_items
>
>
>
>
> def compute_order(order)
> matched_line_items = order.line_items.select do |line_item|
> line_item.product.tax_category == rate.tax_category
> end
>
> line_items_total = matched_line_items.sum(&:total)
> round_to_two_places(line_items_total * rate.amount)
> end
>
>
> Would 'matched_line_items' be an array of product objects with
> their tax_category value set from this operation?

Normally not since #== usually works read only. On face value
matched_line_items are all elements from order.line_items which have a
product with tax_category equal to rate.tax_category.

> If so, how is this happening? Seems that the comparison would be
> returning true or false.

Right. Actually #== can return anything - Ruby will treat false and nil
as false and everything else as true.

Cheers

robert

Simon Krahnke

unread,
Apr 9, 2012, 4:23:29 PM4/9/12
to
* Blue Hand Talking <j...@whidbey.com> (19:20) schrieb:

>I wonder if someone could explain how the '==' comparison operator
>is being used below,
>not quite getting it, or even the general flow.
>
>Properties of Class Order:
>
> attr_accessible :line_items
> has_many :line_items
> accepts_nested_attributes_for :line_items
>
>
>
>
> def compute_order(order)
> matched_line_items = order.line_items.select do |line_item|
> line_item.product.tax_category == rate.tax_category
> end
>
> line_items_total = matched_line_items.sum(&:total)
> round_to_two_places(line_items_total * rate.amount)
> end

The select method on an array works by returning an array of the
elements for which the block returns true. do introduces such a block.

That means matched_line_items are the order.line_items for which the
line_item's product's tax_category is the same as rate.tax_category.

mfg, simon .... hth

Blue Hand Talking

unread,
Apr 11, 2012, 11:54:15 PM4/11/12
to
Robert and Simon,

Thank you both. did read the documentation on select,
but evidently did not mull over it enough.

/****************************************/

select {|item| block } → an_array

Invokes the block passing in successive elements from array, returning
an array containing those elements for which the block returns a true
value (equivalent to Enumerable#select).

/****************************************/


Hmm, looks pretty straight forward this time :)

Cheers,

Jet

Robert Klemme

unread,
Apr 12, 2012, 11:54:46 AM4/12/12
to
On 12.04.2012 05:54, Blue Hand Talking wrote:
> Robert and Simon,
>
> Thank you both. did read the documentation on select,
> but evidently did not mull over it enough.

You're welcome!

> /****************************************/
>
> select {|item| block } → an_array
>
> Invokes the block passing in successive elements from array, returning
> an array containing those elements for which the block returns a true
> value (equivalent to Enumerable#select).
>
> /****************************************/
>
>
> Hmm, looks pretty straight forward this time :)

:-)

Apparently that always happens in hindsight. :-)

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

0 new messages