Ruby function 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 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

Related

Ruby: how to interpolate in a string? [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 10 months ago.
Improve this question
Is a simple question about how to print an interpolation on a string in ruby
My method is this:
def hello(parameter)
puts "hello + #{parameter}"
end
puts hello(name)
but irb, show me these message:
NameError: undefined local variable or method `name' for main:Object
It is about my commands to irb? or what i'm doing wrong in my code, 'cause i know that i 've a mistake but can't find where.
Thank you, it's my first post ejje.
The issue lies in your call to hello(name). name in this case is treated at the name of a variable, which you have not defined. If you meant for the console to print "hello + name", then you should call the function with the string "name", surrounded by quote marks: hello("name").

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)

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>

Array Not Defined in Loop? [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 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

Resources