Undefined Method (Nomethoderror) Ruby

undefined method (NoMethodError) ruby

This is because you are calling method choices, before defining it. Write the code as below:

puts "Select [1] [2] [3] or [q] to quit"
users_choice = gets.chomp

def choices (choice)
while choice != 'q'
case choice
when '1'
break puts "you chose one!"
when '2'
break puts "you chose two!"
when '3'
break puts "you chose three!"
end
end
end

choices(users_choice)

I used break, to exit from the while loop. Otherwise it will create an infinite loop.

ruby class undefined method (NoMethodError)

self refers to the class itself in a class method or to the current object in an instance method. In your case it refers to WordGame, the object's class.

If you really want it to refer to 30 into the factors method you have to define it as an instance method, because called on an object (30), not a class (Integer), opening the Integer class

class Integer
def factors
(1..self).select { |n| (self % n).zero? }
end
end

I think you know the alternative:

def self.factors(x)
(1..x).select { |n| (self % n).zero? }
end

def self.convert(number)
factors(number).map(&:to_s).each.map { |char| WORDGAME_MAP[char] }.join
end

Or better, with OOP.

class WordGame
def initialize(n)
@n = n
end

def convert
factors.map(&:to_s).each.map { |char| WORDGAME_MAP[char] }.join
end

private

def factors
(1..@n).select { |m| (@n % m).zero? }
end
end

Wordgame.new(30).convert

Ruby: NoMethodError: undefined method when Mutating Instance Variable

NoMethodError: undefined method `reshuffle_count='

That means you are missing a setter. You have the getter defined def reshuffle_count but no setter.

class MagicBus < Array

attr_writer :seating

def seating
@seating || []
end

def reshuffle_count=(value)
@reshuffle_count=value
end

def reshuffle_count
@reshuffle_count || 0
end
.....

Or as @tadman pointed out, attr_writer :resuffle_count

NoMethodError - undefined method `strftime` for nil:NilClass

Most probably your due_by is nil, no value is set on the particular record you are showing.

You must have a handling for this case or make sure due_by is always present by having a presence validation (or have a db validation on top).

One solution to handle nil due_by is this:

class Task < ActiveRecord::Base
belongs_to :category

def date_format
due_by&.strftime("%a, %b %-d %Y") # By adding `&.`
end
end

Undefined method NoMethodError

The problem is, that you defined the random method as an instance method, but call it on the class level. You have two options to fix this:

Make it a class method (note the self):

class Script
def self.random(temp)
puts "#{temp}"
puts "inside function"
end

print "Enter a number: "
number = gets

random(number)
end

Or change the method to create an instance first (note the new):

class Script
def random(temp)
puts "#{temp}"
puts "inside function"
end

print "Enter a number: "
number = gets

new.random(number)
end

` main ': undefined method `call' for nil:NilClass (NoMethodError)

foo.(bar) is syntactic sugar for foo.call(bar). So, test(:tester).("hello world") is syntactic sugar for test(:tester).call("hello world").

However, test returns nil, so you are calling nil.("hello world") which is the same as nil.call("hello world"), and if you look at the documentation of NilClass, you can easily see that it does not have a method named call. (NilClass inherits from Object which in turn inherits from Kernel and BasicObject, so theoretically, the method could be defined there, but as you can easily check yourself, it isn't.)

Since you are calling a method that does not exist, you get a NoMethodError.

Rails NoMethodError (undefined method `something ' for Time:Class): not so trivial error

NoMethodError (undefined method `now ' for Time:Class):

It says now  not now or as in any other case, where the method can't be found. Try deleting the space right after the now method name.



Related Topics



Leave a reply



Submit