Talks
Active Support 3, It's finally getting interesting
Summarized using AI

Active Support 3, It's finally getting interesting

by Bryan Liles

In the video titled "Active Support 3, It's finally getting interesting," Bryan Liles introduces attendees at LA RubyConf 2011 to the underappreciated features of Active Support 3, a core component of the Ruby on Rails framework. The talk emphasizes the utility and functionality of Active Support in enhancing Ruby code and includes a comparison to the Facets library, which predates Active Support. Key points discussed include:

  • Introduction to Active Support: Active Support is presented as a critical, yet often overlooked, part of the Ruby on Rails ecosystem that provides various utility classes and extensions.
  • Comparison with Facets: Liles outlines similarities between Active Support and the older Facets library, highlighting how both enhance Ruby programming through utility methods without polluting namespaces.
  • Key Features of Active Support: Several features that make Active Support advantageous are explored, including:
    • Accessors: Active Support allows defining accessors at both the instance and class levels, simplifying code management.
    • Benchmarks: Offers an easy way to benchmark code execution through Active Support's Benchmarkable module.
    • Callbacks: Implements callbacks akin to Active Record, allowing methods to respond to specific events, enhancing modularity.
    • Concerns: Introduced as a way to manage shared code within models, reducing boilerplate.
    • Instrumentation: Active Support enables event tracking through its instrumentation system.
  • Flexibility and Performance: The speaker stresses the improved flexibility in Rails 3, allowing developers to include only the necessary components of Active Support without importing the entire library, thus optimizing application performance.
  • Encouragement to Read Code: Liles urges developers to read and learn from existing codebases, regardless of personal preferences for libraries.

In conclusion, the talk reinforces the importance of Active Support in Ruby on Rails development while advocating for a greater understanding and appreciation of its features. Liles hopes attendees came away with practical knowledge and insights on how to utilize Active Support more effectively in their projects.

00:00:29.539 Thank you. Hello everyone, I'm up first. This is a hard place to be because I don't know you, and you don't know me. So what I want to talk about today is Active Support. The reason why I'm going to talk about Active Support is that no one ever talks about it. Everyone wants to talk about the crazy projects, but let's talk about the project that we use every day.
00:00:39.899 But first, let me show you my badge. Sorry about that. I just wanted to inform everyone to ignore this. My name is Bryan Liles, spelled B-R-Y-A-N, space L-I-L-E-S. So let's talk about Active Support. Here's the interesting thing about Active Support. Does anyone know of a library that was kind of like Active Support but existed before Active Support and still exists today?
00:01:14.460 That library is called Facets. I actually knew about Facets before Rails even existed, and I think everyone here should take a look at it. It’s a really interesting collection of utility methods over at rubyworks.github.com. Let’s check out Facets. On the Facets homepage, they provide a very interesting little blurb that I’ll read for you. It states, "Ruby Facets is the premier collection of general-purpose method extensions and standard additions for the Ruby programming language." What does that mean? I'll translate that for you.
00:01:55.380 And there’s more! And you know what that makes me say? "Fascinating!" There are a whole bunch of neat little utility things that you can use to make Ruby even more fun to program. Actually, that’s the biggest thing about Facets; they make Ruby more fun to program when they don’t mess with the rest of your libraries. They don't trample all over the namespaces, which is often a concern. So let's look into this and see what we’ve got.
00:02:38.579 One interesting feature that Facets has is something called `interpolation`. You can just create a string and let’s call it 'hello'. Inside there, you can make a little interpolation, and it does the same thing that it would do in Ruby. Pretty simple, but I bet you didn’t know that! Who here knew this? Okay, I taught someone something, so I'll take credit for that.
00:03:19.260 Here’s another cool feature: symbolizing keys. Where have we seen symbolized keys before? That's right, Active Support! There’s some overlap here, and this is what I’m trying to show you. There is indeed some overlap.
00:03:42.659 So, there’s a bug in my slides; that’s the beauty you come to see! I must let you know there’s no guarantee that anything you see on these slides is correct.
00:04:03.720 Moving on, we have UTC timestamps. We see much more familiar versions of similar functionalities inside of Active Support. However, you may not need it because it's already in Facets. What I'm suggesting here are alternatives to what you already know.
00:04:36.400 So, we looked at the core library of Facets, and there’s actually more to Facets. It gets even crazier! You can generate random letters because who doesn't love random letters? And there’s something else that you've probably seen before in Active Support. That's right, another similarity!
00:05:07.620 You’ll notice that you can include 'memorizable'. Be careful when using this stuff, as there’s no namespace in Facets. What this does for the uninitiated is that we are saying we’re going to include this module named 'memorizable', and we’ll memorize this method. It will only run once in the context of this class or this object.
00:05:59.979 So, the third part of Facets is something called `on_tour`. I don’t know why they call it that; they were probably trying to be funny. Inside of that module, I noticed one interesting thing: a 'Y Combinator' function is there. Who here knows how the Y Combinator works? I just put this slide up there; I thought it looked neat, but I have no idea how it works.
00:06:35.760 So why do we have Facets? Well, we have Facets because there’s a lot of code in it. Even if you don’t choose to use any of it, all of us as developers should be reading lots of code, and there are plenty of codes in Facets to read. You can take your lessons from this collection, whether it's good or bad, because at least you can read it. Make sure to follow namespaces, please!
00:07:14.880 After that story, let’s talk about Active Support. What I’m trying to do here is illustrate the similarities between Facets and Active Support. All of David’s wisdom could just be summarized in a README file; let me read it to you: 'Active Support is a collection of utility classes and standard library extensions that are found useful for the Rails framework.'
00:07:40.259 These additions reside in a variety of namespaces, but what is David really saying? … So what we have here is, what David was trying to say back on February 15, 2005, was that Active Support and Active Record, along with Action Pack, were starting to have a lot of similarities.
00:08:06.300 He said, 'You know what? Let’s break this out into something called Active Support.' The cool thing is, I don’t think there were any blog posts or anything; I actually just had to dig through this.
00:08:38.900 And whenever I release these slides, you’ll notice the project’s initial GitHub shot. So what is Active Support? Active Support is everything that makes Rails work better.
00:09:04.560 Now, I’ve been up here for seven minutes and 15 seconds, which means I have 23 minutes remaining and 80 more slides to go. So let’s get started!
00:09:35.239 I had to pick a small representative. This is weird; I'm not used to having this much time to talk about things. So what I’m going to do for these six items is just glance over some of the cool things that I think are present in Active Support. The first thing is 'accessors'. What’s the coolest thing about accessors? Anyone want to take a guess?
00:10:06.700 No? Okay. I just had to throw a slide of a beer in there; it’s just funny. There’s something cool about accessors. If you notice, when you’re using Active Support, accessors are usually only available at the instance level. But Active Support gives you the ability to use accessors at both the module and class level.
00:10:23.460 So you can have this accessor for instance level. Additionally, I like using examples with 'cows' as classes. You’ll see this often in my examples.
00:10:48.080 What else can we do with accessors? You can have 'adder accessors with default'. I bet you guys know what this is going to do. You can define the name and decide what the default values should be. In my example, I define it, instantiate it, and look at that! It works!
00:11:08.700 But, if you’re using Rails, don’t get too excited—this isn’t working in Active Record context, so that’s the caveat.
00:11:30.000 Moving on to 'benchmarks'. We always want to see how fast our code is. Many of us just use Benchmark and call it by ourselves. However, Active Support provides you with code to make this very simple. In this example, we include Active Support Benchmarkable, and all we do is specify inside of this block what code should run fast.
00:11:56.940 You’ll notice that I have a logger defined because it actually depends on the logger for output. This is how it works in a Rails application.
00:12:37.679 Now, let’s talk about 'callbacks'. A few months ago, I did a talk about Active Model, and it uses a lot of callbacks. So you want to include Active Support callbacks inside your code, and we can define a callback called 'CenterAddress'. Because, as we know, we always want to synergize!
00:13:01.479 Now, once we have our startup defined, we make an instance of a startup. You might have heard this before, so after we synergize and define our startup, we retrieve more VC funds—because that's what we do.
00:13:47.860 So, we have 'Billy Bill Startup' that inherits from 'Startup' because it is, in fact, a type of startup. Less evil is better, right? We send the callback to synergize, and then we say that our pivot is complete.
00:14:26.080 Now, I know we’ve seen this before. I actually typed in a Rails example of how callbacks work in Active Record; this is exactly how they function. You see a little bit of magic with Active Model and related processes, but this implementation is neat and wrapped-up nicely.
00:15:20.260 It’s not every day you find that special snippet of code on the internet and just knowing you can use it later! We've all had moments when we thought we'd mastered something, but there's always more code to learn from.
00:15:52.120 At a high level, what we’re doing here is finding a 'vaccine'—or rather, a solution—when we include this in our code. This is how a lot of plugins operate in Rails.
00:16:18.540 Currently, we have class methods and instance methods, but then you have those who think it’s too much to type. What we want right now is to simplify this process. Recently, after an old issue was revisited this past week, they introduced something called 'concerns'.
00:17:00.780 Concerns allow you to create modules in a neat and convenient way that removes a lot of this boilerplate code. You don’t have to include everything—you just need to follow the conventions without the need for cataloging.
00:17:37.659 Next, let’s discuss something configurable, which means adding a feature that is highly flexible. For those not aware, Arduino is highly configurable; so let’s look at something in Active Support that perhaps shouldn’t exist, but I want to show it to you anyway.
00:18:05.669 You can include something called Active Support Configurable, allowing you to instantiate a new class and set configuration variables right off the bat. While there might be other ways, doing it this way looks nice!
00:18:37.579 Now let’s talk a bit about instrumentation. Is anyone familiar with this piece inside Rails 3? Active Support 3? No one? Come on! This is one of the coolest parts of Rails! It gives you a kind of queue where you can instrument events, allowing your application to respond when certain milestones occur.
00:19:19.840 Here’s how it works: you can subscribe to events and run blocks of code whenever these events happen. For example, you can 'milk' a cow and send the options into your method. So now we listen in and track these events and store them in an array for processing later.
00:19:54.980 Now, it’s just a simple hash; you can state the event, its duration, and the payload. This is a very straightforward implementation; there are even more complex ways, but this is an easy solution to implement in any active application.
00:20:20.680 So, where does all this lead us? That's a question I asked while preparing this talk. I wanted to present an important feature of Active Support 3: using it in a non-Rails app. Previously, utilizing Active Support meant including everything, which would bloat your application.
00:20:50.740 Now in Rails 3, you can require Active Support to include only the pieces you actually need! If you only want certain components like notifications or callbacks, you can include only those instead of the whole collection!
00:21:33.600 I am here to entertain! In closing, this was a discussion about the importance of reading and understanding the pieces of Active Support. Also, the reality is that many people do not talk about code or utilities that should be widely recognized.
00:21:53.459 At most, I probably taught you seven things, even if you never try them. Either way, I do hope you have learned something today. Thank you!
00:22:26.180 You’ll notice I didn’t use one ‘f’ word in the whole thing. So, any questions?
00:22:58.660 What else would you like to discuss? Yes?
00:23:27.660 That’s an interesting point! Instrumentation is something that's invaluable to the Rails framework but can be complex at times. Yes, that’s correct! I went through Active Support in detail while preparing. Rails does utilize it effectively.
00:24:07.020 Have I used Facets in a project? Yes! I actually utilized it during a timeframe nearly 2008 and above while Rails was developing.
00:25:00.760 That’s exactly it! Overlapping libraries can complicate things, and it is best to ensure compatibility. Always take a look at how they share or conflict with namespaces.
00:25:36.080 Any more questions? Alright, it’s now 12:57. Thank you, everyone!
00:25:55.500 Thank you!
Explore all talks recorded at LA RubyConf 2011
+5