Undefined method for main:Object (NoMethodError) though method is defined
Ruby expects the method to be declared before you call it, try to move your method definition before you call the method like:
def check_params(param)
# some code
end
check_params param
NoMethodError: undefined method `on' for main:Object
I had the same error just now after running a bundle update, no major versions were updated, except for Rake to 11. An upgrade from capistrano 3.2.1 to 3.4.0 fixed it for me!
NoMethodError: undefined method `get' for main:Object when I try to install webpacker
I changed the ruby version to the 2.5.6 through rvm and it works.
Thanks to @paulo-felipe-souza!
RSpec NoMethodError: undefined method `describe' for main Object
The solution
Call RSpec.describe
instead of just describe
.
require 'rspec'
require './RubyOffRailsTuts/classes/furlong'
RSpec.describe Furlong do
end
The reason it works
You can tell from the error message "undefined method `describe' for main Object" that the underlying problem is that you are trying to calldescribe
on the the basic Object main
, which does not have a describe
method. Undefined method error for main:Object when using gem
You have defined instance methods, not module methods. Change:
def or_gate(a, b)
to:def self.or_gate(a, b)
and it'll work the way you expect:Logic.or_gate(1,2)
=> 1
Repeat this change for all your method definitions.Alternatively, you can use extend self
to accomplish the same goal without having to add self.
to each method definition:
module Logic
extend self
def or_gate(a, b)
a || b
end
end
This adds/copies all the instance methods as module methods.There is more discussion of this here, and this answer goes into some more detail on how methods are defined in modules.
.select undefined method for main:Object (NoMethodError) Ruby
To get rid of the error just replace array(array.index(value) + 1)
with array[array.index(value) + 1]
.
However the solution will still be incorrect. The method index
of an array returns the index of the first object in array
such that the object is == to value
. In case of duplicate elements in array
there will be an error.
I'd recommend rewriting your method as
def remove_double_spaces(array)
array.join.squeeze(' ').split('')
end
remove_double_spaces([" ", " ", " ", "w", "h", "a", "t", " ", "s", " ", "m", "y", " ", " ", " ", " ", " ", "l", "i", "n", "e", " "])
# => [" ", "w", "h", "a", "t", " ", "s", " ", "m", "y", " ", "l", "i", "n", "e", " "]
undefined method `each' for main:Object (NoMethodError)
self
can't get any values, maybe you are looking for something like gets
:
def scrabble
values = {"a" => 1, "e" => 1, "i" => 1, "o" => 1, "u" => 1}
list = []
gets.chomp.split("").each do |letter|
list << values[letter]
end
puts list
end
gets
will ask user for input (in console) and with split
you convert that input into an array with all characters, then you can use each
that array.
Related Topics
Ruby on Rails: Pluralize for Other Languages
How to Share Code Across Chef Cookbooks in a Chef-Repo
Is The Assignment Operator Really "Just" an Operator
Ruby How to Generate a Tree Structure Form Array
Encoding Problems in Rails on Ruby 1.9.1
How to Flush HTML to The Wire in Sinatra
Ruby: How to Convert an Array of Data to Hash and to JSON Format
Starting with Redmine Locally - How Easy Is Migration to Server Later
Fastest Way to Skip Lines While Parsing Files in Ruby
How to Click on Specific Element in Canvas by Its Coordinates (Using Webdriver)
What Is The Purpose of Setting Ruby Block Local Variables When Blocks Have Their Own Scope Already
Can Optionparser Skip Unknown Options, to Be Processed Later in a Ruby Program
How to Restrict Markdown Syntax in Ruby
Rails How to Tell If a Sidekiq Worker Is Done with Perform_Async
Gitlab Configuration Issues:: Nginx Unicorn Port Conflict
How to Calculate a String's Width in Ruby
Command Not Found/Install Missing Gem Binaries with 'Bundle Install' Using Autotest