This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I wrote this code:
def sumeven (i)
result = 0
while i < 100
if i % 2 == 0
result += i
end
i += 1
end
result
end
How do I give the i for the equation? When I run the equation in the terminal (mac), I get no output because I'm unable to figure out how to substitute the i for a number. Substituting the "i" with a number in the text editor still gives no output.
Try:
def sumeven(i)
# method implementation...
end
# call the method, passing it an argument
sumeven(4)
If this actually answers your question, may I suggest taking a tiny step back and reading a book on ruby before you get too much farther? You are asking why the car doesn't go before you know how to use the gas pedal.
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am experimenting on arrays in Ruby. I cannot access the maximum nested array count. Example
experiment is shown below:
array = [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
So, do you know a limit of dimensions of array in Ruby?
The limit is when you run out of memory.
Per commenter #HunterMcMillen's idea, you could test it like so:
# Warning: be prepared for a sad computer!
depth=0; arr=[]; a=arr
begin
while true do
b = []
a.push(b)
a = b
depth += 1
end
rescue => e
puts "OK: depth=#{depth}, error=#{e} (#{e.class})"
end
Presumably at some point it will raise some error (e.g. out of memory) and you'll see how many arrays you were able to nest.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Is there any method available in ruby 1.8.6 for calculating n values n power?
Like 2 has 5 power then answer is 32 and 5 has 3 power then answer is 125?
2**5 will give you the result 32.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Today I had a really hard time programming in ruby. I came upon this question, and I was really stumped.
Print a sequence of number pairs "x,y" such that x ranges from 0 to 3 and y from 5 to 7 *
I already submitted my answer, but I'm not quite sure if I got it right. I'd just like my question answered now for the sake of understanding.
Something simple like
p [*0..3].product([*5..7])
0.upto 3 do |x|
5.upto 7 {|y| puts x, y}
end
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Trying to debug what may be a require/load problem in my environment (just printing $" & $: was not enough) I was hoping to wrap the load method, but my expected print statements do not execute, so I assume that the following did not work as I had hoped
module Kernel
alias :old_load :load
def load(fname, wrap=false)
puts "loading #{fname}"
old_load fname, wrap
end
end
So is it generally possible to wrap kernel methods (using aliases or otherwise)? If so whats would be the right syntax?
Thanks
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
Mathematica 8.0.1 on Mac OS X 10.7.2 does this: Quit the kernel and execute
MathieuS[MathieuCharacteristicA[ 1, -(1/4)], -0.25`, 15.707963267948966`]
MathieuS[MathieuCharacteristicA[ 1, -(1/4)], -0.25`, 15.707963267948966`]
(*
5.10119 10^-15
MathieuS[MathieuCharacteristicA[1, -(1/4)], -0.25, 15.708]
*)
That is, the first time it numerically evaluates the expression, while the second time it returns the unevaluated form.
Why? Or have I just spent too long staring at my screen and am doing something stupid?