Newbie question about return value of == operator

  • Thread starter Blue Hand Talking
  • Start date
B

Blue Hand Talking

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
 
R

Robert Klemme

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
 
S

Simon Krahnke

* Blue Hand Talking said:
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
 
B

Blue Hand Talking

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
 
R

Robert Klemme

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top