45058

Ruby - everything evaluates to true

According to the literature I have been reading online, everything evaluates to true in Ruby except false and nil. I am trying to find out what benefits this may have? Thanks

Answer1:

I will sum this advantage up in checking if the user input a value.

PHP:

if($somevalue !== null) { # '' and '0' are falsy.


Ruby:

if somevalue

Answer2:

It makes the language's concept of truth very simple — the only things that are considered "false" are things that do logically represent a "no" answer: false itself and nil, which represents the concept of "nothingness". In a language like Ruby, I find it hard to argue for making more values falsy unless it's just to make things feel more familiar for people experienced in other languages.

(Incidentally, I also think C's model of truth is very simple: If something is equivalent to zero, it's false. But in Ruby, where we deal mainly in objects rather than numbers, this would make false truthy!)

Answer3:

It makes most logic statements easier to read. (And write, but I consider that of lesser importance.)

Answer4:

Everything in Ruby is an object. Here's a decent read.

Recommend

  • Test if numpy array contains only zeros
  • What does || mean? [duplicate]
  • What does this Javascript code mean? [duplicate]
  • Passing the argument name while calling function in javascript
  • C++: Mask and decoding bits
  • What does different namespaces mean in Flex?
  • Minimum ordering of a loop of values
  • onreadystatechange is not called in firefox
  • Python function optional arguments - possible to add as condition?
  • How to accept hash parameters in routes
  • How to make an elasticsearch query that filters on the maximum value of a field?
  • How can the java 'class' literal return different instances of the Class object for the sa
  • In BASH convert a string with . in float
  • Raise Session_OnStart event from custom ASP.NET SessionStateProvider class
  • How to only store 3 values for a key in a dictionary? Python
  • Is WITH the replacement for a #TEMP table?
  • Is there a way to link a linux's thread TID and a pthread_t “thread ID”
  • NUnit 3.0 TestCase const custom object arguments
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Android activity accessing service's static reference before the service is ready
  • How to define and use opencv mat of user type
  • Switching to Release Build causes runtime error in Web Reference
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Projection media query: browser support and workarounds?
  • what is the difference between the asp.net mvc application and asp.net web application
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Jquery - Jquery Wysiwyg return html as a string
  • Arrays break string types in Julia
  • Comma separated Values
  • Matrix multiplication with MKL
  • Linker errors when using intrinsic function via function pointer
  • A cron job substitute?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • json Serialization in asp
  • WPF Applying a trigger on binding failure
  • Java static initializers and reflection
  • LevelDB C iterator
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?