Instance vs Class variables and Random Numbers Ruby [closed] - ruby

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hello I am writing a code for a simple die of (n) sides and it works fine:
class Die
def initialize(sides)
#sides = sides
raise ArgumentError if sides < 1
end
def roll
roll_number = rand(#sides) + 1
end
end
However, when I change my initialized variable from instance to class the random numbers generated go from integers to a bunch of small floating point numbers around 1.
class Die
def initialize(sides)
HERE --> ##sides = sides
raise ArgumentError if sides < 1
end
def roll
roll_number = rand(#sides) + 1
end
end
I wanted to know what may cause this difference?

In the second code snippet, you're initializing the class variable ##sides but still passing the instance variable #sides to rand(). Since #sides is not initialized, you are passing nil to the call to rand(), so Kernel#rand gives a float instead of integer.

There's an error in your code, in your second version of Die you actually never call the right variable in your roll method.
It should be:
def roll
roll_number = rand(##sides) + 1
end
Instead yours was evaluating to:
roll_number = rand(nil) + 1
According to the rand library if you don't supply an argument rand returns a pseudo-random floating point number between 0.0 and 1.0, including 0.0 and excluding 1.0.

Related

ruby not equal operator doesn't work but equal does [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm very puzzled with this simple method I have where I'm just trying to puts a character of an array if, when compared with the character of another array, it is different.
This works with the == operator but not with the !=
Maybe it has to do with the each loops but I can't see what the error is. Any ideas?
Thanks
def remove_vowels(s)
nw_s = s.chars
vowels = "aeiou".chars
result = []
nw_s.each do |char|
vowels.each do |vowel|
if char != vowel
print char
end
end
end
end
remove_vowels("apple")
Nested each is no ruby way of doing this kind of task. You can write this
def remove_vowels(s)
nw_s = s.chars
vowels = "aeiou".chars
result = nw_s.map {|k| k unless vowels.include?(k) }.compact
end
remove_vowels("apple")
One line of code instead seven

Having trouble using the modulo operator with a method [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have two methods turn_count(board) and current_player(board). The turn_count(board) method, which returns the number of "X"s and "O"s in an array is working appropriately. But the current_player(board) method, which is supposed to puts "X" if turn_count(board) is even and "O" if turn_count(board) is odd keeps giving me an error. The error is:
/Users/john/Desktop/test file.rb:13:in current_player': undefined method%' for nil:NilClass (NoMethodError)
from /Users/john/Desktop/test file.rb:18:in `'
Clearly it's saying there's an issue with the modulo operator being used, but i'm not sure why and have been wracking my brain trying to figure it out.
Any help is greatly appreciated!
def turn_count(board)
count = 0
board.each do |x| if x == "X" || x == "O"
count = count + 1
end
end
puts count
end
def current_player(board)
if turn_count(board) % == 0
puts "X"
else
puts "O"
end
end
The problem is you are using % on a NilClass. Your turn_count() method returns nil. Check what happens if you replace puts count with count.

Explanation needed for this Ruby challenge [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need some help understanding this code:
def SimpleAdding(num)
sum = 0
(num + 1).times do |x|
sum = sum + x
end
return sum
end
SimpleAdding(12) #=> 78
SimpleAdding(140) #=> 9870
I am not sure of the method. Why is the method written the way it is? Why is sum on the first line set to 0? And why is sum = sum + x used on the third line?
In Ruby, the def keyword delimits the start of a method, in your case named SimpleAdding. It takes one argument (in the parentheses) named num.
In the method body, the variable sum is given the initial value of 0.
The line containing (num + 1).times do |x| tells Ruby to execute the code between the do and end keywords a set number of times (an iterator), in this case num + 1. Remember, num represents the value received in the form of an argument when the method was called.
On the next line, the variable sum (initialized to 0 at the beginning of the method) is assigned the value of itself plus x.
Next line, our iterator ends.
Finally, we return the value stored inside of the variable sum.
And, the end of our method.
Enjoy learning Ruby!

Ruby NameError: Undefined local variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Just started to go through the 7 languages in 7 weeks book; day one problem make a simple higher or lower game in ruby. When I execute my script I get the following error but I don't know why.
EDIT: This is thrown after I guess once.
NameError: undefined local variable or method actual' for main:Object
from guess_game.rb:2:inguess'
from guess_game.rb:22
def guess(guess, acutal)
unless guess == actual
if guess > actual
puts 'Lower'
else
puts 'Higher'
return false
end
end
puts 'Correct'
return true
end
answer = rand(10)
game_won = false
puts 'I am thinking of a number, what is it?'
until game_won
num = gets.to_i
# Static Debug Line
puts "Guess #{num} : Answer #{answer}"
game_won = guess(num, answer)
end
def guess(guess, actual)
unless guess == actual
if guess > actual
puts 'Lower'
else
puts 'Higher'
return false
end
end
puts 'Correct'
return true
end
Problem: Spelling mistake, Corrected

Ruby Methods and Method calls? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am following a beginners course on Ruby, trying to define two methods in the editor:
A greeter method that takes a single string parameter, name, and returns a string greeting that person. (Make sure to use return and don't use print or puts.)
A by_three? method that takes a single integer parameter, number, and returns true if that number is evenly divisible by three and false if not.
The error I'm getting is "unexpected end".
def greeter(name)
return "hey" + name + "how are you" + "."
end
greeter(alan)
def by_three?(number)
if number % 3 == 0
return true
else
return false
end
by_three?(12)
You should terminate if statement with end keyword:
def by_three?(number)
if number % 3 == 0
return true
else
return false
end
end
Having said that, this method is written really bad, and it can be much simpler:
def by_three?(number)
number % 3 == 0
end

Resources