Understand better the If statement


#1

Hiii everyone, another Nubby question from your favorite Italian writer!

I was looking from understing how to use the if statement here in sonic PI:
I did this test:
define :randi do
rrand(0.50, 0.75)
if < 0.60 do
choose([0.60, 0.45, 0.64895, 0.51, 0.69785)
else do
choose([0.75, 0.978, 1., 0.8532689)]
end
end
end

" define :randi do
rrand(0.50, 0.75)
if < 0.60 do
choose([0.60, 0.45, 0.64895, 0.51, 0.69785)
else do
choose([0.75, 0.978, 1., 0.8532689)]
end
end
end"

And it gives me this error: "Syntax Error: [buffer 0, line 6]
syntax error, unexpected ‘<’
[Line 6]: if "

Can someone help to understand better this language?


#2

Hey! This error is perfectly normal. Here are a few things you can change:

“<” is a comparison operator. You need to compare something to something.
blabla < blabla2. In your code, there is nothing at the left of the comparison.

Concerning your if statement, here is what you should do:

a = 1 ; b = 2
if (a < b)
  puts "hey guys"
else
  puts "oh no"
end

You need to store your randomised value in a variable for instance, before comparing to something else. Be sure to look closely at the syntax used in the documentation because some things are just rigid and you need to follow strictly the syntax if you want your code to run smoothly.

The computer is just made to follow strict instructions. I think that your problem here was to assume that the computer would understand that you were trying to compare a value to the value you just created before, but the computer knows nothing about that.

EDIT: Here is a valid version of your code.

define :randi do
  a = rrand(0.50, 0.75)
  if a < 0.60
    b = [0.60, 0.45, 0.64895, 0.51, 0.69785].choose
  else
    b = [0.75, 0.978, 1, 0.8532689].choose
  end
end

#3

Thank you so much @Bubo, I tried this one:

define :ciao do
if (a < b)
puts choose([0.60, 0.45, 0.64895, 0.51, 0.69785])
else
puts choose([0.75, 0.978, 0.678, 0.8532689])
end
end

but now, when i’m trying to recall in some parameters it gives me another error:
"Runtime Error: [Buffer 0, line 17] - RuntimeError
Thread death ±->: live:_loop_bach
Unable to normalise argument with key :amp and value [[1, “0.75”]]


#4

I have used puts in my example because I was not intending to do anything except show you the result of the if statement in the console. You don’t need puts because it’s used only when you want to print something in the console. Usually, you don’t.

I don’t know exactly what is happening with your code right now, but I assume that you must have written something like this:

play :c4, amp: ciao

If you did so, the crash is perfectly normal. You are feeding a “string” to your parameter. This is not a format that can be used. You need to return a float or integer number. Remove the puts and that should do the work.

a = 4
define :hey do
  if a > 2
    [0.60, 0.20].choose
  end
end

play :c4, amp: hey

This is only a personal preference, but I think that using the other syntax for choose is better because the code is more readable. Instead of choose([5, 1, 2, 3]) , try to do [5, 1, 2, 3].choose. I don’t know much about Ruby, but I guess that it’s another way to call a method, and I do prefer the latter.


#5

Thank you again @Bubo you’re so kind!
But I was wondering if before the object “choose”, with that dot it change is property? What it becomes?


#6

This is absolutely the same thing. They don’t have the same syntax but they have the exact same effect.


#7

I owe you! Thank you thank you!


#8

Hi,

You’ve got the switch statement as well if you want to check a few different things e.g.

a = 50

case a
when 1
  puts "It's 1"
when 3
  puts "It's 3"
when 4
  puts "It's 4"
else
  puts "It's something else"
end

# different type of case
case
when a > 100
  puts "It's greater than 100"
when a == 50
  puts "It's 50"
else
  puts "It's something else"
end