rubymine - Ruby: Undefined method -
i having issue first ruby program hashes. error saying
'throw': undefined local variable or method 'directions' .......
here code:
class die    directions = {        north: 1,       south: 2,       east: 3,       west: 4   }     def throw        direction = directions.select{ |key ,value | value == rand(4)+1}       puts direction      end end   dice = die.new  dice.throw question 1
how how fix error?
question 2
ruby-mine has zig-zag line under hash directions , gives option remove assigment why this?
question 3
there zig-zag under 'key' , offers convert "to block" why ?
- it has scopes. method body runs in scope/context of instance, class definition runs in own scope. use instance variables , initialize hash - @directionsin initialize method, in case hash not change, recommend using constant. in ruby these declared variables, when first character uppercase, constants.
- probably because variable never used, reasons detailed in (1) 
- keynever used, can ignore using variable name starts underscore (- _key) or underscore do. use first form, know what's in there when come later piece of code.
- it wants convert block because of better readability. 
full code:
class die   directions = {        north: 1,       south: 2,       east: 3,       west: 4   }    def throw     direction = directions.select |_key, value|       value == rand(4)+1     end     puts direction   end end 
Comments
Post a Comment