Project

General

Profile

Feature #16799

Updated by jackmaple (maple jack) about 4 years ago

Classes are currently first-class citizens in Ruby, but functions are not. 
 This leads to a situation where the function calls are not uniform, for example: 
 ``` ruby 
 def boo(x) 
   return x 
 end 
 boo2 = -> x { x } 
 a = boo 10 
 b = boo2.call(10) 
 or 
 b = boo2.(10) 
 or 
 b = boo2[10] 
 ``` 
 This is very confusing to people from other languages (JavaScript, Python, etc.), or people who are just starting to learn ruby. 
 If the method calling format can be unified, and the method is also a first-class citizen, then a higher-order function can be realized, and the method name is just a variable,so maybe more things can be done,and it might be better if the lambda is changed to the following form,for example(suppose): 
 ``` ruby 
 method1 = (f,n) -> f(n) 
 or 
 method1 = (f,n) -> { 
     return f(n) 
 } 
 or 
 method1 = (f,n) -> do 
     return f(n) 
 end 
 #call method 
 result = method1(x -> x * 2,10)    #result = 20 
 ``` 
 The same can also be achieved as follows: 
 ``` ruby 
 def method1(f,n) 
     return f(n) 
 end 

 def double(x) 
     return x * 2 
 end 
 # &double is reference double method. 
 result = method1(&double,10)       #result = 20 
 ``` 
 We can also use closures in methods(suppose): 
 ``` ruby 
 def method1() 
     param = [] 
     def method2(x) 
         param.push(x) 
         return param 
     end 
     return method2 
 end 
 f = method1 
 f(10) 
 ``` 
 Or you can implement decorators: 
 ``` ruby 
 def decorator(f) 
     def child_method(*param) 
         puts(param) 
         return f(*param) 
     end 
     return child_method 
 end 

 def method1(x) 
     return x 
 end 

 method1 = decorator(&method1) 
 method1(10) 
 ``` 
 Although the syntax change is not good, but it makes ruby easier to use. 
 These are just some of my thoughts, thanks.

Back