Performance Optimization

Summarized using AI

Hidden Gems of Ruby 1.9

Aaron Patterson • September 17, 2010 • Earth

The video titled "Hidden Gems of Ruby 1.9" features Aaron Patterson, a Ruby core committer, discussing the lesser-known yet valuable components of Ruby's standard library introduced in version 1.9. The presentation aims to enlighten developers on tools that do not require external gems and demonstrate their practical applications.

Key Points Discussed:

  • Introduction to Aaron Patterson: Patterson introduces himself, mentioning his work in open source at AT&T and hints at his humor regarding his career path.
  • Presentation Style: The talk is split into two segments—first a humorous introduction followed by the serious practical content focused on Ruby 1.9.
  • Upgrade to Ruby 1.9: He strongly encourages upgrading to Ruby 1.9.2 for its improved features and functionalities.
  • MiniTest: Patterson highlights MiniTest, noting its differences from traditional unit testing in Ruby, such as using minitest/autorun and introducing methods like refute and skip, which enhance testing capabilities.
  • ObjectSpace Enhancements: Introduced methods in ObjectSpace are discussed, including count_objects, size_of, count_nodes, and count_data_objects, which provide insights into memory usage and garbage collection.
  • Fiddle: Patterson presents Fiddle as a wrapper for the libffi library, explaining its use for calling native methods and its functionality compared to the deprecated DL.
  • Psych: The video discusses Psych, the YAML parser in Ruby, which supports JSON parsing and event-driven parsing, making it easier for developers to manage structured data.
  • Code Coverage: The coverage library is mentioned, detailing its methods to track which parts of the code were executed during tests, allowing developers to optimize performance effectively.

Conclusion and Takeaways:

Patterson concludes by emphasizing the importance of understanding the Ruby standard library's capabilities and encourages developers to explore these "hidden gems". He reiterates the significance of these tools in improving code quality and efficiency in development workflows.

Overall, attendees gain valuable insights into advanced features of Ruby 1.9 that can be consistently utilized without relying heavily on external gems, thus broadening Ruby's ecosystem for developers.

Hidden Gems of Ruby 1.9
Aaron Patterson • September 17, 2010 • Earth

When attempting to solve our latest 367 hidden gems of ruby 1 9programming conundrum, we typically reach for the latest Ruby Gem that solves the problem for us. Oftentimes in our search for a solution, we neglect to look through some of the great libraries that come shipped with Ruby. In this talk we'll explore some of the tools we can use that don't require a gem install. We'll especially focus on the new toys in Ruby's standard library that ship with Ruby 1.9, but also look at some of the toys that ship with Ruby Classicâ„¢. Be prepared to be amazed and entertained. But most of all, be prepared to go home with something useful!

Help us caption & translate this video!

http://amara.org/v/GZSe/

GoGaRuCo 2010

00:00:04.920 Aaron Patterson
00:00:16.500 Hello, I love Madonna! All right, all right, uh oh.
00:00:24.060 My God! Hello, happy Saturday! Uh, my name is Aaron Patterson, also known as Tender Love.
00:00:32.820 I'm Tender Love on Twitter, GitHub, etc. You can Google Tender Love, but the search results might not be safe for work, so be careful.
00:00:38.040 I work for AT&T Interactive, and I want to thank them because I am currently employed full-time to do open source work. So, I work on open source all day.
00:00:50.760 Because I work for AT&T, that means I must be an Enterprise developer, which I am. You can go to this address, and you'll see that I am, in fact, an Enterprise developer.
00:01:02.760 I am a Ruby core committer but not a Rails core committer; I’m just a Rails committer.
00:01:08.280 First, I want to talk about my failure. The first failure is that I am not a Rails core committer.
00:01:14.580 I wonder why I work on Rails full-time and I'm not one yet, and if you wanted the same thing, I encourage you to ask DHH.
00:01:21.420 Continuing on with my failures, I am presenting last.
00:01:28.020 I like Ryan Davis, but my slides suck. I'm sorry, Shane! I use MiniTest, and I enjoy reading RFC 2119 for fun.
00:01:38.040 Oh, by the way, I don't like RSpec. I don't have method parents on method definitions, so my code looks like this.
00:01:43.800 Most people here say that they use Ruby and JavaScript, but really, I code more in C than I do in JavaScript.
00:01:50.760 But really, I want to talk about my failures as a presenter.
00:01:57.440 I really like to have fun and give a talk. I want to have fun entertaining people while also learning something practical.
00:02:07.440 One of the best things about attending conferences is learning something and being able to go home and use that new knowledge.
00:02:18.220 The truth is, I'm a nerd, and I enjoy boring things. One of my biggest failures as a presenter is that I don't know how to combine fun with boring content.
00:02:25.280 Tonight, I'm going to give two presentations: a practical presentation and a fun presentation.
00:02:38.220 So, I like to eat dessert first, so we're going to start with the fun.
00:02:43.980 How many of you were at GoGaRuCo last year? Okay, yeah, all right!
00:02:49.920 I was not a GoGrouper last year. I probably should have listed that among my failures, but I heard last year there was a presentation that was very popular.
00:03:03.240 I want my presentation to be popular too, so I decided to write a guide about making popular presentations.
00:03:10.440 Ryan was reviewing my slides, and he told me that it wasn't about popularity so much as it was about notoriety.
00:03:17.220 So this is actually your guide to presentation notoriety.
00:03:24.000 As far as I can tell, the three bullet points you need are: a provocative title, risque photos, and possibly Ruby code.
00:03:30.180 First step, we need to come up with a provocative title. Using Ruby 1.9 like an engineer is not provocative, sorry.
00:03:37.259 The way to fix this is by making it sexier: 'Use Ruby Like a Sexy Engineer.' Easy, no problem!
00:03:44.040 Next up is risque photos. Everyone, this is super important.
00:03:49.320 What I like to do is take lots of photos and make sure that I come up with these risque images.
00:03:56.760 One of my favorite TV shows is America's Next Top Model, so I created a segment called America's Next Top Engineer.
00:04:02.280 If anyone here watches that show, you'll know that it typically involves a guy and a camera, who shouts out a bunch of words.
00:04:10.560 Then someone in front of the camera tries to act out what those words mean.
00:04:17.220 So, what I'm going to show you next are the words in combination with me acting out those words.
00:04:24.240 These are just some ideas for you to create risque photos for your slides.
00:04:29.640 First one: confidence, elegance, sultry, sexy, thoughtful, fierce, playful, powerful, and finally, provocative.
00:04:34.199 Thank you! Finally, the last thing to have a notable presentation is obviously Ruby code.
00:04:44.580 I went online and found some Ruby code, and I said, 'Hahaha, too long, didn't read, I don't care!'
00:04:51.240 So that wraps up my first presentation. Now, let's move on to the practical presentation.
00:04:56.760 I have a really bad sense of humor, though I should have mentioned that earlier.
00:05:03.300 The title of this talk is "The Hidden Gems of Ruby 1.9." This is essentially a Ruby 1.9 Public Service Announcement.
00:05:09.000 Please upgrade to Ruby 1.9! Ruby 1.9.2 is awesome! You should use it!
00:05:15.960 The reason I titled this talk this way is because I'm not going to talk about gems at all.
00:05:23.040 Everything I'm going to discuss is from the standard library, so please run now!
00:05:29.160 The first thing I want to talk about is MiniTest. This is our normal testing.
00:05:35.580 Normal tests look like the standard test unit stuff, but I'll highlight some differences between MiniTest and a normal test.
00:05:41.040 For starters, instead of requiring 'test/unit', we require 'minitest/autorun'.
00:05:46.080 Next, we inherit from 'Minitest::Test' rather than 'Test::Unit::TestCase'. So a base test might look like this.
00:05:53.220 Another difference is that we don't have 'assert_not' in MiniTest; we have 'refute'.
00:05:59.040 So this makes all the asserts and refutes align nicely. An 'assert_not' would translate to something like this: 'refute'.
00:06:05.880 We have a new method called 'skip', which lets us skip tests that don't make sense to run on a particular platform.
00:06:12.240 So when we run our tests, we’ll see an output that indicates how many were skipped, alongside the assertions and failures.
00:06:18.360 Next up is randomization. MiniTest randomizes the order in which your tests are executed.
00:06:25.740 You'll notice that this test modifies global values while it's running.
00:06:32.460 Sometimes it will succeed, while other times it will cause failures. That's a bad scenario for our tests.
00:06:38.520 So, we see that sometimes all four tests will pass, and everything is fine. Other times, we'll get an error.
00:06:45.900 You’ll see an option that says 'Test run options --seed'. What that seed does is determine the order in which the test should run.
00:06:52.740 You can grab that command line option '--seed', along with '-v' for verbose, and run the tests.
00:06:59.400 Then you’ll be able to see what tests were run and in what order, so that you can debug any issues.
00:07:07.320 Another interesting aspect is test performance. If we use MiniTests, we can see how fast the tests run.
00:07:14.460 For example, if one of our tests is slow, when we run the test with '-v', we can check for performance times.
00:07:20.940 If a test takes an unusually long time, you can find out where those slow tests are located.
00:07:28.200 If you're using rank, then you should add other test options. Testing on the environment variables is useful.
00:07:34.920 Okay, 13 minutes in, and I’m not even halfway done yet.
00:07:43.080 So, does everyone know that Ruby 1.9 ships with RSpec? No? You did not?
00:07:48.060 Take a look at this test; it may look like RSpec, right? Yes? Right!
00:07:54.780 Actually, it’s not; that is MiniTest's spec. MiniTest has an RSpec-like notation.
00:08:01.140 You just have to require 'minitest/spec' instead of 'minitest/autorun'.
00:08:06.420 Okay, apparently you do not need that require. Thank you, Ryan; I made a mistake.
00:08:12.480 Next up is ObjectSpace. How many of you know about ObjectSpace? Yes? I'm sure you've seen code that looks like this.
00:08:19.200 In Ruby 1.9, we have new extensions to ObjectSpace that can give us interesting statistics about the virtual machine.
00:08:26.010 There are four new methods: count_objects, size_of, count_nodes, and count_data_objects.
00:08:33.540 Count_objects returns a hash with keys as object types and values as their memory sizes.
00:08:39.180 Size_of returns the number of bytes that a specific Ruby instance uses.
00:08:45.720 Count_nodes actually counts the number of parse nodes that Ruby has identified.
00:08:52.200 Finally, count_data_objects counts instances of C-backed Ruby objects.
00:08:59.520 For instance, in a specific run, we had 64 instances identified by counting how many were found.
00:09:06.780 Next, let's discuss Fiddle. Does anyone know about Fiddle? It's pretty cool!
00:09:13.080 Fiddle is a wrapper for the libffi library. So, now we have Fiddle as a part of Ruby 1.9.2.
00:09:21.000 How many people have heard of Ruby FFI? Excellent!
00:09:27.660 Fiddle wraps native method calls and closure allocations.
00:09:34.020 It provides two utilities: one to call native methods and the other to allocate native closures.
00:09:41.040 However, it can't do any type of memory management that DL does.
00:09:47.520 I want to discuss how DL and Fiddle interact by calling functions with both.
00:09:54.840 We must follow four steps: open a dynamic library, locate the function pointer, wrap it, and call the function.
00:10:02.640 The final result shows how we wrap the sine function provided by the libm library.
00:10:10.440 Then, we can call this function and perform operations.
00:10:17.280 If we want to create a function that mimics the sine function, we can subclass Fiddle's closures.
00:10:24.720 The important thing here is to realize that when we call 'function.call', it exercises Ruby code only.
00:10:33.600 We can test our closures without interacting directly with native code.
00:10:40.020 The returned memory location from the closure can also be wrapped in a Fiddle function.
00:10:46.560 Now, let’s talk about Psych. Psych is the YAML parser in Ruby 1.9 and above.
00:10:55.740 It wraps the original YAML parser, replacing libraries as necessary.
00:11:03.120 With Psych, you can load and dump YAML conveniently.
00:11:08.520 New features include the ability to parse JSON since JSON is a subset of YAML.
00:11:15.480 Psych can also support evented parsing, which means you can get notifications while parsing.
00:11:22.200 You can write a handler to respond to specific events during parsing.
00:11:28.440 This allows real-time notifications when your YAML is processed.
00:11:37.800 Psych also supports immediate document emission without any buffering.
00:11:43.740 We've seen how this can be an efficient way to emit multiple YAML documents.
00:11:50.220 However, streaming JSON works the same way and can be implemented easily.
00:11:57.000 In fact, Twitter uses streaming JSON APIs.
00:12:03.840 Now, I actually did it! I can't believe I'm under 30 minutes. So, are there any questions?
00:12:10.320 I actually have more slides, so if there are no questions, I can continue.
00:12:16.560 Who does your hair?
00:12:23.040 Hi, Mike! Just get closer to the mic.
00:12:27.000 Apparently, I'll repeat the question, so I can hear you. Go ahead.
00:12:32.520 There’s a plea not to use Psych because it makes it difficult to create JRuby libraries, right? I completely disagree with you!
00:12:45.840 It’s essential because even though it may not be compatible today, it will be crucial in the future.
00:12:52.380 The importance of wrapping libyaml is that it's widely used in the community.
00:12:58.860 There are already well-known implementations in other languages, such as Java.
00:13:06.240 I’m working on the Java version for JRuby, so while some limitations exist today, they won’t last.
00:13:12.780 More questions?
00:13:18.780 Hold on, I need to check what other slides I have.
00:13:25.020 Coverage is another new feature in the standard library.
00:13:30.780 I recently blogged about it, but it's essential to know about two methods: start and result.
00:13:37.620 Imagine we have a class that iterates ten times with some comments and an if-statement.
00:13:44.940 The way to measure coverage is simple. We start coverage with 'coverage.start'.
00:13:50.700 After requiring the file, the output will show coverage data in a hash format.
00:13:59.160 A nil value means a line was never executed; a number indicates how many times it was.
00:14:05.880 Using this information, we can deduce which lines had coverage and which didn’t.
00:14:12.720 This provides valuable insight for optimizing our code.
00:14:19.680 I encourage everyone to check out the coverage tool. It works well with Rails.
00:14:26.100 However, be cautious with what I presented, especially concerning JRuby.
00:14:32.640 Some features may not be available on other Ruby implementations.
00:14:38.280 Thank you! Thank you very much!
Explore all talks recorded at GoGaRuCo 2010
+21