Array Not Defined in Loop? [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 7 years ago.
Improve this question
I have code like the following:
arra = ["a","b","c"]
arrb = ["a","e","d"]
arrc = arra - arrb
while arrc.size != 0
somedef(arrc)
end
I get this error:
NameError: undefined local variable or method `arrc' for main:Object
pointing to the line inside the while loop. Can anyone help me understand why this is?

Are you doing something like this in "somedef":
def somedef(a)
p arrc
end
If so, you get the error because "arrc" is not a global variable. Do this instead:
def somedef(a)
p a
end

Related

In Ruby how can I check if a symbol is in an array of symbols? [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 5 years ago.
Improve this question
Can I do the following? If so how?
I have an array of symbols
symbols = %w{:sym1 :sym2 :sym3} # is this correct implementation?
# I'm putting this in a function for this illustration
def check_symbol(symbol)
symbols.include?(symbol)
end
puts check_symbol(:sym1) # expect true, but I get false
puts check_symbol(:sym44) # expect false of course
How can I do so I get true on the first puts statement?
You can specify an array of symbols as %i[ ... ].
And to check if your symbols is in an array of symbols you could use all? to check if all they respond with true to .is_a?(Symbol) and then if the array includes your specific one, like:
array_of_symbols = %i[sym1 sym2 sym3]
p array_of_symbols.all? { |e| e.is_a?(Symbol) } && array_of_symbols.include?(:sym1)
# true
You don't have an array of symbols, what you have is an array of strings that look like symbols.
The correct definition would be
symbols = %i{sym1 sym2 sym3}

What is the syntax error in this short ruby block? [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 5 years ago.
Improve this question
What is the syntax error in this short ruby block?
def do_this (a,b,c)
puts 1.to_s, 2.to_s,3.to_s
if a == 4
do_this (1,2,3)
end
end
do_this (4,5,6)
I get errors on the fourth and seventh lines, where "do_this" is called.
The error is: 'unexpected ")", expecting "." or...' [...]
Remove spaces between method name and parentheses.
You need to avoid using spaces between the method call and the arguments in parenthesis:
def do_this (a,b,c)
puts 1.to_s, 2.to_s,3.to_s
if a == 4
do_this(1,2,3)
end
end
do_this(4,5,6)

Ruby function undefined local variable or method [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 6 years ago.
Improve this question
def wri(var)
puts var
end
wri(hey)
output : main.rb:4:in': undefined local variable or method hey' for main:Object (NameError)
where is the Mistake?
You are passing the variable hey as an argument to the method wri(). You probably want the string 'hey'
>def wri(var)
> puts var
>end
>nil
>wri('hey')
hey
=> nil
>the_variable_hey = 'hey'
=> 'hey'
>wri(the_variable_hey)
hey
=> nil

unexpected tidentifier expecting keyword_end [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 6 years ago.
Improve this question
I've got a problem in Ruby, "unexpected tidentifier expecting keyword_end", how Can I solve it?
def riko(user)
if user.name.eql? 'Mia Khalifa Fan'
#client.send_msg 'Hola Mia <3 ¿Cómo te trato este dia, cosa guapa y sensual?',
else
if user.mame.eql? 'Skul Goy'
#client.send_msg 'Muerete. '
else
#client.send_msg "Hola #{user.name} o/ \ :v / "
end
end
You're using else if which works fine in other languages, but in Ruby represents 2 distinct conditionals. You probably want to replace it with elsif instead, which is the Ruby equivalent.

initialize method with argument is not recognizing the argument [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 7 years ago.
Improve this question
I am building a rather simple Module::Class with a initialize method.
module Encryption
class Caesar
def initalize(number)
#caesar_number = number
end
end
end
when I run Encryption::Caesar.new(2) i get the following error:
ArgumentError: wrong number of arguments (1 for 0)
from (irb):32:in `initialize'
from (irb):32:in `new'
from (irb):32
from /Users/yedidyaweiner/.rvm/rubies/ruby-2.1.3/bin/irb:11:in `<main>
If i run Encryption::Caesar.new, it successfully creates a new instance of the class.
Why is the error saying that it does not expect an argument when it is defined in the initialize method?
initalize is misspelled; it should be initialize.
module Encryption
class Caesar
def initialize(number)
#caesar_number = number
end
end
end
foo = Encryption::Caesar.new(2)
foo.inspect #=> #<Encryption::Caesar:0x1e05580 #caesar_number=2>

Resources