initialize method with argument is not recognizing the argument [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 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>

Related

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

ruby inheritence failure I can't undertand [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 can't understand why the following code give error
./nodes.rb:14:in `initialize': wrong number of arguments (1 for 0) (ArgumentError)
from ./nodes.rb:14:in `initialize'
from ./nodes.rb:23:in `initialize'
from ./nodes.rb:31:in `new'
from ./nodes.rb:31:in `<main>'
Can someone please enlighten me?
class Base
def initalize(msg)
print "########## This is the Base class ###########"
end
end
class A < Base
attr_accessor :var_a
def initialize(msg)
super
var_a = "AAAAA"
print "########### From A: #{msg} VAR: #{var_a} ########################\n"
end
end
class B < A
attr_accessor :var_b
def initialize(msg)
super
var_b = "BBBBB"
print "########### From B: #{msg} VAR: #{var_b} ########################\n"
binding.pry
end
end
b = B.new("test")
no = A.new("This is 'A'")
The other posters are correct that you spelled "initialize" wrong in your code.
Something to be aware of when using super in ruby - when calling "super" by itself it will pass all arguments given to the current method. So in your case it was passing msg to a new Base class. Because you spelled initialize wrong, it wouldn't accept any arguments hence why you were getting a (1 for 0) error.
If you kept your current code and used super() it would call the super method without any arguments, and work. Albeit with the error, but this would be able to run. Using super with the empty parenthesis is one of the only time I can think of where this will make a difference.
You spelled initialize wrong in Base. So, the super call in A refers to the default Object#initialize which doesn't take any arguments.
You have a typo in:
class Base
def initalize(msg)
It should be initialize. So Ruby uses the default initialize that takes no argument, causing the ArgumentError you saw.
super invokes a method with the same name as the current method in the superclass of the current class. It is invoking initialize in each of the parent classes and needs a msg.

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

no implicit conversion of Symbol into Integer? [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 can't seem to find what is causing this bug. The server is saying that it is line #8 in this code:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params [:session][:password])
log_in user
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
end
end
Any help would be appreciated.
You have a space here:
if user && user.authenticate(params [:session][:password])
# see?
params [:session][:password]
Fix it to params[:session][:password] and re-try

Resources