Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Ruby shortcut for calling the same name of a method chain?

Tags:

ruby

Say I have

class A
end

class B < A
end

p B.superclass.superclass.superclass # => BasicObject

Instead of calling .superclass 3 times, is there some way I can specify something like a repeat operator which will basically say call this method x times?

like image 676
daremkd Avatar asked Oct 28 '25 22:10

daremkd


2 Answers

This is how it is done

3.times.reduce(B) {|a, _| a.superclass } # => BasicObject
like image 187
Boris Stitnicky Avatar answered Oct 31 '25 12:10

Boris Stitnicky


I made it as below :

([:superclass]*3).inject(B,:send)
# => BasicObject
like image 32
Arup Rakshit Avatar answered Oct 31 '25 10:10

Arup Rakshit