{"id":5801,"date":"2013-10-09T18:34:34","date_gmt":"2013-10-09T15:34:34","guid":{"rendered":"http:\/\/railsware.com\/blog\/?p=5801"},"modified":"2021-08-11T17:51:35","modified_gmt":"2021-08-11T14:51:35","slug":"better-ruby-choosing-convention-for-class-methods-definition","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/","title":{"rendered":"Better Ruby: choosing convention for class methods definition"},"content":{"rendered":"\n<div class=\"intro-text\">Ruby doesn&#8217;t have procedural module akin to Python or Perl module. Usually, one uses class methods as a typical way to mimic this functionality.<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Class methods in Ruby are also very handy for supporting instance methods with fabrics and other common-for-class functionality. This is clearly seen in ActiveRecord::Base with dozens of supportive class methods (e.g. #find, #create, #where).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are two standard approaches for defining class method in Ruby. The first one is the &#8220;def self.method&#8221; (let&#8217;s call it Style #1), and the second one is the &#8220;class &lt;&lt; self&#8221; (let&#8217;s call it Style #2). Both of them have pros and cons. So, let&#8217;s take a look at them and try to decide which style is better and why.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code sample<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here is &#8220;def self.method&#8221; style:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Style #1\nclass User &lt; ActiveRecord::Base\n  def rating\n  end\n\n  def self.most_popular\n  end\n\n  def title_with_rating\n  end\n\n  def self.the_king\n  end\nend\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And the same example for &#8220;class &lt;&lt; self&#8221; style:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Style #2\nclass User &lt; ActiveRecord::Base\n  class &lt;&lt; self\n    def most_popular\n    end\n\n    def the_king\n    end\n  end\n\n  def rating\n  end\n\n  def title_with_rating\n  end\nend\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Pros of &#8220;def self.method&#8221; style<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. It&#8217;s shorter and easier to add one method<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s obvious, that Style #1 is two lines shorter due to the &#8220;class &lt;&lt; self; end&#8221; part. This difference is noticeable for one\/two methods. Also, it&#8217;s easier to add first class method because you don&#8217;t have to type &#8220;class &lt;&lt; self&#8221; wrapper.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. More simple for newbies<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Usually Style #2 looks more exotic than Style #1; and, at first, the &#8220;class &lt;&lt; self&#8221; part looks a little bit awkward.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pros of &#8220;class &lt;&lt; self&#8221; style<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Class and instance methods are separated<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Glancing over class definition, in Style #1 it&#8217;s hard to say, whether it&#8217;s a class or instance method. You need to focus on name part. On the other side, in Style #2 indentation and &#8220;class &lt;&lt; self&#8221; wrapper clearly says who is who.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another benefit of Style #2 &#8211; it&#8217;s easy to see amount of class and instance methods. They are not intermixed. Without clear separation it&#8217;s harder to work with class &#8211; you need to go over mix of methods and look for the required method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Of course, you can take convention, for grouping class methods in one place, but it&#8217;s another rule, which should be followed by all, and it&#8217;s so easy to violate it occasionally. With Style #2 is much harder to accidentally create another &#8220;class &lt;&lt; self&#8221; scope.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Easier to add batch of methods<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">What I especially appreciate in Style #2 &#8211; it&#8217;s easiness of adding new class methods. While Writing a new method, you shouldn&#8217;t be focused on adding &#8220;self.&#8221;-part in name. Class and instance method definition have the same syntax.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When I started my way in Ruby, it frustrated me a lot when I added class method and it didn&#8217;t work. And only then I realised that I actually forgot to add &#8220;self.&#8221;-prefix.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I strongly believe that a programmer should focus more on what he does and less on various language quirks. And after I switched to Style #2, these kinds of errors just disappeared from everyday coding.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Simpler search of method definition<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">&#8220;grep&#8221;-ing for some method is simpler to always search by &#8220;def name&#8221; combination. Style #1 also requires search by &#8220;def self.name&#8221;. This item is not a showstopper, but I run into this issue from time to time while searching through a code of different gems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Simpler refactoring<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">What is started as object-oriented class can turn out to be a procedural module with a bunch of functions. To refactor instance methods into class methods you just need to add &#8220;class &lt;&lt; self&#8221; wrapper and adjust indentation, which can be easily done in any editor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the other side, Style #1 requires to put &#8220;self.&#8221;-prefix in every method, which is tedious and can&#8217;t be archived with usual means.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, there is a case when you go into the opposite direction. Thing, which started as procedural module use some common context and should be refactored into class with instance methods. And again Style #1 requires removing of &#8220;self.&#8221;-prefix from all names, when in Style #2 you just remove &#8220;class &lt;&lt; self \/ end&#8221; wrapper and adjust indentation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s clearly seen, that &#8220;class &lt;&lt; self&#8221; style has much more benefits than more commonly used &#8220;def self.method&#8221;. For me, the most crucial part is clear separation between class and instance stuff &#8211; because it improves code readability. I also like that Style #2 removes one class of errors from my Ruby programming.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Generally, we stick to the rules below while using it:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Create &#8220;class &lt;&lt; self&#8221; wrapper even for one method<\/li><li>Put class-methods part at the top of the class<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Following these rules will allow you to always find class-methods in a predictable place and in a predictable style.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ruby doesn&#8217;t have procedural module akin to Python or Perl module. Usually, one uses class methods as a typical way to mimic this functionality. Class methods in Ruby are also very handy for supporting instance methods with fabrics and other common-for-class functionality. This is clearly seen in ActiveRecord::Base with dozens of supportive class methods (e.g&#8230;.<\/p>\n","protected":false},"author":25,"featured_media":9441,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Sergii Boiko"],"class_list":["post-5801","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"Ruby doesn&#039;t have procedural module akin to Python or Perl module. Usually, one uses class methods as a typical way to mimic this functionality. Class methods in Ruby are also very handy for supporting instance methods with fabrics and other common-for-class functionality. This is clearly seen in ActiveRecord::Base with dozens of supportive class methods (e.g.\" \/>\n\t<meta name=\"robots\" content=\"max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n\t<meta name=\"author\" content=\"Sergii Boiko\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"| Blog on Engineering, Product Management, Transparency, Culture and many more...\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Defining class methods in Ruby - choosing better style | Railsware Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Ruby doesn&#039;t have procedural module akin to Python or Perl module. Usually, one uses class methods as a typical way to mimic this functionality. Class methods in Ruby are also very handy for supporting instance methods with fabrics and other common-for-class functionality. This is clearly seen in ActiveRecord::Base with dozens of supportive class methods (e.g.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2017\/12\/Better-Ruby.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2017\/12\/Better-Ruby.png\" \/>\n\t\t<meta property=\"og:image:width\" content=\"360\" \/>\n\t\t<meta property=\"og:image:height\" content=\"360\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2013-10-09T15:34:34+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2021-08-11T14:51:35+00:00\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#article\",\"name\":\"Defining class methods in Ruby - choosing better style | Railsware Blog\",\"headline\":\"Better Ruby: choosing convention for class methods definition\",\"author\":{\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/author\\\/sergii-boiko\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/railsware.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/Better-Ruby.png\",\"width\":360,\"height\":360,\"caption\":\"Ruby: choosing convention for class methods definition\"},\"datePublished\":\"2013-10-09T18:34:34+03:00\",\"dateModified\":\"2021-08-11T17:51:35+03:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#webpage\"},\"articleSection\":\"Engineering\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/railsware.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/category\\\/development\\\/#listItem\",\"name\":\"Engineering\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/category\\\/development\\\/#listItem\",\"position\":2,\"name\":\"Engineering\",\"item\":\"https:\\\/\\\/railsware.com\\\/blog\\\/category\\\/development\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#listItem\",\"name\":\"Better Ruby: choosing convention for class methods definition\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#listItem\",\"position\":3,\"name\":\"Better Ruby: choosing convention for class methods definition\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/category\\\/development\\\/#listItem\",\"name\":\"Engineering\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/#organization\",\"description\":\"Blog on Engineering, Product Management, Transparency, Culture and many more...\",\"url\":\"https:\\\/\\\/railsware.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/railsware.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/Logo-circle.png\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#organizationLogo\",\"width\":3137,\"height\":1054,\"caption\":\"Railsware logo\"},\"image\":{\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#organizationLogo\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/author\\\/sergii-boiko\\\/#author\",\"url\":\"https:\\\/\\\/railsware.com\\\/blog\\\/author\\\/sergii-boiko\\\/\",\"name\":\"Sergii Boiko\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#authorImage\",\"url\":\"https:\\\/\\\/railsware.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/06\\\/sergii-boiko-96x96.jpg\",\"width\":96,\"height\":96,\"caption\":\"Sergii Boiko\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#webpage\",\"url\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/\",\"name\":\"Defining class methods in Ruby - choosing better style | Railsware Blog\",\"description\":\"Ruby doesn't have procedural module akin to Python or Perl module. Usually, one uses class methods as a typical way to mimic this functionality. Class methods in Ruby are also very handy for supporting instance methods with fabrics and other common-for-class functionality. This is clearly seen in ActiveRecord::Base with dozens of supportive class methods (e.g.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/author\\\/sergii-boiko\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/author\\\/sergii-boiko\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/railsware.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/Better-Ruby.png\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#mainImage\",\"width\":360,\"height\":360,\"caption\":\"Ruby: choosing convention for class methods definition\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/better-ruby-choosing-convention-for-class-methods-definition\\\/#mainImage\"},\"datePublished\":\"2013-10-09T18:34:34+03:00\",\"dateModified\":\"2021-08-11T17:51:35+03:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/railsware.com\\\/blog\\\/\",\"description\":\"Blog on Engineering, Product Management, Transparency, Culture and many more...\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/railsware.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Defining class methods in Ruby - choosing better style | Railsware Blog","description":"Ruby doesn't have procedural module akin to Python or Perl module. Usually, one uses class methods as a typical way to mimic this functionality. Class methods in Ruby are also very handy for supporting instance methods with fabrics and other common-for-class functionality. This is clearly seen in ActiveRecord::Base with dozens of supportive class methods (e.g.","canonical_url":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/","robots":"max-snippet:-1, max-image-preview:large, max-video-preview:-1","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#article","name":"Defining class methods in Ruby - choosing better style | Railsware Blog","headline":"Better Ruby: choosing convention for class methods definition","author":{"@id":"https:\/\/railsware.com\/blog\/author\/sergii-boiko\/#author"},"publisher":{"@id":"https:\/\/railsware.com\/blog\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2017\/12\/Better-Ruby.png","width":360,"height":360,"caption":"Ruby: choosing convention for class methods definition"},"datePublished":"2013-10-09T18:34:34+03:00","dateModified":"2021-08-11T17:51:35+03:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#webpage"},"isPartOf":{"@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#webpage"},"articleSection":"Engineering"},{"@type":"BreadcrumbList","@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/railsware.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/railsware.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/railsware.com\/blog\/category\/development\/#listItem","name":"Engineering"}},{"@type":"ListItem","@id":"https:\/\/railsware.com\/blog\/category\/development\/#listItem","position":2,"name":"Engineering","item":"https:\/\/railsware.com\/blog\/category\/development\/","nextItem":{"@type":"ListItem","@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#listItem","name":"Better Ruby: choosing convention for class methods definition"},"previousItem":{"@type":"ListItem","@id":"https:\/\/railsware.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#listItem","position":3,"name":"Better Ruby: choosing convention for class methods definition","previousItem":{"@type":"ListItem","@id":"https:\/\/railsware.com\/blog\/category\/development\/#listItem","name":"Engineering"}}]},{"@type":"Organization","@id":"https:\/\/railsware.com\/blog\/#organization","description":"Blog on Engineering, Product Management, Transparency, Culture and many more...","url":"https:\/\/railsware.com\/blog\/","logo":{"@type":"ImageObject","url":"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2020\/11\/Logo-circle.png","@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#organizationLogo","width":3137,"height":1054,"caption":"Railsware logo"},"image":{"@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#organizationLogo"}},{"@type":"Person","@id":"https:\/\/railsware.com\/blog\/author\/sergii-boiko\/#author","url":"https:\/\/railsware.com\/blog\/author\/sergii-boiko\/","name":"Sergii Boiko","image":{"@type":"ImageObject","@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#authorImage","url":"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2021\/06\/sergii-boiko-96x96.jpg","width":96,"height":96,"caption":"Sergii Boiko"}},{"@type":"WebPage","@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#webpage","url":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/","name":"Defining class methods in Ruby - choosing better style | Railsware Blog","description":"Ruby doesn't have procedural module akin to Python or Perl module. Usually, one uses class methods as a typical way to mimic this functionality. Class methods in Ruby are also very handy for supporting instance methods with fabrics and other common-for-class functionality. This is clearly seen in ActiveRecord::Base with dozens of supportive class methods (e.g.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/railsware.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#breadcrumblist"},"author":{"@id":"https:\/\/railsware.com\/blog\/author\/sergii-boiko\/#author"},"creator":{"@id":"https:\/\/railsware.com\/blog\/author\/sergii-boiko\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2017\/12\/Better-Ruby.png","@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#mainImage","width":360,"height":360,"caption":"Ruby: choosing convention for class methods definition"},"primaryImageOfPage":{"@id":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/#mainImage"},"datePublished":"2013-10-09T18:34:34+03:00","dateModified":"2021-08-11T17:51:35+03:00"},{"@type":"WebSite","@id":"https:\/\/railsware.com\/blog\/#website","url":"https:\/\/railsware.com\/blog\/","description":"Blog on Engineering, Product Management, Transparency, Culture and many more...","inLanguage":"en-US","publisher":{"@id":"https:\/\/railsware.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"| Blog on Engineering, Product Management, Transparency, Culture and many more...","og:type":"article","og:title":"Defining class methods in Ruby - choosing better style | Railsware Blog","og:description":"Ruby doesn't have procedural module akin to Python or Perl module. Usually, one uses class methods as a typical way to mimic this functionality. Class methods in Ruby are also very handy for supporting instance methods with fabrics and other common-for-class functionality. This is clearly seen in ActiveRecord::Base with dozens of supportive class methods (e.g.","og:url":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/","og:image":"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2017\/12\/Better-Ruby.png","og:image:secure_url":"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2017\/12\/Better-Ruby.png","og:image:width":360,"og:image:height":360,"article:published_time":"2013-10-09T15:34:34+00:00","article:modified_time":"2021-08-11T14:51:35+00:00"},"aioseo_meta_data":{"post_id":"5801","title":"Defining class methods in Ruby - choosing better style | Railsware Blog","description":null,"keywords":[],"keyphrases":{"focus":[],"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":{"id":"aioseo-article-6385f5b182e82","slug":"article","graphName":"Article","label":"Article","properties":{"type":"BlogPosting","name":"#post_title","headline":"#post_title","description":"#post_excerpt","image":"","keywords":"","author":{"name":"#author_name","url":"#author_url"},"dates":{"include":true,"datePublished":"","dateModified":""}}},"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":"{\"article\":{\"articleType\":\"BlogPosting\"},\"course\":{\"name\":\"\",\"description\":\"\",\"provider\":\"\"},\"faq\":{\"pages\":[]},\"product\":{\"reviews\":[]},\"recipe\":{\"ingredients\":[],\"instructions\":[],\"keywords\":[]},\"software\":{\"reviews\":[],\"operatingSystems\":[]},\"webPage\":{\"webPageType\":\"WebPage\"}}","pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":{"locations":{"business":{"name":"","businessType":"","image":"","areaServed":"","urls":{"website":"","aboutPage":"","contactPage":""},"address":{"streetLine1":"","streetLine2":"","zipCode":"","city":"","state":"","country":"","addressFormat":"#streetLineOne\n#streetLineTwo\n#city, #state #zipCode"},"contact":{"email":"","phone":"","phoneFormatted":"","fax":"","faxFormatted":""},"ids":{"vat":"","tax":"","chamberOfCommerce":""},"payment":{"priceRange":"","currenciesAccepted":"","methods":""}}},"openingHours":{"useDefaults":true,"show":true,"alwaysOpen":false,"use24hFormat":false,"timezone":"","labels":{"closed":"","alwaysOpen":""},"days":{"monday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"},"tuesday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"},"wednesday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"},"thursday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"},"friday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"},"saturday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"},"sunday":{"open24h":false,"closed":false,"openTime":"09:00","closeTime":"17:00"}}}},"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-01-04 12:44:29","updated":"2025-09-26 11:19:39","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/railsware.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/railsware.com\/blog\/category\/development\/\" title=\"Engineering\">Engineering<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tBetter Ruby: choosing convention for class methods definition\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/railsware.com\/blog"},{"label":"Engineering","link":"https:\/\/railsware.com\/blog\/category\/development\/"},{"label":"Better Ruby: choosing convention for class methods definition","link":"https:\/\/railsware.com\/blog\/better-ruby-choosing-convention-for-class-methods-definition\/"}],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/railsware.com\/blog\/wp-content\/uploads\/2017\/12\/Better-Ruby.png","article_background":"","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/5801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=5801"}],"version-history":[{"count":28,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/5801\/revisions"}],"predecessor-version":[{"id":13925,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/5801\/revisions\/13925"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/9441"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=5801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=5801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=5801"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=5801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}