Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So I thought of some more analysis that I'd like to do with our orders.
Instead of just identifying the pending and completed, what if we divide it into two groups?
Hmm, that's a good idea.
Now we could probably do that with a combination of select and each, but that really is
the long way around the barn because the innumerable module gives us some higher level
methods for doing this and other tasks like it.
So let's have a look at that.
So you said you wanted to divide the orders and the method for doing that is actually called
partition in the innumerable module.
It takes a block.
It's going to give us all the orders as we would expect here and we want a partition
then based on their status.
So we're going to say the status is equal to pending.
And let's just see what that returns to us.
I'm going to assign this to a variable called results and then I'm going to use the P method
and pass it that array and the P method different than the put estimate that here is just
going to show us the internal representation of that array.
So we're going to see the array in its rawst form.
So we run this.
Here's the output that we're getting right here.
And we see that we get an array, but inside of that array are actually two sub arrays.
Here's the first sub array.
Notice that it has the orders that have a status of pending.
So it's the orders that match the criteria in our block and then it has a second sub array
right here and it contains all the orders that aren't pending.
Okay, so our results array here is actually an array of arrays.
And there's an easy way to destructure this into two individual arrays in Ruby.
We can do that simply by assigning to two different variables.
So our first variable we called our pending orders and our second variable will be the
completed orders.
That's everything else.
And because this method returns an array of arrays and we have two variable names separated
by a comma on the left hand side, it'll go ahead and separate those two sub arrays out
into these two different variables.
So then here we can just say, okay, here are our pending orders.
Put us pending orders.
That's the name of the variable.
And then here are our completed orders.
Just like that.
If we run this, now we see, okay, here's the heading pending.
We've got the two pending orders and completed, the two completed orders.
So we've got two separate arrays now, one containing pending orders, one containing completed
orders.
So that gave me an idea.
We could do something similar for big orders and small orders.
Sure, we'll just use partition again.
I'll do this just from the beginning.
We'll just go, we want a big orders array, we want a small orders array.
We're going to take our orders array here and partition it, knowing that it's going to
give us two different things back here when we destructure that.
O is going to be the block parameter.
In this case, we want the order total greater than or equal to 300.
So for all the orders where this returns true, it's going to be put in this array.
And for all the other orders, they're going to be put inside of this array.
So we'll just print those out.
These are big.
And here are smaller ones.
Run that and sure enough, a partition them just as we would expect.
Our big orders and our smaller ones.
So here's another example of the partition method in action.
Let's say we also track the country in which the order was placed.
So the order class also has a country attribute.
And we're interested in dividing the domestic and international orders into two separate
arrays.
The very clever partition method makes this really easy.
The orders for which the block returns true, in this case, the country is USA, end up in
the first array domestic.
The orders for which the block returns false, any country not USA, end up in the second
array, international.
So it just got to know from marketing.
And it says here that they want to get all the emails from our orders so they can email
our customers and send them a newsletter.
So we basically need to transform all of our orders that are in an array into an array
of emails.
So let's see how to do that.
So let's start with a label here.
These are going to be our newsletter emails.
And we want to create a new array that contains the email.
So I'm going to have an empty array called emails right there.
And then what we need to do is go through all the orders in the orders array and accumulate
those emails into the email array.
So we already know one way to do that would be to use our each method.
We're going to take our orders array.
We can call each, give it a block.
That's going to give us an order.
Then we can append to that emails array.
I'm going to do this all on one line by taking the orders email just like that.
Oh, and we should downcase them.
Sure.
We can convert those to lowercase using the downcase method because email is just a string.
So that takes all the orders.
Converse them into emails and puts them in the emails array.
And then we can just print out that array.
And again, I'm going to use p which will give us the internal representation of that array
using p emails.
Print that out and we've got our array of emails printed out nicely like that.
Now this works, but there's a much better way to do this using the map method.
We can actually get rid of this temporary variable that has our empty array called emails.
And we can use the map method instead of each.
The map method is going to take everything that's in the orders array in this case.
And then we're going to give it a block until it we want to new array containing only
these elements or these attributes of an array.
So rather than appending them to an array, we can just say take the orders and convert them
into their downcase emails.
Now the map method returns a new array that array is just going to contain our emails,
which are going to be strings, which we can print out that way.
So if we run this, we get exactly the same thing.
We've got our array of custom emails.
So what happened was the map method here called the block for each element in the array.
And then it placed whatever the block returned in the new array.
In this case, the block is returning.
This is the only expression in the block.
So it's implicitly returning the value or the result of that expression, which will be the
downcase email.
So what we get back is an array containing all the values returned from calling the block.
And then we just print them out.
Now there's a gotcha with this.
And it's the same gotcha we saw a little bit earlier, which is if inside of this block
you use puttest to actually print out the email inside of the block.
Well remember, puttest is going to print out this email to the screen, but then puttest
itself returns nil.
So what happens if we do this?
Well, we get back in a array of nil because puttest returns nil map just takes whatever's
returned by the block nil in this case and puts it in the new array emails.
So that's just something to watch out for.
One important thing to note here is that our original orders array hasn't changed.
Map hasn't changed it and we can print out our orders.
I'll just use p to do that.
Our original orders array.
And we see that it still has all of our orders in there.
So let's recap.
We have an array of four order objects.
The map method calls the block for each order and places the blocks return value in a new
array.
So when mows order is passed to the block, the return value of the block is mows email
downcase or a string.
That string is then mapped into the first position of the new array.
The map method iterates through each successive order mapping the orders downcase email
to the corresponding position of the new array.
As we're calling map on an array of four orders, it returns an array of four emails.
The return to rate is always the same size as the original array.
So the folks from the marketing department talk to people from the finance department and
figure out what we can do.
So now the finance department wants to know the taxes on the orders in Colorado.
Okay, so what we need to do is filter all the Colorado orders out of our orders array.
And then we need a new array that just contains the tax for those orders.
Right.
Okay.
Well, we can pull this off by stringing together a couple of the methods we've already
used.
So conveniently, we already have this tax method that computes the tax based on the total
and the state of the order.
So we don't have to worry about that part of the equation.
Down here at the bottom then, we're just going to print out our taxes.
So the first part of this is just to get all the Colorado orders.
I'm going to assign that to an array called CO orders.
We're going to use orders.
We want to filter all the orders from Colorado.
So we're going to use the select method to do that.
And our criteria is the order state equals Colorado.
All right, that's step one.
The next step then is to get the taxes for all the orders from Colorado.
So what we're going to do is we're going to take our CO orders array and we're going
to map those orders to their tax.
It's not an actual attribute.
So you'll often want to use any methods that derive values like a tax here inside your
blocks, this criteria.
You don't have to just check individual attributes.
Actually in this case, whatever the method returns, we'll get mapped into that position
of the array.
And then we'll just print out our CO taxes array.
And you see that we've got an array of two values for and two that match the two Colorado
orders that we have.
Now we can actually refactor a bit to do all of this on one line and chain together method.
And you'll often see Ruby code that does this.
So it's good to sort of get used to it.
What we can do is at the end of the select, remember select returns and array.
So we can turn around and just call map on whatever's returned by select.
And this is going to end up being our CO taxes.
So we select all the orders from Colorado.
That expression returns an array, which we then just turn around and call the map method
on.
The result of that's going to be an array of taxes and we assign that to CO taxes.
And that works just the same.
So here's our last request.
This one is from the sales department.
Uh oh.
So we need to iterate through all the orders and just accumulate their total.
Right.
Okay.
All right.
We solved this before using code that looks something like this.
We just had a sum variable and then we used each to iterate through the orders and
use the plus equal operator to accumulate the order total into sum and then we just printed
out total sales that way.
But as you might imagine, there's an easier way to do this.
And that's by using the reduced method.
The reduced can be a little bit tricky.
So let's jump over to a scratch pad and play around with some basic numbers.
So here's on array of numbers and let's say that we just want to sum up all of these numbers.
The way we do that is by using the reduced method on that array.
The reduced method takes a method parameter.
In this case, it's going to be the initial value, which we're going to set as 0.
And then it takes a block.
Now this method gives two block parameters.
The first is going to be an accumulator value or a variable in which we want to accumulate
these numbers as we total them up.
And the second block parameter is going to be the actual number.
This is going to be the elements in the array.
It's going to give us each of those number elements.
Then the block itself is going to take the sum and it's going to add the number.
Now notice we're not using plus equals here.
We're just saying sum plus number.
And the reduced method will take the result of this expression.
And each time through the iteration, we'll assign it back to this sum.
So you're just going to go through each of those numbers, running the block and taking
whatever the block returns and adding it to sum.
So it'll take care of that for us.
Now the return value of reduce is the value returned by the block the last time it was
called.
So we need to assign it to a variable or print it out.
I'll just assign it to a variable called result here and we'll just print out result.
Right?
What do we get back when we run it?
Well, it's 10.
It's the sum of all those numbers.
So you can think of reduce as reducing a collection down to a single result by running
the code in the block for successive elements in the array.
Now you may see reduce called without a method parameter.
It's an optional parameter so you don't have to pass into parameter there.
If you don't pass into parameter, then the initial value will be the first element in the
array.
In this case, it's going to be one.
Now for the first iteration of this array, all it did was take zero, which was our initial
value and added one.
So this is going to do the exact same thing for us.
We run that.
Sure enough we get 10.
You may also see a shortcut used with reduce.
There are a couple different shortcuts you can use.
Right?
One of them is you can just drop the block completely and as a method parameter, you can pass
in the name of a method or an operator to apply to each element.
In this case, we want to add them up.
So we use a symbol and then we say we want to use the plus operator.
That's just going to run the plus operator against all the elements in the array.
Numbers in this case and numbers in Ruby support plus.
So we get the same value if we do that.
And if you want to set an initial value, say for example zero, you can say zero comma
and then that plus operator.
Or if you wanted to multiply all these things, you could actually pass in, for example,
the multiply operator, which gives us 24.
But that only works for objects that support things like times and plus.
In this case, we have numbers in our array so it works out just fine.
So that's another shortcut you may see used with the reduce method.
So we can apply this to summing up all our order totals.
Sure.
Instead of doing it the long way here, take our orders and set of calling each will
call the reduce method.
We need to pass in an initial value because remember our orders array contains order
objects.
So if we don't pass in an initial value, it's going to try to use the first order in
that array and it doesn't know to use the order total.
So we're just going to tell it start with zero.
Then inside of the block, instead of using plus equals, we just use sum plus the order
total.
Remember, it's going to take that order total and it's going to want to assign it back
to something.
That something in a science it back to is the first block parameter.
So we've got two block parameters, sum which is in a accumulator and order which is the
elements in the array.
Remember that reduce returns of value so we need to assign this to a value.
This is going to be our sum and we print out the sum below total sales of a thousand.
Well since we have a tax method, we could easily sum up all the tax amounts.
Sure.
It's a good practice here.
Our total tax is what we're looking for.
We're going to take our orders array.
We're going to reduce it down to block parameters remember, sum and order, although you can
call this whatever you want.
We could call it total and then inside of here we use total plus order dot tax and then
we just print out total tax.
We run that.
Our total tax is $22.
So once you start learning these innumerable modules, you often find multiple ways to do
the same thing.
Just as an example of that, there's a different way we could do this.
We're going to comment out the first one just so you can see it.
Instead of trying to reduce orders down to tax totals, we could do this another way.
We could take our orders.
We could map them to their tax.
That's going to give us an array of taxes.
A array of taxes is going to be an array of numbers so we could take that array of numbers
and reduce it down using the shortcut because numbers support the plus operator.
That should give us the same thing.
We just did it a different way.
It's sure enough our tax total is still $22.
So here's the last recap.
The reduced method takes an initial value zero in this case as the method parameter.
The associated block then takes two block parameters.
The first parameter is the accumulator so we called it sum in this case.
The second parameter is assigned successive elements in the array which in this case are orders.
So how does reduce work?
The first time through the iteration, sum is assigned the initial value of zero and order
is assigned the first order object in the array.
In the block, the value of sum and the orders total are then added together and return from
the block.
This return value 300 in this case is then assigned to sum to start the second iteration.
When we go through the same accumulation for the second object and all the other objects
in the array, the final value returned by the block 1000 in this case is then returned by
the reduced method and total equals 1000.
There's a lot you can do with these three methods and in the exercise you're going to
get a chance to practice with them with a variety of examples.
Again, in the next section, we'll shift gears a bit and learn how to write our own methods
that call blocks which lets us write more expressive and compact code.
See you then.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.