Build a Pretty Price Method in Ruby
In this Ruby coding exercise, we're going to build out a pretty price method inside of Ruby and what that really means because that name is a little bit vague. What it means is that it's going to be a method that can take in two different numbers so it can take in whatever the gross cost is and then it's going to be able to take in a decimal extension.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

So why I actually use this example because I needed to build this into an application pretty recently. And so I want to share it with you I thought to be a fun exercise to build out. What the issue was is there was a system I was building that needed to have a pretty price so something that looks something like this so it could say 7.95 but this number is auto-generated. So it was taken from another calculation which means that the value might have been 7.20 or 7.01 or 7 flat or it even could have been 7.99.

And so I didn't know what that number was going to be because it was generated in some other part of the program. I simply knew that I was going to get some kind of decimal value and that I needed to make it look like something you would see in an e-commerce shop and so that's what we are going to build out.

Now the one little caveat I want to add here is I want this to have a relatively flexible API. And so what that means is if you come down here you can see we have two different ways of creating this pretty price. One is by passing in a gross cost and then also passing in a decimal extension. Now if you look at the three examples here the decimal extension can be either 95 2 or 99 and that is simply what gets added to the return value.

large

But if you come down here there are many times where if I were making this as a ruby gem or some kind of code library like that then some developers might prefer to pass in an actual decimal. And so what this means is the system should be able to take 95 automatically know that it means that we mean 95 cents not 95 dollars and then adjust it but if .95 or 0.95 gets passed in then that should be what gets added to the system and so that is a little bit of a twist on how to do this.

I have the code right up here that you can use to start off. And so I highly recommend that right now you pause the video, try to build this out yourself, try to get all of the test passing and then you can come back and watch how I would build this out.

Welcome back. If you built that out yourself and it's working great job, if not we're going to go through the solution right here. So the very first thing I'm going to do is I want to figure out how to get the clean version of these numbers. So right here you can see that I might end up with a 3, I might end up with a 100.83, or 42.25. I don't know, I might get an integer, I might get a decimal. But no matter what I get I want to strip off the decimal here.

Well the way we can do that is we can take a decimal and then cast it as a integer. So if I switch down here and switch into irb you can see here I have a number such as 100.23. If I wrap this up and cast it to an integer like this you can see that, that strips off that decimal value and that's working perfectly.

large

And then if I take that value and I want to add it to another decimal so if I say Integer 100.23 and then if I want to add say 95 cents on to it you can see that that is the exact behavior that we're looking for.

large

So I'm going to close that out and so now we know pretty much how to build out the basic functionality. So I know that I can cast an integer so I'm going to take this gross cost and I'm just going to cast it as an integer and then I'm going to attack on the decimal extension. Now if we were just giving a basic implementation so if we were confident that we are always going to get a decimal like in these three examples here then this is all that we'd have to do for the solution. But remember that I want the ability to take in either a decimal value for the cent or a number value.

So the way I'm going to do that is I'm just going to add a conditional here right at the top and this is a little bit of what is called duck typing in Ruby where I'm going to ask the decimal extension what type of data it is so I'm going to override the decimal extension values I'm going to reassign it to decimal extension and then times zero point zero one. So if this were 95 this would change it to 95 cents and I'm only going to do that though if the decimal extension is an integer.

def pretty_price(gross cost:, decimal_extension:)
  decimal_extension = (decimal_extention * 0.01) if decimal_extension.is_a?(Integer)
  Interger(gross_cost) +_decimal_extension
end

So what that means is that if it recognizes that an integer is being passed in such as these three examples like 95, 2, or 99 these are integers and so if that gets passed in then the system is going to know that that is a whole number and then it's going to reassign decimal extension to that number multiplied by 0.01 and then it's going to go and build out that pretty price for us so we can test this out here so I can say pretty_price pass in because these are named arguments. So pass in a gross count of 55.23 cents and then a decimal extension of 99.

So now if I save the house and run it you can see that that returns 55.99 so that is perfect.

large

I can pass a whole number here and then change this up and make this a decimal. If I save this and run it, you can see that we're still getting 55.99 cents even though we've passed in some different types of value.

large

So in the first example we passed in a float here and then we passed in a whole integer here and then we swapped the data types in the second example and we're still getting the exact same behavior. So that is working nicely, let's come down and run the spec so this is rspec mathematical_computation/pretty_price_spec.rb so if I run this we have two examples zero failures so that is how you can build out a pretty price method in Ruby.

large

Resources