Why Is 'A = A' 'Nil' in Ruby

Why does Ruby use nil to name the null object?

Well, "nil" is the traditional name for the reified concept of "nothing" in Lisp and Smalltalk†. The word "null" is used as an adjective meaning "empty", as in "the null list," which is nil.

Meanwhile, "null" is traditionally a pointer value in C that signifies the pointer doesn't point to anything valid. It refers to the fact that the pointer is null (in the same sense that Lisp uses the word), but it came to be thought of as a value on its own.

Matz was a fan of both Smalltalk and Lisp, so he went with their terminology. There isn't really an important difference in meaning between the two terms — one is just C-ish.

Actually, "nil" existed in a lot more languages than just those. Even Algol-68, the great granddaddy of C, called it "nil". I'm not sure if C invented "null" as the name for the null reference or just popularized it, but I'm pretty sure that all the modern languages using that term got it from C.

Why is `a = a` `nil` in Ruby?

Ruby interpreter initializes a local variable with nil when it sees an assignment to it. It initializes the local variable before it executes the assignment expression or even when the assignment is not reachable (as in the example below). This means your code initializes a with nil and then the expression a = nil will evaluate to the right hand value.

a = 1 if false
a.nil? # => true

The first assignment expression is not executed, but a is initialized with nil.

You can find this behaviour documented in the Ruby assignment documentation.

Why does '= nil' appear in the output when I use this website to code but not when I use Sublime Text?

nil in this case, is just the return value of your method call. It didn't return anything. The repl prints it out for you, but when you run a Ruby file, you won't see it.

e.g. in pry, irb, repl.it, etc.

puts 5


=> nil

The puts command prints 5, then returns nil. The repl prints that for you, so you know what the return value was. You can try it for yourself

def test
puts 'test'
return 5


=> 5

If you want it to appear when running a file, you can print the return of the function. e.g.

puts "=> #{test.inspect}"


=> 5
=> nil # this line only if running in repl

Why am I getting a nil value from a Ruby object's member, when it is not nil?

I've solved the problem: chart.ScenarioID is the way I'd do it in C++ and it doesn't work here; it returns nil.

In Ruby, chart[:ScenarioID] works, and correctly returns 160.

Why is class variable's value 'nil' in ruby?

basically this:

class << self
attr_accessor :member

does not create a reader/writer for @@member as you expect but rather @member on the class level.

Classes are instances (of Class) and so they have instance variables too.

@@ variables are a special thing - a variable that can be accessed from both the class and instance scope (and also shared through the iheritance chain) - and are not the same as the class's instance variables.

If you want a reader/writer for the class variable you either have to write it by hand (e.g. def member and def member=(val) in the class << self block), or if you require 'active_support/all' you can use:

class Foo
cattr_accessor :member

In this case calling @@member = from an instance method or calling Foo.member = modify the same value.

undefined method `-' for nil:NilClass in ruby model

You can't call or chain methods on a nil object. You're trying to subtract something from nil which the error has told you. So you can fix this with:

def seat_avaliable
if seat.is_a?(Integer) && students&.any?
seat - students.count > 0

Change your view code to this

<% if bus.seat_available %><td>Seat Available</td><% end %>

The Array contain nil element or not?

array.nil? checks if array is nil, not if it contains a nil value. Similarly, array.empty? checks if array contains no elements.

If you want to check if any element is nil, use any?.

array.any? { |element| element.nil? }

That can be shortened to array.any?(nil).

Why let(:counter) { 0 } returns nil in example?

Why let(:counter) { 0 } returns nil in example?

No, it doesn't. counter is not what you think it is. Try evaluating/printing defined?(counter) and defined?(mocked_retry_count).

What is the principal difference between 'counter' and 'mocked_retry_count' ?

You do not assign to mocked_retry_count. Remember, let creates methods. So when you attempt to assign to counter, you're creating a local variable counter which shadows your method counter (and has default value of nil).

This post explains in more detail: Why is `a = a` `nil` in Ruby?

Having trouble setting an object to nil in ruby binary search tree

You cannot "set an object to nil". An object can never change its class, it either is an instance of Node or it is an instance of NilClass, it cannot at one point in time be an instance of Node and at another point in time be an instance of NilClass.

Likewise, an object cannot change its identity, object #4711 will always be object #4711, however, nil is a singleton, so there is only one nil and it has the same identity during the entire lifetime of the system.

What you can do is to bind the variable which references the object to nil. You are doing the opposite operation inside your insert method.

Related Topics

Leave a reply