Skip to content

Commit

Permalink
Notes/tech/coding/ruby.md
Browse files Browse the repository at this point in the history
  • Loading branch information
dvogt23 committed Feb 14, 2024
1 parent 6b79504 commit 20f0c22
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Notes/tech/coding/ruby.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ pluralize(count, "apple") # pluralize if count > 1 to apples

```

### Lambda composition
Combine lambda blocks together with `>>` i.e. to create a new one like `TAX_FEE = TAX >> FEE`

```ruby
# List of our individual pricing rules
TAX = ->(val) { val + val*0.05 }
FEE = ->(val) { val + 1 }
PREMIUM = ->(val) { val + 10 }
DISCOUNT = ->(val) { val * 0.90 }
ROUND_TO_CENT = ->(val) { val.round(2) }
# One presenter
PRESENT = ->(val) { val.to_f }

# Pre-define some rule sets for some pricing scenarios
REGULAR_SET = [FEE, TAX, ROUND_TO_CENT, PRESENT]
PREMIUM_SET = [FEE, PREMIUM, TAX, ROUND_TO_CENT, PRESENT]
DISCOUNTED_SET = [FEE, DISCOUNT, TAX, ROUND_TO_CENT, PRESENT]
```

Now we can define a price calculator:

```ruby
def apply_rules(rules:, base_price:)
rules.inject(:>>).call(base_price)
end
```

Source: [get-around.tech](https://getaround.tech/ruby-lambda-composition/)
## Object lookup

```ruby
Expand Down

0 comments on commit 20f0c22

Please sign in to comment.