Looking for Side Income? Click Here to Watch the Customer Testimonials of 1,000s of Others Doing This Right Now!

Author profile picture

@gregorywitekGregory Witek

I manage engineers by day and help developers grow their careers by night

Some time ago during a chat with one of my colleagues we discussed our Python coding style. We joked about how Python follows this idea of having exactly one way to do each thing, which makes some decisions very easy (while feels limiting on the other side). It made me think about Ruby, which has opposite philosophy – everything can be done in many different ways.

This led me to a random idea of checking in how many different ways I can call a single method in Ruby. I ended up with 12 different ways (a couple are a bit of cheating). Prepare for some surprises below – especially the last one is mind blowing!

Warning the code below is not meant for use in production (especially the last 3 examples). It is just my exploration of the language capabilities. Some of the techniques shown are useful in a bunch of cases, but make sure to proceed with caution. Simplicity, security and readability are more important than fancy stuff.


For the sake of this experiment, I’m setting up a single class with one method that I’ll be calling in many different ways. For the sake of simplicity, the method takes no arguments (although I believe each example would work with arguments as well).

The class is called User, it has 1 attribute, name, and the method to be called is hello, which prints a welcome message, including user name.

class User
  def initialize(name)
    @name = name

  def hello
    puts "Hello, #{@name}!"

  def method_missing(_)

user = User.new('Gregory')

1: the obvious way

Not much to see here – this is how you call methods in plenty of languages. Interesting fact: you can put spaces around the dot:

user  .  hello()

will work as well.

2: skip the brackets

Technically it works the same as one before, it just skips brackets, which are optional in Ruby (as long as the code is unambiguous, sometimes they are required due multiple ways in which the code could be parsed)

3-4: using send and public_send


In this case we pass the name of the function to be called as an argument to either send or


methods that are defined in every class. The difference between send and


is that the latter respects the privacy of methods – if you try to call private method, it will raise an error, while send will still call it.

I’m passing the name of the function using symbol type (


), but you can use string as well  (



5-7: using “method” and “call”


3 examples, with the 2nd and 3rd being just a syntax sugar, so I put them together. This one is quite interesting. Calling


returns an instance of Method class. This object can be passed around as any value and can be called any time – it also stores the reference to the object to which it belongs, so if I change the user’s name, the new one will be used:

method = user.method(:hello)
user.set_instance_variable(:@name, "Not Only Code")
method.call() # prints "Hello, Not Only Code!"





are equivalent of


and can also take arguments –






will all work the same way (the last one won’t support named arguments though).

8: using “tap”


is a funny little method that takes a block, passes itself as an argument there and executes the block, and then finally returns itself. I rarely use it, but there are cases where it might be helpful (like side effects when chaining methods).



syntax turns the


symbol into a Proc instance. You can read more about this syntax on Honeybadger blog – it’s really well explained there.


is a callable object, just like


from previous examples.

9: using “to_proc” on function name


I like this one because it reverses the order – user becomes the argument of the function. What happens under the hood here is very similar to the previous one – the


function of


passes the initial symbol to the argument received. Something similar to this:

class Proc
  def call(obj)

10: using “method_missing”

class User
  def method_missing(_)

user.i_am_a_lizard_king # prints "Hello, Gregory!"
user.i_can_do_everything # prints "Hello, Gregory!"

This one is a bit of cheating, since still under the hood I use the standard way of calling a method, but I think it’s worth putting it here.


is a method that will be executed when object receives a call to a method that is not defined. It’s a powerful function that is one of fundaments of Ruby’s flexibility, however it might lead to bugs that take ages to find (and to some perfomance issues), so use it with caution.

11: using “eval”

Again, a bit of cheating, since I still use the standard method call syntax, but how it works under the hood is obviously very different.


passes the string to Ruby parser and interpreter just as if it was a part of my code, and then executes the code. You should definitely, absolutely avoid using it in your code, especially if you allow users to pass some values to your application.

12: using “source” and “instance_eval”

require 'method_source' # external gem

method_source = user.method(:hello).source
method_body = method_source.split("n")[1...-1].join(";")

Ok, the last one – and it’s quite crazy, so explantion is a bit longer. It relies on external gem, called method_source, but that’s because it would take me too much time and space to write this code here (it’s all just Ruby though, no magic!). Let me explain how it works:


returns the source of the method as a string. The output of this is the whole body (including the spaces):

  def hello
    puts "Hello, #{@name}!"

How does


gem do it? The


class in Ruby has a


function that returns the location of the method’s source code – file and line number where the method starts. Then


essentially opens that file, finds the respective line, looks for end that will end the method and returns the code in between.

Now that I have the full code of the method, I want to remove the method definition and the end. In my case I just remove the first and last line, but if the method was a one-liner, it would require some changes. The output of the 2nd line is a string with value:

puts "Hello, #{@name}!"


Finally, I take this string and I pass it to


of my user object.


works kind of like eval except that it executes code in different scope. If I call eval it would execute code in scope of my whole file, where the


variable is not defined. By passing it to


I ensure it uses the correct values.

Is there more?

That was a fun little experiment. I’m pretty sure there are some more ways to call methods in Ruby – it’s a large and very flexible language. If you know some other ways, let me know, I’m very curious!

Also published at: https://www.notonlycode.org/12-ways-to-call-a-method-in-ruby/


Join Hacker Noon

Create your free account to unlock your custom reading experience.

Source link

By Editor