Calling proc as a hash key in Ruby [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 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.

Related

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

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?

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 iteration error [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
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

Resources