Slide 1

Slide 1 text

This slide intentionally left self- promotional https://pragprog.com/ titles/ruby5 35% off with code RailsConf_Atlanta

Slide 2

Slide 2 text

Let's look at some code

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Rails.env.production? instead of Rails.env == "production"

Slide 5

Slide 5 text

It's Rails Magic!

Slide 6

Slide 6 text

There's a secret:

Slide 7

Slide 7 text

It's all Ruby

Slide 8

Slide 8 text

Rails on Ruby: How Ruby Makes Rails Great Noel Rappin (@noelrap@ruby.social) https://www.chime.com http://noelrappin.com

Slide 9

Slide 9 text

5 Ruby tricks in the Rails codebase

Slide 10

Slide 10 text

We're not going to explain all of Rails' internals

Slide 11

Slide 11 text

We're also not going to be defensive about Rails design

Slide 12

Slide 12 text

Act 1: String Inquirer

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

method_missing the method of last resort

Slide 15

Slide 15 text

Allows you to parse the method name

Slide 16

Slide 16 text

Rails used to use this for AR record finders find_all_by_name_and_email

Slide 17

Slide 17 text

If the method name ends in ? compare it to the value of the string Rails.env.production?

Slide 18

Slide 18 text

respond_to_missing?

Slide 19

Slide 19 text

Why do this?

Slide 20

Slide 20 text

Flexible to new environments

Slide 21

Slide 21 text

Encapsulates knowledge of internals

Slide 22

Slide 22 text

Act 2: View Assigns

Slide 23

Slide 23 text

Here's some code...

Slide 24

Slide 24 text

instance_variables returns a list: ["@user", "@country"]

Slide 25

Slide 25 text

returns a hash: {user: "Noel", country: "USA"}

Slide 26

Slide 26 text

And some other code

Slide 27

Slide 27 text

instance_variable_set("@user", "Noel")

Slide 28

Slide 28 text

Easy to use by devs (Especially if you don't think about it)

Slide 29

Slide 29 text

Act 3: Callbacks

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

define_method

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

This is part of the code

Slide 35

Slide 35 text

Used by Controllers ActiveJob ActionCable ActiveRecord

Slide 36

Slide 36 text

Easy to implement new callback hooks

Slide 37

Slide 37 text

Act 4: Dirty

Slide 38

Slide 38 text

ActiveRecord::Dirty API > user.zip_code = "60015" > user.zip_code_changed? true > user.zip_code_was "02134"

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

included

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Looks for a pattern match

Slide 44

Slide 44 text

first_name_was becomes attribute_was(:first_name)

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Also used for ? and =

Slide 47

Slide 47 text

AR doesn't like looking up attribute names

Slide 48

Slide 48 text

Act 5: Migrations

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

class_eval

Slide 51

Slide 51 text

Makes reversible migrations very easy

Slide 52

Slide 52 text

Conclusion: Why is Rails like this?

Slide 53

Slide 53 text

We have to talk about discoverability

Slide 54

Slide 54 text

The "Look up Definition" IDE button doesn't work

Slide 55

Slide 55 text

One Neat Trick source_location > ApplicationController.method(:before_action).source_location => ["[REDACTED]/actionpack-7.0.4/lib/abstract_controller/callbacks.rb", 204]

Slide 56

Slide 56 text

Flexible

Slide 57

Slide 57 text

Generalizable

Slide 58

Slide 58 text

Developer experience

Slide 59

Slide 59 text

When can you adopt these methods

Slide 60

Slide 60 text

This slide intentionally left self-promotional https://pragprog.com/ titles/ruby5 35% off with code RailsConf_Atlanta @noelrap@ruby.social noelrappin.com