Ruby Gosu crash error: undefined method `*' for nil:NilClass - ruby

def update
if button_down?(Gosu::KB_SPACE) && !#pressed
#y -= 30
#pressed = true
elsif !button_down?(Gosu::KB_SPACE)
#pressed = false
end
if #vel.nil?
#vel *= 4
#y += #vel * 0.05
end
end
everytime i add an operator for #vel it gives me this error. Why's that?
tried checking if the variable is nil to run but none worked for me

The conditional #vel.nil? only returns true when #vel is nil. You want the opposite, either use !#vel.nil? or use the unless keyword instead of the if keyword before #vel.nil?.

Related

How can I modify a Symbol in Ruby without a RuntimeError: Can't modify frozen Symbol?

I am attempting to change the color of the chess piece if the position matches given criteria however keep getting the following error message:
Position#move_str
Failure/Error: it {expect(Position[P: [e2, e3], p:[d3, d4]].move_str(e2,d3)).to eq "ed3"}
RuntimeError:
can't modify frozen Symbol
# ./chess.rb:24:in `color'
# ./chess.rb:122:in `block in move_str'
# ./chess.rb:122:in `select!'
# ./chess.rb:122:in `move_str'
# ./chess_spec.rb:75:in `block (3 levels) in <top (required)>'
I am calling the code from a separate file (which has linked properly as previous tests with other sections are working). It is running through the following code snippets
chess_spec.rb File:
75. it {expect(Position[P: e2, p:d3].move_str(e2,d3)).to eq "ed"}
76. it {expect(Position[P: [e2, e3], p:[d3, d4]].move_str(e2,d3)).to eq "ed3"}
chess.rb File color
21. class Symbol
22. def color
23. return #color unless #color.nil?
24. #color = :a < self ? :black : :white
25.
26. end
chess.rb File move_str
113. def move_str(from, to)
114. piece = board[from]
115. piece_str = piece.pawn? ? "" : piece
116. list = find(piece, to)
117. is_capture = board[to] || piece.pawn? && to == ep
118. if piece.pawn? && is_capture then
119.
120. possible_pawn_pos = [*0..7].select{|row|board[from%10+(row+2)*10] == piece}
121. possible_pawn_pos.select! { |row| target = board[to%10 + (row+2+white(-1, 1))*10]; target && target.color != piece.color }
122. if possible_pawn_pos == 1 then"#{from.to_sq[0]}#{to.to_sq[0]}"
123. else
124. "#{from.to_sq[0]}#{to.to_sq}"
125. end
126. else
127. if list.size == 1 then
128. "#{piece_str}#{to.to_sq}"
129. elsif list.select { |idx| idx%10 == from%10}.size == 1
130. "#{piece_str}#{from.to_sq[0]}#{to.to_sq}"
131. elsif list.select { |idx| idx/10 == from/10}.size == 1
132. "#{piece_str}#{from.to_sq[1]}#{to.to_sq}"
133. else
134. "#{piece_str}#{from.to_sq}#{to.to_sq}"
135. end
136. end
137. end
chess.rb File white
109. def white(w,b,t=turn)
110. t == :white ? w : b
111. end
I know the error is coming from line 122 as stated in the error message, and believe it is coming from the (row+2+white(-1, 1))*10] section, although not really that sure as am new to Ruby. As it is a Symbol I know you can not dup it at all.
How then would I be able to change the Symbols color?
Thanks for any help in advance and sorry if I have made any mistakes in posting this as I am new to both Ruby and stack overflow.
In Ruby instances of Symbol are intended for use as constants or immutable value. For this reason symbols are always frozen.
:a.frozen? #=> true
The Object#freeze documentation says the following about frozen objects:
freeze → obj
Prevents further modifications to obj. A RuntimeError will be raised if modification is attempted. There is no way to unfreeze a frozen object. See also Object#frozen?.
This method returns self.
a = [ "a", "b", "c" ]
a.freeze
a << "z"
produces:
prog.rb:3:in `<<': can't modify frozen Array (FrozenError)
from prog.rb:3
Objects of the following classes are always frozen: Integer, Float, Symbol.
This means that the error will be raised on the following line:
class Symbol
def color
return #color unless #color.nil?
#color = :a < self ? :black : :white
# ^ Instance variable can only be read, writing to it
# counts as modification, thus raising an exception.
end
end

An integer I pass as an argument becomes a method

I am trying to write a function. Here is the code.
def get_sum(a,b)
if a == b do
return a
end
else
total = 0
for num in a...b
total += num
end
return total
end
end
I get this error:
undefined method `b' for main:Object (NoMethodError)
Can anyone tell me why I get this?
No do for ifs
if a == b
return a
end
Note that you can do the same thing so
def get_sum(a, b)
return a if a == b
(a...b).inject(:+)
end
It is because you have a block do ... end after b. Neither the keyword if nor the syntax sugar form ... == ... accepts a block. The only possibility left to interpret your code syntactically is to interpret b as a method that takes this do ... end block, and that is how it is parsed.

Ruby - Finding primes under 100?

I'd like to write a code that prints out all primes under 100. Here is the code I have so far
class Numbers
def is_a_prime?(int)
x = 2
while x < int/2
if int % x == 0
return false
else
return true
end
end
end
def primes_under_100
x = 2
while x < 100
print x if is_a_prime?(x) # calling the method I defined above
x+= 1
end
end
end
Unfortunately when I call the method using primes_under_100 I get
undefined local variable or method 'primes_under_100' for main:Object
I'd like to know where I went wrong. None of my methods are private. Help is appreciated.
An other way to do this is extend Fixnum. Whit this you should be able to call it on int values.
this should be something like this
class Fixnum
def is_a_prime?
(2..(self/2)).each do |x|
if self % x == 0
return false
end
end
return true
end
end
In order for your code to work you will need to make the following modifications
class Numbers
def is_a_prime?(int)
x = 2
while x < int/2
if int % x == 0
return false
else
return true
end
end
end
def primes_under_100
x = 2
while x < 100
# Notice you're calling is_a_prime? on the instance of the Numbers object
# and sending x as an argument. Not calling is_a_prime? on the 'x'
print x if is_a_prime?(x)
x+= 1
end
end
end
Then call Numbers.new.primes_under_100
How are you calling it? They are public methods of the Number class, so in order to call them, you need to instantiate an object of the Number class:
number = Numbers.new
primes = number.primes_under_100
Also, as the comment from Leo Correa in my answer stated, the method is_a_prime? can't be called like that, you should use:
print x if is_a_prime?(x)
I don't know which version of Ruby include this method to Prime, but if you are using 2.2 and higher you can do it like this.
Add this to top of the file
require 'prime'
And method for showing primes under specific number.
Prime.each(100) do |prime|
p prime #=> 2, 3, 5, 7, 11, ...., 97
end
Here is the reference

while loop less than symbol '<' raising error in Ruby (RubyMine)

I am adding all numbers that are multiples of 3 and 5 less-than 1000.
This is my error:
in `multiple_sum': undefined method `<' for nil:NilClass (NoMethodError)
Here is my code:
def multiple_sum(n)
five_total, three_total, three_subtract = 0
while five_total < n
five_total += five_total+5
end
while three_total < n
if (three_total+3)%5 == 0
three_subtract += three_total+3
end
three_total += three_total+3
end
puts (three_total-three_subtract) + five_total
end
multiple_sum(1000)
Is there a problem with my while loop condition?
You're probably looking for chained assignment: five_total = three_total = three_subtract = 0.
Nope. You are just not setting a value for all the variables listed:
five_total, three_total, three_subtract = 0
That code assigns zero to the first variable, five_total only. three_total and three_subtract are set to nil.
You should set them too:
five_total, three_total, three_subtract = 0, 0, 0

How to redefine a Ruby constant without warning?

I'm running some Ruby code which evals a Ruby file every time its date changes. In the file, I have constant definitions, like
Tau = 2 * Pi
and, of course, they make the interpreter display the unwanted "already initialized constant" warning every time, so, I'd like to have the following functions:
def_if_not_defined(:Tau, 2 * Pi)
redef_without_warning(:Tau, 2 * Pi)
I could avoid the warning by writing all my constant definitions like this:
Tau = 2 * Pi unless defined?(Tau)
but it is inelegant and a bit wet (not DRY).
Is there a better way to def_if_not_defined? And how to redef_without_warning?
--
Solution thanks to Steve:
class Object
def def_if_not_defined(const, value)
mod = self.is_a?(Module) ? self : self.class
mod.const_set(const, value) unless mod.const_defined?(const)
end
def redef_without_warning(const, value)
mod = self.is_a?(Module) ? self : self.class
mod.send(:remove_const, const) if mod.const_defined?(const)
mod.const_set(const, value)
end
end
A = 1
redef_without_warning :A, 2
fail 'unit test' unless A == 2
module M
B = 10
redef_without_warning :B, 20
end
fail 'unit test' unless M::B == 20
--
This question is old. The above code is only necessary for Ruby 1.8. In Ruby 1.9, P3t3rU5's answer produces no warning and is simply better.
The following module may do what you want. If not it may provide some pointers to your solution
module RemovableConstants
def def_if_not_defined(const, value)
self.class.const_set(const, value) unless self.class.const_defined?(const)
end
def redef_without_warning(const, value)
self.class.send(:remove_const, const) if self.class.const_defined?(const)
self.class.const_set(const, value)
end
end
And as an example of using it
class A
include RemovableConstants
def initialize
def_if_not_defined("Foo", "ABC")
def_if_not_defined("Bar", "DEF")
end
def show_constants
puts "Foo is #{Foo}"
puts "Bar is #{Bar}"
end
def reload
redef_without_warning("Foo", "GHI")
redef_without_warning("Bar", "JKL")
end
end
a = A.new
a.show_constants
a.reload
a.show_constants
Gives the following output
Foo is ABC
Bar is DEF
Foo is GHI
Bar is JKL
Forgive me if i've broken any ruby taboos here as I am still getting my head around some of the Module:Class:Eigenclass structure within Ruby
Another approach, using $VERBOSE, to suppress warnings, is discussed here: http://mentalized.net/journal/2010/04/02/suppress_warnings_from_ruby/
Update 2020/5/6: In response to the comment that the link is now dead, I am pasting an example here from my old project, though I can't say whether and in what circumstances it is a good approach:
original_verbose = $VERBOSE
$VERBOSE = nil # suppress warnings
# do stuff that raises warnings you don't care about
$VERBOSE = original_verbose
If you want to redefine a value then don't use constants, use a global variable instead ($tau = 2 * Pi), but that's not a good practice too. You should make it an instance variable of a suitable class.
For the other case, Tau = 2 * Pi unless defined?(Tau) is perfectly alright and the most readable, therefore the most elegant solution.
Unless the values of the constants are pretty weird (i.e. you have constants set to nil or false), the best choice would be to use the conditional assignment operator: Tau ||= 2*Pi
This will set Tau to 2π if it is nil, false or undefined, and leave it alone otherwise.

Resources