Domain Specific Language (DSL)

Summarized using AI

Live coding: Grepping Ruby code like a boss

Jônatas Davi Paganini • September 04, 2020 • online

Summary

In the video "Live coding: Grepping Ruby code like a boss" by Jônatas Davi Paganini, presented at RubyKaigi Takeout 2020, the speaker discusses the challenges of searching Ruby code and introduces a tool called Fast, which simplifies creating complex search patterns using the Abstract Syntax Tree (AST). The talk emphasizes the importance of refining code searching methods to enhance coding practices in Ruby.

Key Points Discussed:

  • Introduction to Fast: Jônatas presents his personal project, Fast, designed to implement node patterns for easier code searching in Ruby.
  • Limitations of Traditional Tools: Popular tools like grep, ag, and rg have limitations when it comes to searching Ruby code, often yielding confusing results, especially when searching for specific content such as method definitions or comments.
  • Advantages of Fast: Fast allows for specific matches with clear syntax-highlighted output, making it easier to traverse the code structure and find relevant nodes, such as method definitions and class structures.
  • Creating Custom Patterns: The presentation covers how to define search patterns within Fast using command line access, showcasing methods like type and children for better search context.
  • Parser Rewriter Functionality: Jônatas explains how Fast can automate refactoring processes through its parser rewriter, making it easy to replace patterns and manage code efficiently.
  • Real-World Example: He shares an amusing example involving a "waiter" library to illustrate how to manage execution times in tests and enhance test coverage through minor adjustments.
  • Automation of Code Integration: Fast simplifies the process of removing methods and searching patterns to identify weaknesses in existing code.
  • Collaboration with Tools: Using existing patterns from community tools like rubocop can facilitate additional refactoring efforts while promoting a collaborative learning environment.
  • Conclusion and Takeaway: The talk concludes by highlighting the need to refine search strategies in Ruby, ultimately aiming to empower developers with tools that enhance coding practices and uncover weak points in code.

Conclusion

Jônatas Davi Paganini provides valuable insights into refining search capabilities in Ruby through the Fast tool, urging developers to leverage sophisticated techniques and automated solutions for better code management.

The overall emphasis on clear, practical examples serves to empower viewers with the knowledge needed to improve their coding workflows.

Live coding: Grepping Ruby code like a boss
Jônatas Davi Paganini • September 04, 2020 • online

Our favorite language allows us to implement the same code in a few different ways. Because of that, it becomes tough to search and find the target code only with regular expressions.
I'd like to present fast, a searching DSL that can help you build complex searches directly in the AST nodes as regexes does for plain strings.
The presentation will be a live coding tour of how to use the tool and create your searching patterns.
I'll also show how to manipulate the code in the target AST nodes, allowing us to refactor the source code in an automated way. I'll share a few funny stories about how I refactored thousands of files in a week.

RubyKaigi 2020 Takeout

00:00:01.680 Hello friends from RubyKaigi Takeout. My name is Jônatas, and today I'm going to give you a short talk about grepping Ruby code like a boss.
00:00:09.640 So, here is the Fast project, which is my personal project that implements node patterns. Today, I'm going to show you how to build these complex node patterns and how to benefit from using more advanced tools for Ruby.
00:00:15.840 I am a big fan of tools like grep, ag (the Silver Searcher), and rg (ripgrep). We have several tools for building searches, but they can be quite hard to get used to, particularly because they may have issues with generic searches.
00:00:22.080 For example, let me show you how I can see how much I have in my library. I can see that I have several results, and I also see comments, method calls, method definitions, and so on. This can get very confusing when searching, especially if you're trying to find something specific.
00:00:36.080 If you use some more advanced ways, you can use options like -n to see line numbers. However, sometimes they don’t show you exactly what you want to find. For instance, I can use -A and -B options to show more context around my search, but it often still lacks clarity and can be hard to navigate.
00:00:49.680 Using Fast, for example, I can do specific matches with clear output. I need to specify the kind of node I'm searching for. If I want to find a method definition, Fast will show me syntax-highlighted results and the entire method code, which allows for a much better understanding of the code.
00:01:02.720 Let’s say I want to understand what classes contain a specific pattern. I can use 'self' to indicate the parent context, which will show me all the classes that match. This is a powerful feature allowing me to traverse and examine the structure of my code effectively.
00:01:12.360 Now, I would like to demonstrate how to build powerful searches and how to create your custom patterns. Let's start with the fundamentals of parsing something.
00:01:26.239 For instance, I can define a method match that contains a specific node with some logic inside. In Ruby, parsing will show us this small expression, which is what we see as the Abstract Syntax Tree (AST) representation. Let's dive into this further using command line access.
00:01:36.640 While using my Fast project, I can load it through the bin command, which gives me access to the methods in the library. I can check the AST class and see it works with the parser gem as well. This boosts my ability to traverse the tree from child to parent.
00:01:53.519 We have some basic methods like type and children. If I examine a simple node, I can confirm it’s a basic type that holds a single value. For example, I could demonstrate how floating-point numbers work and show how that can be searched in the context of some variable assignments.
00:02:09.919 If I search for float or integer values, I can capture their specifics. This demonstrates the simplicity of leveraging node patterns within Fast while getting access to details effortlessly, such as checking for methods or variable assignments, which enhance our search capabilities.
00:02:39.200 Now, I will show you the parser rewriter functionality, which allows us to make replacements and automate refactoring processes. When you have code, you also have a location for your expressions, making edits straightforward.
00:03:11.840 I have a simple example where I can define a constant receiving an integer. Using location methods, we can gain insights about everything happening within our nodes, including operator presence, which is useful when trying to create references to code blocks.
00:03:42.239 Let’s analyze another practical example called 'waiter', a rather amusing library that reminds us of how we can manage delays within our code testing. The waiter method is designed to manage execution time, allowing us to set up specs that validate the use cases with some fun twists.
00:04:41.359 For instance, if I run the waiter specs, I'll find that they pass successfully. However, I can also challenge this by creating scenarios where unnecessary elements are stripped to see its robustness. These are just examples of how slight adjustments can lead to significant improvements in our test coverage.
00:05:22.799 I'd also like to demonstrate how to automate and enhance code integration with specific patterns like method removal. By employing Fast, I can define shortcuts for search patterns that assist in creating effective solutions and uncovering potential weaknesses within existing code.
00:06:43.440 In addition to that, I want to highlight how using existing patterns from tools such as rubocop can aid in refactoring exercises. This is a collaborative approach that not only allows automation but also fosters an environment for learning.
00:07:12.239 To give you one final example, I had a case from production involving a bug that showcased the need to validate parameters for method calls. Each parameter's significance is crucial to affirming the method's execution and keeping the code robust and error-free.
00:07:37.839 The goal here is to refine our search strategies, allowing us to group code more efficiently and uncover any weak points. This culminates in utilizing specific commands to identify unsatisfactory patterns and verify required parameters.
00:08:01.679 I sincerely thank you for taking the time to watch my presentation, and I hope you found it insightful. My aim was to empower you with knowledge and tools that may enhance your coding practices in Ruby.
00:08:25.599 Enjoy coding and see you around!
Explore all talks recorded at RubyKaigi 2020 Takeout
+17