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 ?

  1. it has scopes. method body runs in scope/context of instance, class definition runs in own scope. use instance variables , initialize hash @directions in initialize method, in case hash not change, recommend using constant. in ruby these declared variables, when first character uppercase, constants.

  2. probably because variable never used, reasons detailed in (1)

  3. key never 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.

  4. 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

Popular posts from this blog

Python Kivy ListView: How to delete selected ListItemButton? -

asp.net mvc 4 - A specified Include path is not valid. The EntityType '' does not declare a navigation property with the name '' -