50

3 Unexpected Behaviors using Ruby

 5 years ago
source link: https://www.tuicool.com/articles/hit/MzUzau7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ZRJjMzI.jpg!webbqieUr7.jpg!web
Cat and mouse become best friends. Unexpected..

3 Unexpected Behaviors using Ruby

In this article we’re going to explore the following topics:

  • return values within an ensure clause
  • variables declared in a conditional block
  • String#to_i vs Integer(String)

Introduction

In Ruby, what I call an unexpected behavior is a behavior that doesn’t seem natural at first.

This means that with a bit of knowledge about what happens behind the scene. we are able to understand the purpose of these choices.

In this article we’re going to break down 3 examples of unexpected behaviors using Ruby.

Return values within an ensure clause

In Ruby, the value of the last evaluated statement is used as return value of the called method if there is no explicit call to return

vmAz6rn.jpg!web

Here, the result of the 21 + 21 operation is used as return value of the add method.

That’s why a call to the add method returns 42 .

But, what happens when the last statement of an ensure clause is evaluated?

vmAz6rn.jpg!web

The last evaluated statement of the henry.hello('TJ Dillashaw') method call is the "hello #{@name}" string interpolation within the ensure clause.

So we’d expect the call to henry.hello('TJ Dillashaw') to return "hello TJ Dillashaw" .

But, it returns "TJ Dillashaw" which is the result of the @name = name assignation.

So, we have to explicitly use the return keyword if we want to return the value of the ensure clause

vmAz6rn.jpg!web

Variables declared in a conditional block

Variables declared in a conditional block that is never evaluated are initialised with a default nil value

vmAz6rn.jpg!web

Here, the champ variable is initialized with a nil value even though the content of the if conditional block is never evaluated.

In this case, we’d expect the following error to be raised:

NameError (undefined local variable or method `champ’ ..)

This behavior is due to the fact that your Ruby code must first be parsed before it can be run.

It’s at this stage that the behavior you’re experiencing originates.

As the parser scans through the code, whenever it encounters a variable declaration then it allocates space for that variable by setting its value to nil .

String#to_i vs Integer(String)

The String#to_i method is used to convert the string representation of a number to an Integer

vmAz6rn.jpg!web

It works as expected.

But what if the string that calls the to_i method is not the representation of a number ?

vmAz6rn.jpg!web

Here we can see that "a string".to_i returns 0 .

We’d expect this method call to return nil — or to raise an error.

To do so, we can use the Kernel#Integer() method.

This method raises a TypeError if the passed string is not strictly conformed to numeric representation

vmAz6rn.jpg!web

Voilà!

ONE MORE THING ⬇

m6rQNfj.png!webVZ7BRr6.png!web

Feel free to subscribe here: www.rubycademy.com

Thank you for taking the time to read this post :-)

Feel free to :clap: and share this article if it has been useful for you. :rocket:

Also, as I post an article every 3 days, feel free to follow me to be notified of my new article releases.

Here is a link to my last article:

Understanding the Eigenclass in less than 5 minutes


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK