{"id":7,"date":"2014-06-03T16:45:44","date_gmt":"2014-06-03T16:45:44","guid":{"rendered":"https:\/\/www.lianeallen.com\/home\/?p=7"},"modified":"2014-06-06T16:51:15","modified_gmt":"2014-06-06T16:51:15","slug":"predicate-and-bang-methods","status":"publish","type":"post","link":"https:\/\/www.lianeallen.com\/home\/2014\/06\/predicate-and-bang-methods\/","title":{"rendered":"Predicate and Bang Methods"},"content":{"rendered":"<p>Ruby allows you to use non-alphanumeric characters in method names. There are a couple that are used fairly commonly: ? and !<\/p>\n<p>You put the character at the end of the method name.<\/p>\n<h2>Predicate<\/h2>\n<p>? indicates a &#8220;predicate&#8221; method. This is a method that returns true or false. So, for example, if you want to know if the result of a particular operation is true or false, you can create a method that does that operation, then returns true or false based on the result of the operation.<\/p>\n<p>For example, if you want to know if the player won a game, you could make a method called &#8220;won?&#8221;<\/p>\n<p>Then use it like:<\/p>\n<blockquote><p>if won?<br \/>\n&nbsp;&nbsp;&nbsp;puts &#8220;Congratulations, you won!&#8221;<br \/>\nelse<br \/>\n&nbsp;&nbsp;&nbsp;puts &#8220;Sorry, you lost.&#8221;\n<\/p><\/blockquote>\n<p>It&#8217;s a great way to make code that almost reads like English.<\/p>\n<h2>Bang<\/h2>\n<p>A bang method tells you that this method does something &#8220;dangerous&#8221; (changes a value, dun, dun, duu-uu-un).<\/p>\n<blockquote><p>def turns_remaining!<br \/>\n&nbsp;&nbsp;&nbsp;@turns_remaining -= 1  # decrements variable<br \/>\nend<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Ruby allows you to use non-alphanumeric characters in method names. There are a couple that are used fairly commonly: ? and ! You put the character at the end of the method name. Predicate ? indicates a &#8220;predicate&#8221; method. This is a method that returns true or false. So, for example, if you want to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.lianeallen.com\/home\/2014\/06\/predicate-and-bang-methods\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Predicate and Bang Methods&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":10,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":17,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/posts\/7\/revisions\/17"}],"wp:attachment":[{"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}