Is Ruby a functional language?

前端 未结 12 1474
谎友^
谎友^ 2021-01-30 04:09

Wikipedia says Ruby is a functional language, but I\'m not convinced. Why or why not?

12条回答
  •  囚心锁ツ
    2021-01-30 04:32

    Ruby does support higher-level functions (see Array#map, inject, & select), but it is still an imperative, Object-Oriented language.

    One of the key characteristics of a functional language it that it avoids mutable state. Functional languages do not have the concept of a variable as you would have in Ruby, C, Java, or any other imperative language.

    Another key characteristic of a functional language is that it focuses on defining a program in terms of "what", rather than "how". When programming in an OO language, we write classes & methods to hide the implementation (the "how") from the "what" (the class/method name), but in the end these methods are still written using a sequence of statements. In a functional language, you do not specify a sequence of execution, even at the lowest level.

提交回复
热议问题