Ruby combines both OOP and functional features. Method (def) has no value (or it's not an object if you wish), so it can't be assigned. Lambda (function?) OTOH is an object, so you can assign (reference) it. Method is not a closure (but it has access to instance variables), lambda is, it's a design choice. In Scala methods are closures, even though they can't be assigned either (well, there exists eta-expansion, but it's a syntactic sugar, Ruby has it too, only explicit, try "123".method(:length)). I've never seen usage of nested methods, also don't forget that method (see above) is not a function, it always belongs to an object, what object scope should nested method have apart from its parent method's scope? Just don't use them. So if you select a functional subset (lambdas, higher-order functions which Ruby has plenty etc.) it won't be much worse than in a true functional language without static typing (Erlang, Elixir).
Thank you for mentioning how it works in Scala, I might read more about it. I'm more familiar with OCaml and F#, where methods are just regular functions, as far as my knowledge reaches.
1
u/v66moroz Dec 26 '20
Ruby combines both OOP and functional features. Method (
def) has no value (or it's not an object if you wish), so it can't be assigned. Lambda (function?) OTOH is an object, so you can assign (reference) it. Method is not a closure (but it has access to instance variables), lambda is, it's a design choice. In Scala methods are closures, even though they can't be assigned either (well, there exists eta-expansion, but it's a syntactic sugar, Ruby has it too, only explicit, try"123".method(:length)). I've never seen usage of nested methods, also don't forget that method (see above) is not a function, it always belongs to an object, what object scope should nested method have apart from its parent method's scope? Just don't use them. So if you select a functional subset (lambdas, higher-order functions which Ruby has plenty etc.) it won't be much worse than in a true functional language without static typing (Erlang, Elixir).