Passing arguments through parameter 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 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.

Related

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

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)

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?

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.

refactoring ruby code inject issues [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
Hi can someone explain to me why my inject isn't working here?
Am I using inject correctly here? For some reason my code gets caught up in an endless loop once this scenario comes into play (the 5th move usually in my game)
def cpu_block_player
winning_combinations = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
executed = 0
winning_combinations.each do |combination|
result_of_combination = ""
result_of_combination = combination.inject("") {|result, element| result + #board[element]}
if result_of_combination == "XXe" || result_of_combination == "eXX" || result_of_combination == "XeX"
executed += 1
puts executed
player_move(#current_turn, result_of_combination.index("e"))
end
break if executed >= 1
end
First of all, these kinds of questions are better suited for the Code Review Stack Exchange site.
However, here are my thoughts:
My first thought when looking at your code is that you're just having one large class. In order to see some real advantages with Object-Oriented Programming, I'd recommend extracting some of the code into separate classes. I can definitely see a Board class inside the Game class, just waiting to be extracted.
Some ideas for methods to add to a Board class:
to_s -- This would be what's in your print_board method at the moment, without the print.
finished? -- Check whether the game is "finished" (ie., someone has won). A winner method would also make sense.
taken? -- Whether someone has taken a position before.
A lot of the code in your Game class would benefit from naming. For instance, take this piece of code:
#current_turn == #player_x ? #current_turn = #player_o : #current_turn = #player_x
It's not super hard to figure out what this piece of code does, but exactly how you swap who is the current player is probably not important to know while reading the player_move method. All you want to know is that "at this point, we switch players".
Extracting methods and objects doesn't make you write less code, but in my opinion it makes for clearer code. If you can give every piece of line a name (ie., extract it to a method), then it is probably a lot easier to figure out what is going on.

Resources