Asked  6 Months ago    Answers:  5   Viewed   24 times

I find myself repeatedly looking for a clear definition of the differences of nil?, blank?, and empty? in Ruby on Rails. Here's the closest I've come:

  • blank? objects are false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank.

  • nil? objects are instances of NilClass.

  • empty? objects are class-specific, and the definition varies from class to class. A string is empty if it has no characters, and an array is empty if it contains no items.

Is there anything missing, or a tighter comparison that can be made?

 Answers

57

.nil? can be used on any object and is true if the object is nil.

.empty? can be used on strings, arrays and hashes and returns true if:

  • String length == 0
  • Array length == 0
  • Hash length == 0

Running .empty? on something that is nil will throw a NoMethodError.

That is where .blank? comes in. It is implemented by Rails and will operate on any object as well as work like .empty? on strings, arrays and hashes.

nil.blank? == true
false.blank? == true
[].blank? == true
{}.blank? == true
"".blank? == true
5.blank? == false
0.blank? == false

.blank? also evaluates true on strings which are non-empty but contain only whitespace:

"  ".blank? == true
"  ".empty? == false

Rails also provides .present?, which returns the negation of .blank?.

Array gotcha: blank? will return false even if all elements of an array are blank. To determine blankness in this case, use all? with blank?, for example:

[ nil, '' ].blank? == false
[ nil, '' ].all? &:blank? == true 
Tuesday, June 1, 2021
 
Hugo
answered 6 Months ago
67

You can do:

width = 2
height = 3
Array.new(height){Array.new(width)} #=> [[nil, nil], [nil, nil], [nil, nil]] 
Wednesday, August 11, 2021
 
PeteT
answered 4 Months ago
12

The difference between Time and DateTime has to do with implementation. A large amount of the DateTime functionality comes from the Rails world and is an arbitrary date with time of day. It's more of a calendar-based system. Time is measured as seconds since January 1, 1970 UTC and is time-zone agnostic. On some systems it is limited to values between 1901 and 2038, a limitation of how traditionally this value is stored as a signed 32-bit integer, but newer versions of Ruby can handle a much wider range, using a 64-bit value or BigNum as required.

In short, DateTime is what you get from a database in Rails where Time is what Ruby has traditionally used. If you're working with values where dates are important and you want to know things like the end of the month or what day it'll be six weeks ahead, use DateTime. If you're just measuring elapsed time and don't care about that, use Time. They're easy to convert between if necessary.

Date on the other hand is just a calendar date and doesn't have any associated times. You might want to use these where times are irrelevant.

strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification. I've rarely seen strptime used since DateTime.parse is usually good at picking up on what's going on, but if you really need to spell it out, by all means use the legacy parser.

Saturday, September 4, 2021
 
Asnexplore
answered 3 Months ago
34

I believe the issue was to do with my Ruby install. I ran the below commands and this seems to have solved the issue

rvm install ruby-2.0.0-p643
rvm --default use 2.0.0
ruby -v
ruby 2.0.0p643 (2015-02-25 revision 49749) [x86_64-darwin14.1.0]
Thursday, September 30, 2021
 
w3stack
answered 2 Months ago
45

what do you get when you try to list your gems.. is json >= 1.4 there?

e.g.:

> gem list json

*** LOCAL GEMS ***

json (1.6.1, 1.5.4, 1.5.3, 1.5.1, 1.4.2)
json_pure (1.6.1, 1.5.4, 1.5.3)

if you use RVM gemsets, you may want to list them also, to check if your default gemset has changed:

rvm gemset list

EDIT: Mystery Solved! :-)

you accidentially installed your gems with sudo -- but you have RVM installed for your user account!

When you have RVM installed for an individual user, it's generally not a good idea to do sudo gem install ... , but you should install the gems just for the local user and let RVM manage them for whatever Ruby version you are using.

If you still install with sudo, RVM will not find the gems, because they are stored in a different location, outside of RVM, and not searched by RVM...! Looks like that's the case you experienced.

Check this RailsCast: http://railscasts.com/episodes/200-rails-3-beta-and-rvm

if you're using Rails: - edit your Gemfile and add the json dependency , then run bundle install

if you're just using Ruby by itself, or an old version or Ruby: - run gem install json in the shell

With newer Ruby versions you should not manually install the gems

Sunday, October 31, 2021
 
g00fy
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share