3 ms·
Ruby certainly has first-class functions. Not only can you pass "functions" around as lambdas or procs, you can also detach and re-atach methods from a class or
by pselbert 12y ago
Ruby certainly has first-class functions. Not only can you pass "functions" around as lambdas or procs, you can also detach and re-atach methods from a class or object at runtime.
From the wikipedia:
"Specifically, this means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures"
All of these things are certainly possible in ruby, see [0] fizzbuzz in the lambda calculus in ruby
[0]: https://gist.github.com/tomstuart/1466504 https://gist.github.com/tomstuart/1466504
- lucian1900 12y agoSome consider Ruby blocks to not be first-class functions since calling them is different from calling methods. I don't know if I agree or not.
- vidarh 12y ago"Bare" Ruby blocks are called differently from calling methods (yield). But the moment you name a block (pass it to a method with a named block argument) or create one with "proc" or "lambda" or "Proc.new", the form you get access to it in is a Proc object, and calling it is a method call (ob.call). The distinction is quite pointless, other than as an implementation artefact for MRI, as there shouldn't be any obvious observable differences in behaviour between a bare block and a Proc (in my "eternally in progress and not very functional yet" Ruby compiler, all blocks are in fact Proc instances, though if you can tell, it's a bug)
- davexunit 12y agoFrom the same Wikipedia article: "The identifier of a regular "function" in Ruby (which is really a method) cannot be used as a value or passed. It must first be retrieved into a Method or Proc object to be used as first-class data. The syntax for calling such a function object differs from calling regular methods. Nested method definitions do not actually nest the scope." You can certainly make the case that Ruby has first-class functions, but they don't feel very first-class in practice.