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
Please see the code below
require 'rubygems'
require 'selenium-webdriver'
findtrue = true
count = 0
while findtrue do
count+=1
if count 10
findtrue = false;
elsif puts count
end
end
I am getting an error
undefined method `count' for main:Object (NoMethodError)
Why I am getting this error? Anything to be added in the require section?
The code if count 10 is translated to a method call: if count(10) and you clearly don't have a count method. I suppose you want to use a comparison operator like if count == 10.
Edit: You should also change elsif to else:
if count == 10
findtrue = false
else
puts count
end
the error is caused by the line
if count 10
this is read by ruby to be
if count(10)
which tries to call a method called count, which does not exist.
I guess what you intended to do was to check if count equals 10. For that you need to add the == sign:
if count == 10
You come from a more traditionale program language looking at your code, in Ruby this can be done clearer and shorter, in short more Rubyish.
First, require 'rubygems' is not necessary for Ruby > v1.9
And the rest can be done with this one line
9.times{|count|puts count}
or its longer version
9.times do |count|
puts count
# and do whatever else
end
Related
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)
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 am running the following ruby code in my local environment:
def multiples(max)
array = []
(0...max).each do |n|
if (n % 3 == 0) || (n % 5 == 0)
array << n
end
end
array.inject(:+)
end
multiples(1000)
and nothing happens at all. My code looks good to me. What's the issue here?
I'm not sure what you're expecting, but if I paste your code into irb it does in fact do something.
> multiples(1000)
233168
If you are running your code as a command-line Ruby script, then perhaps you want to print this value so you can see the result on the console? In that case you want to use puts:
puts multiples(1000)
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>
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to write a program in ruby programming language which prints the longest name among others,use the split method,size max ,length.
This is what I have so far:
name = gets.chomp.split
name.each do |x|
puts x.size
for i in 1..x.size do
puts i.max
end
end
Use a a variable which is initially an empty string.
max_name = ""
When you are inside the loop, check if each x.size is larger than max_name.size. If that is the case, you have found a new max_name, so do max_name = x.
The code fails when trying to get the maximum of the integer 1. That's an odd-looking guess at the correct code, and means you should probably revise how Ruby's blocks work (you appear to be expecting an interaction between the max and each that really doesn't exist).
The usual way to get the maximum of something from a list, if you are not allowed to use built-ins, is to set a "current maximum" value and then scan through the list, checking each item to see if it is larger than the current. If it is, set the current value to that instead. At the end, you will have the largest item.
name = gets.chomp.split
current_max = ''
name.each do |x|
if x.size > current_max.size
current_max = x
end
end
puts current_max