{"id":555,"date":"2009-12-30T11:10:55","date_gmt":"2009-12-30T13:10:55","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=555"},"modified":"2010-01-08T05:54:36","modified_gmt":"2010-01-08T07:54:36","slug":"def-til-end","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2009\/12\/def-til-end\/","title":{"rendered":"def ~; end"},"content":{"rendered":"

Pratik<\/a> lately is doing a great work refactoring ActiveRecord to make a full use of relations<\/a>. Speaking in code language, this means that in Rails 3 you will be able to do:<\/p>\n

\r\n  User.where(:name => \"Jose\").order(\"birthday\")\r\n<\/pre>\n

And it will return a relation<\/a>. The relation can be further manipulated and the query is only triggered when you call all<\/code>, first<\/code>, to_a<\/code> or some Enumerator<\/code> method.<\/p>\n

Besides that, he’s also doing some crazy experiments<\/a>, which will probably become a plugin later. While discussing with Pratik some ways to implement equality and inequality, I discovered a neat ruby trick. Open up an irb session and do:<\/p>\n

MD7M9TSHSAJY<\/div>\n
\r\n~2  #=> -3\r\n~42 #=> -43\r\n<\/pre>\n

And this method is actually documented<\/a>.<\/p>\n

The nice thing though, is that you can define this method in other classes as well. In the querying scenario for example, we could add this behavior:<\/p>\n

\r\nclass Symbol\r\n  def ~\r\n    :\"LOWER(#{self})\"\r\n  end\r\nend\r\n<\/pre>\n

And now we could actually do:<\/p>\n

\r\nUser.where(~:email => \"jose@plataformatec.com.br\")\r\n#=> SELECT * WHERE LOWER(email) = \"jose@plataformatec.com.br\"\r\n<\/pre>\n

Unary operators<\/h3>\n

Yehuda Katz<\/a> later pointed to me that this can actually be done with any of the three unary operators in Ruby: +, – and ~.<\/p>\n

So we could also do:<\/p>\n

\r\nclass String\r\n  def +@\r\n    upcase\r\n  end\r\n\r\n  def -@\r\n    downcase\r\n  end\r\nend\r\n<\/pre>\n

And now:<\/p>\n

\r\n+\"jose\" #=> \"JOSE\"\r\n-\"JOSE\" #=> \"jose\"\r\n<\/pre>\n

You could even “go crazy” and append several unary operators:<\/p>\n

\r\n+-+\"jose\" #=> \"JOSE\"\r\n<\/pre>\n

And you? Did you know such behavior in Ruby? Do you have any good use case for it?<\/p>\n","protected":false},"excerpt":{"rendered":"

Pratik lately is doing a great work refactoring ActiveRecord to make a full use of relations. Speaking in code language, this means that in Rails 3 you will be able to do: User.where(:name => “Jose”).order(“birthday”) And it will return a relation. The relation can be further manipulated and the query is only triggered when you … \u00bb<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[62,60,61],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/555"}],"collection":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=555"}],"version-history":[{"count":7,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/555\/revisions"}],"predecessor-version":[{"id":565,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/555\/revisions\/565"}],"wp:attachment":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}