こんにちは、chibaです! 私はCommon Lispからプログラミングを始めたせいか返り値を使ったプログラミングスタイルが好きなのですが、Rubyは多値と配列と可変長引数が統合されたようになっていて、なかなか使い勝手が良く、Rubyの好きなところの一つです。いまのところ一つです。
x = 3
[0, *if x.even?
[1, 2, 3]
else
[:a, :b, :c]
end]
;⇒ [0, :a, :b, :c]
def iota(n)
if n.zero?
0
else
[n, *iota(n - 1)]
end
end
iota(10)
;⇒ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Proc.new do |x, y| x * y;end [100,2] ;⇒ 200
;; Common LISP (let ((x 3)) `#(0 ,@(if (evenp x) '(1 2 3) '(:a :b :c)))) ;⇒ #(0 :A :B :C) (defun iota (n) (if (zerop n) (list 0) `(,n ,@(iota (1- n))))) ;⇒ (10 9 8 7 6 5 4 3 2 1 0) (multiple-value-call #'* (values-list '(100 2))) ;⇒ 200 ;; もしくは (apply #'* '(100 2)) ;⇒ 200
Rubyの方が統一感があって良いですね。