Why doesn't this ruby code do anything? [closed] - ruby

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)

Related

Passing arguments through parameter in Ruby [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 5 years ago.
Improve this question
I am passing an argument using implicit return.
Point out what's wrong please, Ruby does not provide any feedback error for the implicit return, it is just blank with no response.
def add(a, b)
a + b
end
def subtract(a, b)
a - b
end
add(20, 45)
subtract(80, 10)
I know it works in terminals. Is that the only way to work this code? I know the puts way to make this work using code editor that forces an implicit return that is not nil.Trying to do the same with this method.
If you're using a read-evaluate-print-loop (REPL) like irb then you'll see the output of your code as you type it in. If you're in a code editor you probably will not.
Here's how to get some output from that code:
def add(a, b)
a + b
end
def subtract(a, b)
a - b
end
p add(20, 45)
p subtract(80, 10)
Putting p before any given thing will give you a quick inspect (debug) view into the object in question. Normally Ruby will just throw away any results in a void context like this, you're not asking it to preserve the results of these method calls anywhere, nor display it in any form, which is why there's no output.
I have my editor configured to run Ruby code with the push of a button, so maybe yours has an option to do that as well. Most do it in some form but it may require some configuration.

undefined method error in main:Object [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
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

Anyone can comment this ruby code? [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 total novice in ruby, i came across this code in an article about a bug in gmail:
(0..0xFFFFFFFFFF).each do |i|
puts "#{"%010X" % i}"
end
it is supposed to generate an dictionary, but i can't figure out how it works
Thank You all!
The code iterates and prints all values from 0 to 0xFFFFFFFFFF Similar to how
(1..10).each do |i|
puts i
end
iterates and prints all values from 1 to 10.
For each value between 0 and 0xFFFFFFFFFF it simply prints out its current hex value:
0000000000
...
0000005E6A
0000005E6B
0000005E6C
0000005E6D
0000005E6E
0000005E6F
...
FFFFFFFFFF

exception in if statement [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 9 years ago.
Improve this question
I wrote a searcher function in ruby for a class that iterates on a list of people (named #personList).
It faces an exception.
My Code‌ :
def search (nCode)
for x in #personList
if x.nCode == nCode
x.to_s
end
end
I wrote the same code using each, it also faced the same exception.
What's wrong with my code ?
(I'm new to Ruby! and I couldn't solve that)
This if is delimited by a pair of newlines:
x.to_s if x.nCode == nCode
This if needs an explicit end:
if x.nCode == nCode
x.to_s
end
You have opened three blocks (def, for..in, if) in your first sample, but only closed two. Your indentation indicates the if is the one you didn't close. Thus, you have a syntax error. Otherwise, both samples are identical in functionality.
Ruby if requires an end to make it complete. If you use end for your exception code, it will work.
if x.nCode == nCode
x.to_s
end
For second case, that worked or you, it is actually completes the condition and the execution in one line. That's why it didn't require any end, and it worked for you.
You'll find more detail about if from here.

Ruby String/Array Write program [closed]

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

Resources