Ruby condition to increment one variable or the other - ruby

Lets say I have to variables in Ruby for a little game
times_won # Any integer
times_lost # Any integer
and the result of the last round
result # string, can be: "won", "lost" or "drawn"
What would be a nicer way of doing this
times_won += 1 if result == "won"
times_lost += 1 if result == "lost"
I'm trying with ternary operator but I dont know how to use it with multiple variables. Also a case statement isn't much nicer.
Maybe anyone knows a one-liner to blow my mind.
EDIT:
Where is the Error in a nested ternary operator like this?
times_won,times_lost += result == 'drawn' ? [0,0] : result =='won' ? [1,0] : [0,1]

You could use a hash:
#counter = {won: 0, lost: 0, drawn: 0}
result = :won
#counter[result] += 1
#counter
#=> {:won=>1, :lost=>0, :drawn=>0}
I've used symbols as keys, but it also works with string.
If other parts of your code rely on times_won and times_lost, you could implement them as methods:
def times_won
#counter[:won]
end
Regarding your edit
Mixing abbreviated assignment and multiple assignment doesn't work:
a, b += 1, 2 #=> SyntaxError
You have to specify the calculation explicitly:
a, b = a + 1, b + 2 # increments `a` by 1 and `b` by 2
And of course you can replace the fixed values with a more complex expression:
a, b = a + (result == 'won' ? 1 : 0), b + (result == 'lost' ? 1 : 0)

Related

Dynamic constant assignment main.rb:6: Ruby [duplicate]

This question already has answers here:
Dynamic constant assignment
(7 answers)
Closed 2 years ago.
I get that error while naming variables in my loop. The point of the method is to print the index position of the only element with unique parity in the array. For example the method should print "3" for the following input array because it is the only odd number and everything else is even: [2, 4, 6, 3, 8, 10]. More specifically, it points out the error to "Odd" and "Even" variables below ("main.rb:7: dynamic constant assignment Odd = numbers[i] % 2 == 1").
def test(numbers)
i=1
countOdd = 0
countEven = 0
Odd = numbers[i] % 2 == 1
Even = numbers[i] % 2 == 0
while i < numbers.length
if Odd
countOdd += 1
else countEven +=1
end
i+=1
end
if countEven == 1
print Odd.index
else print Even.index
end
end
When you define a Capitalized variable in Ruby, that is a constant - it is a special kind of variable that is not allowed to change value (well, technically you can change it with const_set, but that's not really relevant here).
Because of this limitation, Ruby won't allow you to change constants from within functions. It assumes the function will be called many times, which would cause the constant to change value, which as I just mentioned is illegal.
So, quick fix, just replace your Odd and Even with the lowercase versions odd and even. That way they're regular variables and not constants.
the above code could be return easily using the built in array methods:
even_numbers = numbers.select(&:even?)
odd_numbers = numbers.select(&:odd?)
then the even counts will be even_numbers.count, and similarly odd_numbers.count
the first odd number index will be numbers.find_index(odd_numbers.first)
for the constants you can assign a proc to the constant :
Odd = Proc.new{ |n| n%2 == 1 } then call the constant like this: Odd.call(10) #=> false, similarly you can define a proc for even numbers.
what happening here is every time the method get called the constant will be redefined thus having a new value, but constants are meant to hold a value that would not change, and that is the cause of the error.
also note that the following condition in your method:
if Odd
then do something
....
is not actually correct. once the constant holds a value then it will have that value during execution. so after doing Odd = some_integer % 2 == 1 the Odd constant will be either true or false always. and will not re-execute some_integer % 2 == 1 in your if condition. but if you make it a proc or lamda it well re-do the calculation because it wil be as if you are calling a method.
[NOTE] however doing Odd = Proc.new{ |n| n%2 == 1 } inside your method will still give you the same error dynamic constant assignment Odd = Proc.new { |n| numbers[n] % ... since still the code is re-executed every time the method runs giving the constant different values each time. so by putting Odd = Proc.new{ |n| n%2 == 1 } definitions outside of the method then doing if Odd.call(i) it will work just fine.

Recursive Fibonacci in Ruby

This week is my first time doing recursion. One of the problems I was able to solve was Fibonacci's sequence to the nth number; it wasn't hard after messing with it for 5 minutes.
However, I am having trouble understanding why this works with the current return statement.
return array if num == 2
If I push to array, it doesn't work, if I make a new variable sequence and push to that, it returns the correct answer. I am cool with that, but my base case says return array, not sequence. I initially pushed the sequence to the array, the result was not fibs sequence. I only solved the problem when I tried seeing what would happen if I pushed to the sequence array.
Instead of just making it work I was hoping someone could explain what was happening under the hood, what the stacks might be and how the problem works.
I understand recursion to an extent and somehow intuitively can make it work by assuming things, but I feel funny not actually knowing all the whys behind it.
def fib_seq(num)
return [0] if num == 1
return [] if num == 0
array = [0, 1]
return array if num <= 2
seq = fib_seq(num - 1)
seq << seq[-2] + seq[-1]
end
The code can be simplified a bit by removing the temporary array variable. It's a distraction. It also only applies when num == 2; num < 2 will be handled by the other base cases. num < 0 is illegal and should be handled by an error check.
I've also added in an explicit return. Explicit returns make it very obvious what's being returned and that helps understand recursion. In this case it's seq. ("Explicit returns are evil!" all the Ruby style people cry. Tough cookies. Good style isn't an absolute.)
def fib_seq(num)
# Error check
if num < 0 then
raise ArgumentError, "The number must be a positive integer"
end
# Terminating base cases
return [] if num == 0
return [0] if num == 1
return [0,1] if num == 2
# Recursion
seq = fib_seq(num - 1)
# The recursive function
seq << seq[-2] + seq[-1]
return seq
end
Now it's a bit clearer that return [0,1] if num == 2 is one of three base cases for the recursion. These are the terminating conditions which stops the recursion. But processing doesn't end there. The result isn't [0,1] because after that first return the stack has to unwind.
Let's walk through fib_seq(4).
fib_seq(4) calls fib_seq(3)
fib_seq(3) calls fib_seq(2)
fib_seq(2) returns `[0,1]`
We've reached the base case, now we need to unwind that stack of calls.
The call to fib_seq(3) picks up where it left off. seq returned from fib_seq(2) is [0,1]. It adds seq[-2] + seq[-1] onto the end and returns [0,1,1].
fib_seq(4) picks up where it left off. seq returned from fib_seq(3) is [0,1,1]. It adds seq[-2] + seq[-1] to the end and returns [0,1,1,2].
The stack is unwound, so we get back [0,1,1,2].
As you can see, the actual calculation happens backwards. f(n) = f(n-1) + f(n-2) and f(2) = [0,1]. It recurses down to f(2), the base case, then unwinds back up doing f(3) using the result of f(2), and f(4) using the result of f(3) and so on.
Recursive functions need to have an exit condition to prevent them from running forever. The main part of your recursive method is the following:
seq = fib_seq(num - 1)
seq << seq[-2] + seq[-1]
In Ruby, the last expression of a method is considered to be the return value of that method, so the lines above are equivalent to:
seq = fib_seq(num - 1)
seq << seq[-2] + seq[-1]
return seq
Let's run down what would happen if the method only contained these two lines, with num = 4:
call fib_seq(4)
call fib_seq(3)
call fib_seq(2)
call fib_seq(1)
call fib_seq(0)
call fib_seq(-1)
...
Obviously this results in an infinite loop, since we have no exit condition. We always call fib_seq again on the first line, so the code has no chance of ever reaching the return statement at the end. To fix the problem, let's add in these two lines at the beginning:
array = [0, 1]
return array if num <= 2
These can be simplified down to just:
return [0, 1] if num <= 2
Now let's see what happens when we call the method with num = 4:
call fib_seq(4)
4 > 2, exit condition not triggered, calling fib_seq(n - 1)
call fib_seq(3)
3 > 2, exit condition not triggered, calling fib_seq(n - 1)
call fib_seq(2)
2 == 2, exit condition triggered, returning [0, 1]!
fib_seq(2) returned with seq = [0, 1]
add 0 + 1 together, push new value to seq
seq is now [0, 1, 1]
return seq
fib_seq(3) returned with seq = [0, 1, 1]
add 1 + 1 together, push new value to seq
seq is now [0, 1, 1, 2]
return seq
FINAL RESULT: [0, 1, 1, 2]
So it looks like this method is working for values of num that are >= 2:
def fib_seq(num)
return [0, 1] if num <= 2
seq = fib_seq(num - 1)
seq << seq[-2] + seq[-1]
end
There is one bug left: num = 0 and num = 1 both return [0, 1]. Let's fix that:
def fib_seq(num)
return [] if num == 0
return [0] if num == 1
return [0, 1] if num == 2
seq = fib_seq(num - 1)
seq << seq[-2] + seq[-1]
end
Clean it up a little:
def fib_seq(num)
return [0, 1].first(num) if num <= 2
seq = fib_seq(num - 1)
seq << seq[-2] + seq[-1]
end
I always find it confusing when people mix imperative style mutations with functional style recursion – if you're going to do all reassignment and manual array seeking, why bother with using recursion as the looping mechanism? just use a loop.
That's not to say this program can't be expressed in a more functional way, tho. Here, we separate concerns of computing fibonacci numbers and generating a sequence – the result is an extremely easy-to-understand program
def fib n
def aux m, a, b
m == 0 ? a : aux(m - 1, b, a + b)
end
aux n, 0, 1
end
def fib_seq n
(0..n).map &method(:fib)
end
fib_seq 10
#=> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
And another way that's a bit more efficient for generating the sequence specifically – Below, I define an axuiliary function aux that utilizes 4 state variables to generate the sequence in a relatively straightforward way.
Note the difference with the input 10 - this one is closer to your proposed function where 0 returns [] despite the 0th fibonacci number is actually 0
def fib_seq n
def aux acc, m, a, b
m == 0 ? acc << a : aux(acc << a, m - 1, b, a + b)
end
case n
when 0; []
when 1; [0]
when 2; [0,1]
else; aux [0,1], n - 3, 1, 2
end
end
fib_seq 10
# => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

I don't understand this method

I'm a beginner in Ruby and I don't understand what this code is doing, could you explain it to me, please?
def a(n)
s = 0
for i in 0..n-1
s += i
end
s
end
def defines a method. Methods can be used to run the same code on different values. For example, lets say you wanted to get the square of a number:
def square(n)
n * n
end
Now I can do that with different values and I don't have to repeat n * n:
square(1) # => 1
square(2) # => 4
square(3) # => 9
= is an assignment.
s = 0 basically says, behind the name s, there is now a zero.
0..n-1 - constructs a range that holds all numbers between 0 and n - 1. For example:
puts (0..3).to_a
# 0
# 1
# 2
# 3
for assigns i each consecutive value of the range. It loops through all values. So first i is 0, then 1, then ... n - 1.
s += i is a shorthand for s = s + i. In other words, increments the existing value of s by i on each iteration.
The s at the end just says that the method (remember the thing we opened with def) will give you back the value of s. In other words - the sum we accumulated so far.
There is your programming lesson in 5 minutes.
This example isn't idiomatic Ruby code even if it is syntactically valid. Ruby hardly ever uses the for construct, iterators are more flexible. This might seem strange if you come from another language background where for is the backbone of many programs.
In any case, the program breaks down to this:
# Define a method called a which takes an argument n
def a(n)
# Assign 0 to the local variable s
s = 0
# For each value i in the range 0 through n minus one...
for i in 0..n-1
# ...add that value to s.
s += i
end
# The result of this method is s, the sum of those values.
s
end
The more Ruby way of expressing this is to use times:
def a(n)
s = 0
# Repeat this block n times, and in each iteration i will represent
# a value in the range 0 to n-1 in order.
n.times do |i|
s += i
end
s
end
That's just addressing the for issue. Already the code is more readable, mind you, where it's n.times do something. The do ... end block represents a chunk of code that's used for each iteration. Ruby blocks might be a little bewildering at first but understanding them is absolutely essential to being effective in Ruby.
Taking this one step further:
def a(n)
# For each element i in the range 0 to n-1...
(0..n-1).reduce |sum, i|
# ...add i to the sum and use that as the sum in the next round.
sum + i
end
end
The reduce method is one of the simple tools in Ruby that's quite potent if used effectively. It allows you to quickly spin through lists of things and compact them down to a single value, hence the name. It's also known as inject which is just an alias for the same thing.
You can also use short-hand for this:
def a(n)
# For each element in the range 0 to n-1, combine them with +
# and return that as the result of this method.
(0..n-1).reduce(&:+)
end
Where here &:+ is shorthand for { |a,b| a + b }, just as &:x would be short for { |a,b| a.x(b) }.
As you are a beginner in Ruby, let's start from the small slices.
0..n-1 => [0, n-1]. E.g. 0..3 => 0, 1, 2, 3 => [0, 3]
for i in 0.. n-1 => this is a for loop. i traverses [0, n-1].
s += i is same as s = s + i
So. Method a(n) initializes s = 0 then in the for loop i traverse [0, n - 1] and s = s + i
At the end of this method there is an s. Ruby omits key words return. so you can see it as return s
def a(n)
s = 0
for i in 0..n-1
s += i
end
s
end
is same as
def a(n)
s = 0
for i in 0..n-1
s = s + i
end
return s
end
a(4) = 0 + 1 + 2 + 3 = 6
Hope this is helpful.
The method a(n) calculates the sums of the first n natural numbers.
Example:
when n=4, then s = 0+1+2+3 = 6
Let's go symbol by symbol!
def a(n)
This is the start of a function definition, and you're defining the function a that takes a single parameter, n - all typical software stuff. Notably, you can define a function on other things, too:
foo = "foo"
def foo.bar
"bar"
end
foo.bar() # "bar"
"foo".bar # NoMethodError
Next line:
s = 0
In this line, you're both declaring the variable s, and setting it's initial value to 0. Also typical programming stuff.
Notably, the value of the entire expression; s = 0, is the value of s after the assignment:
s = 0
r = t = s += 1 # You can think: r = (t = (s += 1) )
# r and t are now 1
Next line:
for i in 0..n-1
This is starting a loop; specifically a for ... in ... loop. This one a little harder to unpack, but the entire statement is basically: "for each integer between 0 and n-1, assign that number to i and then do something". In fact, in Ruby, another way to write this line is:
(0..n-1).each do |i|
This line and your line are exactly the same.
For single line loops, you can use { and } instead of do and end:
(0..n-1).each{|i| s += i }
This line and your for loop are exactly the same.
(0..n-1) is a range. Ranges are super fun! You can use a lot of things to make up a range, particularly, time:
(Time.now..Time.new(2017, 1, 1)) # Now, until Jan 1st in 2017
You can also change the "step size", so that instead of every integer, it's, say, every 1/10:
(0..5).step(0.1).to_a # [0.0, 0.1, 0.2, ...]
Also, you can make the range exclude the last value:
(0..5).to_a # [0, 1, 2, 3, 4, 5]
(0...5).to_a # [0, 1, 2, 3, 4]
Next line!
s += i
Usually read aloud a "plus-equals". It's literally the same as: s = s + 1. AFAIK, almost every operator in Ruby can be paired up this way:
s = 5
s -= 2 # 3
s *= 4 # 12
s /= 2 # 6
s %= 4 # 2
# etc
Final lines (we'll take these as a group):
end
s
end
The "blocks" (groups of code) that are started by def and for need to be ended, that's what you're doing here.
But also!
Everything in Ruby has a value. Every expression has a value (including assignment, as you saw with line 2), and every block of code. The default value of a block is the value of the last expression in that block.
For your function, the last expression is simply s, and so the value of the expression is the value of s, after all is said and done. This is literally the same as:
return s
end
For the loop, it's weirder - it ends up being the evaluated range.
This example may make it clearer:
n = 5
s = 0
x = for i in (0..n-1)
s += i
end
# x is (0..4)
To recap, another way to write you function is:
def a(n)
s = 0
(0..n-1).each{ |i| s = s + i }
return s
end
Questions?

Array element access in Ruby

I was trying to create a Table of Contents in the command line. Each element in the array is a string variable.
arr = [chap1, chap1_page, chap2, chap2_page, chap3, chap3_page]
x = 0
until x == arr.length
if ((arr[x] != 0 ))
puts arr[x].ljust(line/2) + arr[x += 1].rjust(line/2)
end
x += 1
end
I was wondering if someone could explain the second half of the puts statement. I'm not sure why arr[x+=1] works but arr[x+1] does not. As far as I know, they are the same, are they not?
When dealing with Enumerables like arrays and hashes, it's useful to search the documentation to see if there's something there that will make your code higher level and more expressive. In this case, you can use each_cons to give you the pairs so you don't need to use array indexes at all:
2.3.0 :004 > [1,2,3,4].each_cons(2).to_a
=> [[1, 2], [2, 3], [3, 4]]
Also, rather than using if statements, it's better IMO to use select and reject.
Also, intermediate local variables can make your code more readable.
Using these ideas, your code could look something like this:
array = [chap1, chap1_page, chap2, chap2_page, chap3, chap3_page]
width = line / 2
array.each_cons(2).reject { |x,y| x == 0 }.each do |left, right|
puts left.ljust(width) + right.ljust(width)
end
(I haven't tested this code, but it shows the general idea.)
You could break down those enumerable calls and assign intermediate values to local variables if that makes it clearer for you:
array = [chap1, chap1_page, chap2, chap2_page, chap3, chap3_page]
width = line / 2
pairs = array.each_cons(2)
nonzero_pairs = pairs.reject { |x,y| x == 0 }
nonzero_pairs.each do |left, right|
puts left.ljust(width) + right.rjust(width)
end
x + 1 returns that value and has no side effect (does not change the reference of x). x += 1 reassigns x and returns that value.

(Ruby) how to return a -1 specifically for a specific comparison

let's say when I'm comparing values in ruby, i have a value in mind that no matter what I want, using sort on that value and anything else returns a -1 (so this value is default sorted as smaller than everything).
for example, let's say i want '100' to sort smaller 100% of the time against 99. so that if i'm sorting values in an array, and a comparison comes up between 100 and 99, 100 is sorted smaller (ie, -1 is returned). but, i want all the other cases to be normal (98 is smaller than 99, 50 is bigger than 30, etc)
edit: okay this is what i want
if i have an x and a y, i do not want to use
x <=> y
i want to use (in pseudocode and hand-wavy-ness)
x > y
which means, this x is always greater than this y
Why don't you instead use a dictionary to keep values associated with their relative value? In this case, the string abc can be mapped to -1, and then just make sure no other values map to values equal to or less than -1.
Edit: If you're only concerned with one particular value breaking the norm, then this solution is not for you.
Easier to handle the specialness outside of the sort!
module Enumerable
def sort_excluding(*vals)
special,rest = partition {|x| vals.include?(x)}
rest.sort + special
end
end
One way to do it would be to implement a derivative class for your custom comparisons (http://www.ruby-doc.org/core/classes/Comparable.html)
Here's some sample code (and tests) for you:
class StrA < String
include Comparable
attr :str
def <=>(anOther)
if (str == "abc" && anOther.str == "abc")
0
elsif (str == "abc")
-1
elsif (anOther.str == "abc")
1
else
str <=> anOther.str
end
end
def initialize(str)
#str = str
end
def inspect
#str
end
end
And the tests:
a = StrA.new("Z")
b = StrA.new("B")
c = StrA.new("abc")
d = StrA.new("")
a > b # 1
a > c # 1
c > a # -1
d > c # 1
c > d # -1
c < d # 1
c > d # -1
[a, b, c, d].sort! # [ "Z", "B", "", "abc"]
I think what you want is:
[30, 50, 4, 0, 100, -22, 99].sort_by {|a| [a == 100 ? -1 : 0, a ]}.reverse
which gives:
99
50
30
4
0
-22
100
Hope I understood the question!
Array#sort or Enumerable#sort(I don't know what you are trying to sort) can take an obtional block. If the block is given, the block is used for comparison instead of <=>
For example this code will sort reversed:
foo.sort { |a,b| b <=> a }
In your case you need to call #sort something like the following:
foo.sort do |a,b|
if [a,b].sort == [99,100]
b-a # returns 1 or -1 so that 99 > 100
else
a <=> b
end
end
I am not entirely sure about the way you are trying to sort, but this should enable you to use sort in the manner you need. More inforamtion about Array#sort, and any other method can be found on your linux(and possible other OS's) via ri like this: ri Array#sort.
You could override the sort method for the object like so:
class Fixnum
alias old_sort <=>
def <=>(object)
if (self == 100 or object == 100)
return -1
else
return self.old_sort object
end
end
end
puts (101 <=> 100)
Edit 1: Above is a fully working example.
Edit 2: As stated by johannes in the comments, you really shouldn't implement this exact code. This is merely a simple example of how to override your sorting method to do domain-specific logic. Also, updated the code to reflect johannes' other comment about comparing with both object and self.

Resources