Error in ruby : undefined local variable or method [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
I get an error while trying to program ruby.
Error : C:/Users/PC ASUS/Desktop/g.rb:3:in <main>': undefined local variable or method 'y' for main:Object (NameError)
Here is my code:
puts " Do you like to install hacking pack?"
insta_one = gets.chomp
if insta_one == y
make
else
puts "Ok. Bye!"
end
def make
awe = file.new("shell.bat","w")
readme.puts("#echo off")
readme.puts("color a")
readme.puts("echo Installing hacking pack")
readme.puts("Thanks for downloading rootShell!")
readme.puts("My email - cyniclimbu#gmail.com")
end

y is not string but undefined variable.
Please change line 3:
if insta_one == 'y'

Before you can use a method, you need to define it. In your code, make is defined after you try to call it, so ruby is not familiar with it, and throws an error:
def make
File.open("shell.bat", 'w') do |readme|
readme.puts("#echo off")
readme.puts("color a")
readme.puts("echo Installing hacking pack")
readme.puts("Thanks for downloading rootShell!")
readme.puts("My email - cyniclimbu#gmail.com")
end
end
puts " Do you like to install hacking pack?"
insta_one = gets.chomp
if insta_one == 'y'
make
else
puts "Ok. Bye!"
end

Related

ruby methods with a question mark ? for pattern matching? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Ruby methods that end with a question mark at the end of the name; traditionally returns true or false.
Example:
if success?
puts "yes"
else
puts "oh nos"
end
Is there an accepted style for pattern matching that asks a question and returns [:ok] or [:error, ...]?
For example:
case authorization_valid?
in [:error, msg]
puts "error: #{msg}"
in [:ok]
puts "yes"
end
Instead of a true or false, we are using an :ok, :error.
ruby methods with a question mark ? for pattern matching?
Thoughts?
-daniel
There is no such standard. You might be interested in using Result from dry-monads:
def method_that_return_a_result
if condition
Success :foo
else
Failure :bar
end
end
case method_that_return_a_result
in Success(value)
puts value
in Failure('some specific value')
puts 'this happened, sorry'
in Failure(String => error)
raise ProcessingError, error
in Failure(other)
puts "Unexpected failure #{other.inspect}"
end
(code is just an example of what you can achieve)
Your case would read
case authorization_valid?
in Failure(msg)
puts "error: #{msg}"
in Success
puts "yes"
end

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.

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

Attempting to add line breaks in Ruby to_s message [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
In Pragmatic Programmer's Ruby book, they're using to_s to clean up the output. Their output ends up looking like:
Name: UserName, Address: UserAddress, Phone: UserPhone, EMail: UserEmail
They don't have any line breaks, and I'm attempting to add them to mine using \n within the to_s definition:
class PersonalInformation
def initialize (fullname, address, phonenumber, emailaddress)
#fullname = fullname
#address = address
#phonenumber = phonenumber
#emailaddress = emailaddress
end
def to_s
"Name: #{#fullname}\n, Address: #{#address}\n, Phone: #{#phonenumber}\n, EMail: #{#emailaddress}\n"
end
end
info1 = PersonalInformation.new('UserName', 'UserAddress', 'UserPhone', 'UserEmail')
p info1
but I'm not having any luck getting the output to display the way I want, and it doesn't appear that to_s is doing what I expect it to anyway. Mine looks like:
#<PersonalInformation:0x00000002d0e600 #fullname="UserName", #address="UserAddress", #phonenumber="UserPhone", #emailaddress="UserEmail">
A) What do I need to do to make to_s display the way I expect?
B) How do I go about getting line breaks into the output message?
p calls Object#inspect method, not to_s.
You need to call to_s explicitly, or use methods that call to_s. For example, print, puts, printf with %s format.
A) You don't need anything extra. It already displays the way you want.
B) As you did, putting "\n" works.

Why is this variable undeclared but still working? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm a beginner & just started to learn ruby through Codecademy. I understand this will be a very simple question. Why are we declaring answer2 in this code? Wouldn't they both be equal?
print "This is my question?"
answer = gets.chomp
answer2 = answer.capitalize
answer.capitalize!
Thanks.
They're trying to teach you the difference between capitalize and capitalize!. The first doesn't modify the string but rather returns a modified copy of it. While the latter (with a bang ! mark) modifies the string itself.
So to further explain the example above, consider the following:
print "This is my question?"
answer = gets.chomp
answer2 = answer.capitalize
puts "answer= " + answer + ", while answer2= " + answer2
answer.capitalize!
puts "now answer= " + answer
So if we execute the previous code and enter foobar when it prompts, the output will be:
This is my question?foobar
answer= foobar, while answer2= Foobar
now answer= Foobar
Generally, methods in ruby don't modify the objects that called them, but rather return a modified copy. So in some_object.some_method, the some_method method is not going to change some_object but will return a copy of it with some modifications. Alternatively, methods with at bang ! mark change the objects that called them. So some_object.some_method! will change some_object itself.

Resources