Project

General

Profile

Feature #11167

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

Hi guys, 

 Hi nobu :) 

 Also hi matz if matz reads this, and of course the rest of the core 
 team and everyone else. 

 Today on IRC, this mini-discussion happened (I show a snippet): 

 ~~~ 
 <apeiros> I really miss attr_query or whatever you want to name it 
 <apeiros> which would generate a ? method too 
 <jhass> apeiros: crystal has :P getter? 
 <apeiros> nice 
 ~~~ 

 Ok, so the language crystal has something ruby does not have. 

 We can't let those newcomers get away with making ruby look old 
 now can we! 

 I use ruby not crystal but I very often use methods that end  
 with a '?' query mark in ruby. It helps me in simple if clauses 
 such as: 

 ~~~ruby 
 

   if hash.has_key? 
 
   if hash.key? 
 
   if cat.is_hungry? 
 ~~~ 

 (In the latter, it might be a cat of class `Cat` Cat instance, with 
 an instance variable called `@is_hungry`, @is_hungry, and when the cat is  
 fed with food, it is not hungry logically.) 

 We can generate these `@ivars` @ivars through `attr_`* attr_* right now as is 
 already, such as: 

 ~~~ruby 
 


     attr_reader :foo 
 
     def foo; @foo; end 

 

     attr_writer :foo 
 
     def foo=(i); @foo = i; end 

 

     attr_accessor :foo 
 
     ^^^ Combines the above two methods into one. 
 ~~~ 

 But we have no way to designate methods that end via '?'. 

 I do not know which API call would be nice. apeiros on 
 IRC suggested    `attr_query` attr_query 

 I am fine with that. (The name is secondary for me, I 
 would like to have this feature available - what name  
 it would then have is not the main issue for me.) 

 apeiros then also suggested this syntax: 


 All `attr_`* attr_* that would end with a `?` ? token, would be a  
 combination of `attr_reader` attr_reader and also a variant of the 
 above that has a '?' token, so for example: 

 ~~~ruby 
 attr_reader :foo? 
 ~~~ 

 Would create both a method `foo()` foo() and `foo?()`. foo?(). 

 People who do not need this, can continue to use: 

 ~~~ruby 
 attr_reader :foo 
 ~~~ 

 just fine. 

 So perhaps this suggestion is even better than 
 a new method (such as through `attr_query()`) attr_query()) 

 (I also have added one more line from apeiros, 
 not sure if I understood it, but I think the 
 above explanation should suffice - here is the 
 other suggestion he did:) 

 ~~~ 
 apeiros> e.g. attr_reader :foo? -> foo? // attr_accessor :foo? -> foo= + foo? // all with @foo of course. and foo? returning true/false. 
 ~~~ 

 Ok, that's it. 

 Thanks for reading! 

Back