January 2019
Intermediate to advanced
246 pages
5h 23m
English
Later on in our project we’ll have to fetch daily exchange rates from XML files stored on a website. They contain that information in the following form:
| | <title>1 USD = 0.74402487 GBP</title> |
At this point in our program, we know the base currency, “USD,” and the exchange currency, “GBP,” and we want to get the exchange rate 0.74402487. Here’s how we can do this:
| | base_currency = "USD" |
| | currency = "GBP" |
| | line = "<title>1 USD = 0.74402487 GBP</title>" # exchange rate format |
| | |
| | regex = { |
| | :open => /<title>1 #{base_currency} = /, |
| | :close => / #{currency}<\/title>/ |
| | } |
| | |
| | rate = line.gsub(regex[:open], "").gsub(regex[:close], ... |
Read now
Unlock full access