I wanted to write a PnP assistant. I created a hash with stats and stat levels, and defined a method to level up those stats. This is my hash:
$stats = {
"HP" => 300,
"VIT" => 9,
"STR" => 10,
"DEX" => 15,
"SPD" => 8,
"INT" => 11,
"PSY" => 21,
"HW" => 2,
"MED" => 3,
"SCHW" => 0,
"GEN" => 12,
"RGEW" => 25,
"SELB" => 11,
"MKEN" => 19,
"WILL" => 23
}
Below is my method:
def level_stat (stat, amount = 1)
@string = stat.upcase
print "#{@string}: #{$stats[@string]} > "
$stats[@string] += 1 * amount
puts $stats[@string]
if (@string == "VIT")
$stats["HP"] += 5 * amount
print "#{"HP"}: #{$stats["HP"]} > "
puts $stats["HP"]
end
end
The default value for amount levels up the stat by one, but enables to do multiple level ups on the same stat at the same time (or so it's supposed to be). Calling this works as expected:
level_stat ("int")
However, calling the following statement throws an error:
level_stat ("vit", 2)
# >> syntax error, unexpected ',', expecting ')'
# level_stat ("vit", 2)
# ^
I do not understand why it's doing this.
Remove the space between level_stat and (, or alternatively remove the parentheses completely:
level_stat("vit", 2)
level_stat "vit", 2
By adding a space and parentheses, the ruby parser misinterprets your intention and raises a SyntaxError.
If you haven't already, take a quick look through a ruby style guide -- it would also be common practice to omit the space in the def:
def level_stat(stat, amount = 1)
# ...
end
Just for addition to answer of @Tom.
Look:
def twice(arg)
puts arg * 2
end
twice 5 #=> 10
twice(5) #=> 10
twice (5) #=> 10
def plus(arg1, arg2)
puts arg1 + arg2
end
plus 2, 2 #=> 4
plus(2, 2) #=> 4
plus (2, 2) #=> syntax error, unexpected ',', expecting ')'
Ruby parser thinks that (2, 2) is a single argument, but (2, 2) is not a valid Ruby expression.
Such a situation, when used more than 1 argument. As you see, there is no problem with one argument.
So follow codestyle. Guys gave you link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With