Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby syntax '{:age.gt => 60}', how does it work?

Tags:

ruby

mongoid

I am wondering this syntax {:age.gt => 60}, which I find this syntax from mongoid. How does it work? I'm checking the source code of mongoid, it didn't extend the Symbol class much, and there is a matchers directory which contains gt.rb, lt.rb, .... These files are used for conditioning logic: greater than, less than .... I can't understand how it relates to Symbol class.

In plain irb session, it displays an error:

NoMethodError: undefined method `gt' for :age:Symbol

Anyone could explain me?

like image 931
Chamnap Avatar asked Aug 20 '26 10:08

Chamnap


1 Answers

This is done by mongoid itself as part of Mongoid::Criterion::Complex in this symbol inflections file. Oddly this file is no longer there in the master branch, but I'm using 2.4.7 in my project and it exists there (it probably just moved but I can't find it in master).

Pretty interesting bit of magic. Magic I wouldn't consider all that necessary, :field => { :$gt => 5 } looks just fine to me, but anyway :)

like image 174
rfunduk Avatar answered Aug 24 '26 22:08

rfunduk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!