-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpry.html
More file actions
173 lines (129 loc) · 8.49 KB
/
pry.html
File metadata and controls
173 lines (129 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>How to Pry - Jennifer Kruse</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/clean-blog.min.css" rel="stylesheet">
<link href="css/application.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href='http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header page-scroll">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li>
<a href="http://jenniferkruse.me">Home</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Page Header -->
<!-- Set your background image for this header on the line below. -->
<header class="intro-header" style="background-image: url('img/pry/header.jpg')">
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="post-heading">
<h1 style="color: white">How to Pry</h1>
<h2 class="subheading" style="color: white">How to use Pry, the best Ruby debugger.</h2>
<span class="meta">December 19, 2015</span>
</div>
</div>
</div>
</div>
</header>
<!-- Post Content -->
<article>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<p></p>
<p>Debugging can be difficult, especially if you don't have the right tools. One of the most common ways of debugging is to insert print statements everywhere, run your program, and read the output to try to figure out the problem. You may have to run the program multiple times, changing your print statements each time to print out different variables or trying different functions. Oftentimes there are so many other logs printed out that it's difficult to even find your print statement's output in the first place. </p>
<p>Although this method of debugging works, it's very inefficient. There is a much easier way to debug while programming in Ruby: the <a href="http://pryrepl.org/">Pry</a> gem. First, make sure you have pry installed (type <b>gem install pry</b> into your console). </p>
<p>To demonstrate how to use Pry, I've created a very simple test program written in Ruby. </p>
<img class="blogImg" src="img/pry/screenshot1.png">
<p>If I want to debug this using print statements, I would insert a bunch of "puts" with some helpful information. It might look something like this: </p>
<img class="blogImg" src="img/pry/screenshot2.png">
<p>Then I would run the program and look at the output. It would look something like the screenshot below. Depending on the application, there might also be a ton of logs you need to sort through to even find the print statements. </p>
<img class="blogImg" src="img/pry/screenshot3.png">
<p>As you can see, this makes the program pretty messy, and although it's possible, sorting through the print statements is tedious and time consuming. Instead, we can use Pry. </p>
<p>First, we'll start with one Pry statement. Where you wish to debug your program, insert <b>require 'pry'; binding.pry</b>. </p>
<img class="blogImg" src="img/pry/screenshot4.png">
<p>Make sure you insert the statement in a line of code that the program runs over. Then run the program. When the program hits that line of code, it will stop, and look something like this: </p>
<img class="blogImg" src="img/pry/screenshot5.png">
<p>The program is pausing at that line of code and now you can type things into the command line in order to debug. The little arrow on the left shows you where the program is stopped. You can type a variable to see its value, or even call a function on the variable to see its result. </p>
<img class="blogImg" src="img/pry/screenshot6.png">
<p>To continue execution, press <b>Ctrl + D</b>. To stop execution and exit the program, press <b>Ctrl + C</b>.</p>
<p>Pry is even more useful when you insert multiple pry statements into your program. In this example, I'll insert a Pry statement at the beginning of the test function, and also one inside the loop. </p>
<img class="blogImg" src="img/pry/screenshot7.png">
<p>Now when we run our program, it will stop at the first Pry statement. If we wish to continue debugging and check out the area of code near the second Pry statement, then we can press <b>Ctrl + D</b> and then debug again. </p>
<img class="blogImg" src="img/pry/screenshot8.png">
<p>These are just the simplest Pry commands that I use the most often. There are many more uses for Pry detailed in its documentation that might come in handy for you. Using Pry instead of print statements for debugging has definitely made my life much simpler! </p>
<p class="contactText"> Questions? Comments? Don't hesitate to <a href="http://jenniferkruse.me/about">contact me</a>!</p>
<!--
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'jenniferkruse'; // required: replace example with your forum shortname
var disqus_identifier = 'pry';
var disqus_url = 'http://jenniferkruse.me/blog/pry.html';
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> -->
</div>
</div>
</div>
</article>
<hr>
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="js/clean-blog.min.js"></script>
</body>
</html>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-59909033-1', 'auto');
ga('send', 'pageview');
</script>