How to fix a NoMethodError :undefined method `<<' for nil:NilClass - ruby

I have 3 error messages which I believe all relate to one thing - me calling a method on an object that has not been defined properly.
I've checked over for typos, white spacing, syntax, naming. I'm following the same process as other methods in this program which all tested OK.
I've narrowed the loop down and believe the issue is here:
def add(param)
#param << param
Heres the code in full context:
class Todo
def initialize(param)
#param = param
end
def text
return #param
end
end
class TodoList
def initialize
#item_list = []
end
def add(param)
#param << param
end
def print
#item_list.each do |param|
puts "* #{param.text}" #=> .text?
end
end
end
Error Messages:
Here are the error messages I'm trying to resolve. As I mentioned earlier they all look to be pointing to a method I've not defined properly but I've not yet been able to work out why that may be.
1) Q1. Todo list TodoList adding todos adds a todo without raising an error
Failure/Error: expect { todo_list.add(todo) }.to_not raise_error
expected no Exception, got #<NoMethodError: undefined method `<<' for nil:NilClass> with backtrace:
# ./questions/question_1.rb:43:in `add'
# ./spec/question_1_spec.rb:31:in `block (5 levels) in <top (required)>'
# ./spec/question_1_spec.rb:31:in `block (4 levels) in <top (required)>'
# ./spec/question_1_spec.rb:31:in `block (4 levels) in <top (required)>'
2) Q1. Todo list TodoList printing todos one todo prints a single todo with a bullet point
Failure/Error: todo_list.add(todo)
NoMethodError:
undefined method `<<' for nil:NilClass
# ./questions/question_1.rb:43:in `add'
# ./spec/question_1_spec.rb:41:in `block (5 levels) in <top (required)>'
3) Q1. Todo list TodoList printing todos many todos prints the todos, separated by newlines
Failure/Error: todo_list.add(todo_1)
NoMethodError:
undefined method `<<' for nil:NilClass
# ./questions/question_1.rb:43:in `add'
# ./spec/question_1_spec.rb:59:in `block (5 levels) in <top (required)>'
Thanks!

ruby do not know what #param is in your TodoList class, you have to initialize it in your constructor like this #param = [] that fix your issue

By default instance variables are initialized to nil, hence the error. So in your TodoList class, you need to initialize #param to an object that can respond to <<, which in your own case, is an array []
class TodoList
def initialize
#param = []
end
end

Related

Celluloid 0.17.3 giving unexpected "undefined method" error

I have started using Celluloid gem this morning for that first time. I am following this Railscasts tutorial and trying to figure things out.
I have a class called "SomeClass" and it has only one method. Here is the code:
require 'celluloid'
class SomeClass
include Celluloid
def initialize(name)
#name = name
end
def assholify()
puts "#{#name} has become an ASSHOLE."
end
end
When I create new instances of the class and call its method (with a bang i.e. "assholify!"), I am getting the undefined method 'assholify!', error. But Celluloid is supposed to trigger the method asynchronously when it is called with a bang. So here is how I am calling the method:
names = ['John', 'Tom', 'Harry']
names.each do |name|
n = SomeClass.new name
n.assholify!
end
Here is the full backtrace of the error:
I, [2016-09-09T11:28:02.488618 #3682] INFO -- : Celluloid 0.17.3 is running in BACKPORTED mode. [ http://git.io/vJf3J ]
/home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:42:in `rescue in check': undefined method `assholify!' for #<SomeClass:0x10897dc> (NoMethodError)
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:39:in `check'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:26:in `dispatch'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/call/sync.rb:16:in `dispatch'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/cell.rb:50:in `block in dispatch'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/cell.rb:76:in `block in task'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:339:in `block in task'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task.rb:44:in `block in initialize'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task/fibered.rb:14:in `block in create'
from (celluloid):0:in `remote procedure call'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/call/sync.rb:45:in `value'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/proxy/sync.rb:22:in `method_missing'
from some_class.rb:18:in `block in <main>'
from some_class.rb:16:in `each'
from some_class.rb:16:in `<main>'
Why am I getting this error? Is it the right way to call the function? Also how do I get rid of Celluloid 0.17.3 is running in BACKPORTED mode. warning?
The undefined method error occurred because actor methods are not called with a bang in the recent versions of celluloid gem. Instead you call the method like this: n.async.assholify. So here is what the code should look like:
names = ['John', 'Tom', 'Harry']
names.each do |name|
n = SomeClass.new name
n.async.assholify # Instead of "n.assholify!"
end
For "Celluloid 0.17.0 is running in BACKPORTED mode" warning, take a look at this wiki. Backported Mode is the default, for a limited time. If you use require 'celluloid/current' instead of require 'celluloid', you should not see this warning.

TypeError: wrong argument type class (expected module) in rspec

i'm developing a simple net application (packed as a gem) to learn Ruby and TDD.
I have this class (receiver.rb):
require 'eventmachine'
class Receiver < EM::Connection
def initialize(port)
EM.run do
EM.open_datagram_socket('0.0.0.0', port, self)
end
end
end
And this test (receiver_spec.rb):
require "spec_helper"
require "net/receiver"
describe "Receiver" do
it "can istantiate" do
#rec = Receiver.new(500)
end
end
Anyway, when i run rspec it prints out this:
1) Receiver can istantiate
Failure/Error: #rec = Receiver.new(500)
TypeError:
wrong argument type Receiver (expected Module)
# /var/lib/gems/1.9.1/gems/eventmachine- 1.0.7/lib/eventmachine.rb:1535:in `include'
# /var/lib/gems/1.9.1/gems/eventmachine-1.0.7/lib/eventmachine.rb:1535:in `block in klass_from_handler'
# /var/lib/gems/1.9.1/gems/eventmachine-1.0.7/lib/eventmachine.rb:1535:in `initialize'
# /var/lib/gems/1.9.1/gems/eventmachine-1.0.7/lib/eventmachine.rb:1535:in `new'
# /var/lib/gems/1.9.1/gems/eventmachine-1.0.7/lib/eventmachine.rb:1535:in `klass_from_handler'
# /var/lib/gems/1.9.1/gems/eventmachine- 1.0.7/lib/eventmachine.rb:867:in `open_datagram_socket'
# ./lib/rppc/net/receiver.rb:9:in `block in initialize'
# /var/lib/gems/1.9.1/gems/eventmachine-1.0.7/lib/eventmachine.rb:187:in `call'
# /var/lib/gems/1.9.1/gems/eventmachine-1.0.7/lib/eventmachine.rb:187:in `run_machine'
# /var/lib/gems/1.9.1/gems/eventmachine-1.0.7/lib/eventmachine.rb:187:in `run'
# ./lib/rppc/net/receiver.rb:8:in `initialize'
# ./spec/net/receiver_spec.rb:6:in `new'
# ./spec/net/receiver_spec.rb:6:in `block (2 levels) in <top (required)>'
I'm quite new to the ruby environment, so if i missed something let me know.
I'm not sure what documentation you're working from, but it appears the open_datagram_socket requires a Module and cannot accept a Class as the third (handler) argument.
Per the comment in http://www.rubydoc.info/github/eventmachine/eventmachine/EventMachine.open_datagram_socket, it appears this restriction may have been loosened in a later version of EventMachine

Ruby script raising unexpected backtrace

I have a method that should raise a custom error with a message. When I catch the error and raise my own custom error, it is still raising and printing the backtrace of the original error. I just want the custom error and message. Code below.
Method:
def load(configs)
begin
opts = {access_token: configs['token'],
api_endpoint: configs['endpoint'],
web_endpoint: configs['site'],
auto_paginate: configs['pagination']}
client = Octokit::Client.new(opts)
repos = client.org_repos(configs['org'])
repos.each do |r|
Project.create(name: r.name)
end
rescue Octokit::Unauthorized
raise GitConfigError, "boom"
end
#rescue Octokit::Unauthorized
end
class GitConfigError < StandardError
end
My test (which is failling):
context 'with incorrect git configs' do
before do
allow(loader).to receive(:load).and_raise Octokit::Unauthorized
end
it { expect{loader.load(configs)}.to raise_error(GitConfigError, "boom" ) }
end
Test Output:
GitProjectLoader#load with incorrect git configs should raise GitConfigError with "boom"
Failure/Error: it { expect{loader.load(configs)}.to raise_error(GitConfigError, "boom" ) }
expected GitConfigError with "boom", got #<Octokit::Unauthorized: Octokit::Unauthorized> with backtrace:
# ./spec/lib/git_project_loader_spec.rb:24:in `block (5 levels) in <top (required)>'
# ./spec/lib/git_project_loader_spec.rb:24:in `block (4 levels) in <top (required)>'
# ./spec/lib/git_project_loader_spec.rb:24:in `block (4 levels) in <top (required)>'
If you intend to test the handling of the Octokit::Unauthorized error, then raise the error anywhere before the rescue kicks in. Preferably, someplace where it would actually be raised.
Something like this, for example:
before do
allow(Octokit::Client).to receive(:new).and_raise(Octokit::Unauthorized)
end
And then:
expect{ loader.load(configs) }.to raise_error(GitConfigError, "boom" )
As a side note, I would discourage enclosing all lines of your method in a begin;rescue;end structure; you should enclose only the lines from which you are expecting errors.
You are not testing your code as you think. You have mocked it out.
The line
allow(loader).to receive(:load).and_raise Octokit::Unauthorized
replaces the load method on loader with a stub which just raises the named error.
Remove your before block, and it should test your code as intended. Note as written it will make a real request via Octokit, unless you mock that out instead.

Unexpected Method Call

I'm using mongomapper to store pages in a db, and I index them first. In the index method, I loop through each word, and check to see if it is already in the words hashmap. If not, I add an empty array to the hash, then push its location to the array.
def index_words
#words = self.body.split(" ")
#words.each_with_index do |word,i|
if self.words[word.stem].nil?
self.words[word.stem] = []
end
puts "Called from #{caller[0]}"
self.words[word.stem].push(i)
end
end
When I run this, I get an undefined method error, saying that self.words[word.stem] is nil. Furthermore, this method is actually being called from the loop, when it's only called once in the constructor:
def initialize(*args)
super
index_words
end
The error message is:
p = Page.new({author: 'Michael',url: 'michaelfine.me',title: 'Michael Fine',body: 'Body Text'})
called fromPage.rb:19:in `each'
NoMethodError: undefined method `push' for nil:NilClass
from Page.rb:24:in `block in index_words'
from Page.rb:19:in `each'
from Page.rb:19:in `each_with_index'
from Page.rb:19:in `index_words'
from Page.rb:14:in `initialize'
from (irb):103:in `new'
from (irb):103
from /Users/Michael/.rvm/rubies/ruby-1.9.3-p286/bin/irb:16:in `<main>'

Chingu input property on a Chingu::GameWindow causes exception

This code is causing a very strange exception in Chingu, a game library for ruby that depends upon the Gosu 2D game framework:
class StopSignAttack < Chingu::Window
def initialize
super(1000, 1000, false)
self.input = { :escape => :close }
self.caption = "Stop Sign ATTACK!!!"
push_game_state(Start.new)
end
end
The value of the input property for the Chingu::Window should simply cause the game window to close when pressing the escape key. The window closes when pressing the escape key, but bombs badly with this exception:
/Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/helpers/input_dispatcher.rb:63:in `block in dispatch_input_for': undefined method `button_down?' for nil:NilClass (NoMethodError)
from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/helpers/input_dispatcher.rb:62:in `each'
from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/helpers/input_dispatcher.rb:62:in `dispatch_input_for'
from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/window.rb:148:in `block in intermediate_update'
from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/window.rb:148:in `each'
from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/window.rb:148:in `intermediate_update'
from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/chingu-0.8.1/lib/chingu/window.rb:133:in `update'
from stop_sign_attack.rb:24:in `<main>'
I've tried implementing the close method in the Chingu::Window, calling the super class close method:
def close
super.close
end
And I get this exception when pressing the ESC key:
stop_sign_attack.rb:23:in `close': undefined method `close' for nil:NilClass (NoMethodError)
from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/gosu-0.7.41-universal-darwin/lib/gosu/swig_patches.rb:19:in `rescue in block (2 levels) in <class:Window>'
from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/gosu-0.7.41-universal-darwin/lib/gosu/swig_patches.rb:12:in `block (2 levels) in <class:Window>'
from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/gosu-0.7.41-universal-darwin/lib/gosu/swig_patches.rb:26:in `show'
from /Users/sams/.rvm/gems/ruby-1.9.3-p0/gems/gosu-0.7.41-universal-darwin/lib/gosu/swig_patches.rb:26:in `show'
from stop_sign_attack.rb:28:in `<main>'
Any ideas are appreciated.
You left off some information that would allow someone to debug this. For example, you're pushing a game state called Start, but the Start class is not declared in your example. but you haven't shared that portion of the code. I created the following sample project to test out what you've got there:
require 'chingu'
# A simple GameState-example
class Start < Chingu::GameState
end
class StopSignAttack < Chingu::Window
def initialize
super(1000, 1000, false)
self.input = { :escape => :close }
self.caption = "Stop Sign ATTACK!!!"
push_game_state(Start.new)
end
end
StopSignAttack.new.show
This works just fine, exiting without any exception when I press escape.

Resources