Now, Rails developers are going to cringe at this function. You’ll never actually die and dump to debug in Rails or Ruby applications. Like ever. I’ll cover that in the debugging section.

So yea, named functions in either language are super similar to each others. The biggest difference is that Ruby just needs the keyword def to define the start of a function whereas PHP wants you to type the whole word function. w00t!

Passing Arguments

Yup, this topic actually deserves a whole section. In PHP this is a moot issue. You have only a few different ways of passing arguments. The biggest difference is how arbitary $options are passed.

You may recognize this pattern, where the last argument for a function is a way of passing an associative array that alters the behavior of the function:

functioncombine($cookie,$iceCream,$options=[]){$dessert=sprintf('%s and %s',$cookie,$iceCream);// if a topping is included, let's add it to the $dessert.if(isset($options['topping'])){$dessert=sprintf('%s topped with %s',$dessert,$options['topping'];}return$dessert;}$dessert=combine('oreo','vanilla ice cream');echo$dessert;=>'oreo and vanilla ice cream'// now, with toppings!$betterDessert=combine('oreo','vanilla icea cream',['topping'=>'peanut butter']);echo$betterDessert;=>'oreo and vanilla ice cream topped with peanut butter'

Mmm ice cream with peanut butter. Don’t knock it till you try it.

This pattern is used quite a bit in Ruby, but they had a little twist. When calling a function, you don’t have to explictly pass the last argument with the {} that usually denote a Hash. It’s optional to provide them.

Here’s an example with our yummy combine example from above but Rubified:

defcombine(cookie,ice_cream,options={})dessert="#{cookie} and #{ice_cream}"# if a topping is included, let's add it to the dessert.dessert="#{dessert} with #{options[:topping]}"ifoptions.key?(:topping)enddessert=combine('oreo','vanilla ice cream')putsdessert=>'oreo and vanilla ice cream'

Now check out how we pass the options Hash - no wrapping {} brackets required!

When you pass a Hash without the wrapping curly brackets {} as the last argument, Ruby knows what’re trying to pass to the argument and automatically populates the last variable (a.k.a. options in our example above) with that Hash.

Imagine that our combine function could accept many options, the Hash argument still works the same:

Yea, the outer paranthesis are totally optional. This is absolutely foreign at first, and like me, you may be wondering how the heck Ruby can interpret where the arguments begin and end.

However this inference will quickly become second nature. Reading or writing in this format is optional, but it’s very easy to pick up on when it works and reads nicely.

Anonymous Functions a.k.a. Closures

Thanks to the addition to Anonymous Functions in PHP, we have the ability to start doing things like Higher Order Functions - you can see this pattern in action in GuzzleHttp’s Guzzle libary and Laravel’s Router module. That’s an advanced design pattern that’s outside the scope of this book, but just know it’s possible to use this pattern in both languages.

I shouldn’t probably tell you this. But since version 7, PHP can actually execute anonymous functions immediately just like Javascript:

(function(){echo'Hello World - from inside a Closure.';})();

Not that you couldn’t do it in PHP 5.6 and below, it just needed to be called with user_call_func:

call_user_func(function(echo'Hello World - yup still a Closure.';));

You decide what’s more legible.

So with Ruby’s “Everything is an Object” mantra - can it still do Closures? You bet.

And before we go further,you should know that Ruby uses Closures quite a bit. In PHP you can get by without using Closures for a long long time, but in Ruby they’re a pretty central concept. Don’t sweat it, they’re going to feel natural in no time.

Two Ways to Tango

In our little PHP world, anonymous functions can only really be written out in one way. They need the keyword function, a little set of parenthesis () to define your arguments, and last but not least - a wrapping set of curly brackets {} to hold the business logic in place:

function($argument){return$argument+1;}

This formula holds true even if you’re placing your function in a Class or just dangling out there in the Global namespace.

However Ruby is a bit more flexible than that. You can type a function “block” in multiple ways. Here’s the most similar to PHP, the block that takes up multiple lines:

do|argument|returnargument+1end

First big difference is the total lack of curly brackets {} in this Ruby example. They happen here and there in Ruby but not very often. We’re used to wrapping functions with {} in PHP, but in Ruby we instead infer the opening bracket { and use the keyword end as an alias to the end bracket }.

As a result of this inference on Ruby’s part, we save ourself 1 keystroke every method we write. Lovely.

Second, but minor difference is the lack of paranthesis () that denote the expected arguments, i.e. ($argument). We instead wrap the expected arguments with pipes ||. So ($argument) becomes |argument|.

P.S. don’t forget to say goodbye to our good friend $.

One more goodie Ruby provides for us. There is also special syntax for blocks that’s easier to read for 1 line functions: