Ruby iteration error [closed] - ruby

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
When attempting to use iteration methods, I keep getting errors. This is an initialize method that creates a randomly sized array populated with random integers. Any help is appreciated.
def initialize
i = 0
#random_size = rand(3-12)
#new_arr = Array.new(#random_size)
loop do
#new_arr[i] = rand(1..50)
break if i >= #random_size
i += 1
end
end
Edit
The original question looked as below. Notice the rand(3-12).

In Ruby (and any language that has some functional capabilities, for that matter) you don't usually write explicit indexes, that's too imperative (and verbose). A functional approach would look something like this:
def initialize
#random_size = rand(3..12)
#new_arr = #random_size.times.map { rand(1..50) }
end

Related

ruby convert string to int without using 'to_i' [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 4 years ago.
Improve this question
So I'm defining a anonymous class to test a method like so:
let!(:s_integer) do
class << self
def change(value)
self(value)
end
end
def change(value)
self.class.change(value)
end
end
The problem is that where Integer('9') works, self('9') doesn't, but I can't use to_i so how do I work around this problem?
Integer(arg, base=0) is a method in Kernel, not in Integer. So you can't use self to call it.
def change(value)
Integer(value)
end

self referencing an object in its assignment [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 6 years ago.
Improve this question
I was wondering if there was a way to reference an object when assigning it to a variable. Here is an example of where this question would apply:
Let's say I wanted to assign the substring of a regex to a variable, call it i.
To assign, I could write
i = /some_regex/.to_s
and then
i = i[3...i.length]
I could also write it all in one line, like
i = /some_regex/.to_s[3.../some_regex/.to_s.length]
However, both of these examples seem somewhat redundant and the second approach could become unwieldy with big regex's or multiple method calls. Is there a way to reference the object being changed without having to rewrite everything?
Edit: Sorry for previous ambiguity.
Ruby evaluates the right side of the equals sign before setting the left side equal to it, so if i already exists you can do what you're talking about. A simple example:
i = 10
i = i + 1 # now i = 11
However, you can't use i to define itself. You could use the following two lines:
i = expression.match(/\d+[\+|-|\*|\/]/)
i = i[0..i.length - 1] # Note: this is the same as i = i[0...i.length]

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

Calling proc as a hash key 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 8 years ago.
Improve this question
Refers to - bizzare way of calling Procs?
#BroiSatse thanks a lot for the answer. One additional moment to clarify - what if one needs to pass extra arguments (added to the code as param_1 and _2):
def callbacks(param_1, param_2, procs)
procs[:var_1].call(param_1)
puts "Proceed"
procs[:var_2].call(param_2)
end
callbacks(arg_1, arg_2, :var_1 => Proc.new {block_1},
:var_2 => Proc.new {block_2})
What goes first? i.e. what will be passed first to execute the def callbacks- arguments (arg_1, arg_2) in place of params (param_1, param_2) or procs (:var_1, :var_2)? This is important to know to properly code the params line - def callbacks(param_1, param_2, procs).
Thanks in advance for any help.
I can't see how you think this will possibly make a difference here, but the arguments are evaluated from left to right and in YARV are pushed onto the stack in that order. Obviously, though, they are all passed before the called method starts executing.

Throw-catch usage instead other elements of ruby [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Where we should use throw-catch instead of other elements of ruby like methods, lambdas/procs, return, raise-rescue etc. in a way that we don't mess up our code (for example we don't need to look for catch/throw in other files/classes)? Or to put it into other words, where I can use throw-catch where I cannot (at least easily) use other forms?
For example, this code (from this answer):
catch (:done) do
1.upto(INFINITY) do |i|
1.upto(INFINITY) do |j|
if j>10
throw :done, :done
end
end
end
end
you can write as:
def fun1
1.upto(INFINITY) do |i|
1.upto(INFINITY) do |j|
if j>10
return :done
end
end
end
end
In a Ruby I don't like #2 - catch(:wtf) { throw :wtf } post, author showed that the catch is 4 level deeper than the throw. Later, s/he showed that it can be avoided using simple constructs.
So where is unique usage of throw-catch constructs that make our live easier?

Resources