RPN Calculator Ruby matching in arrays -


i'm making reverse polish notation calculator. wrote out gameplan , trying fill in. i'm @ part want take integers in array , move them new array (using shift remove permanently array).

this should easy can't figure out. tried identifying integers-only things

integer.match(x) x.class == integer 

but that's not working. i've tried building pull that's not in operations array, like, 1 fails:

x.include?(operations) 

i new , not @ coding. if you'd berate me how code sloppy or posted wrong, has. i'm trying. thanks,

class rpncalculator     def initialize(problem)         @problem = problem     end      def resolve         array = @problem.split(" ")         @array = array         operations = ["+", "-", "*"]         @operations = operations         queue = []         @queue = queue          array.map! { |x| /[0-9]/.match(x) ? x.to_i : x}          array.each |x|            #this i'm building & failing               array.shift(x)             queue << x         end      end # iterate through array # if array elements don't match operations element, push them queue # once have match on function, launch method # make 3 new rpn methods, 1 each + - * # method launched should affect last 2 items in queue. # result of method gets pushed end of queue.  # return remaining value in queue answer  # placeholder method + # placeholder method - # placeholder method *  end  test = "1 3 +" mytest  = rpncalculator.new(test) mytest.resolve 

cj, want @ error messages.

integer.match(x)   #=> nameerror: undefined local variable or method `x' main:object 

ruby assuming match class method of class integer. before sending match , match's argument x integer evaluates x , finds neither method or local variable; hence exception. is, have not defined x.

had defined x (as method or local variable), ruby have given different error message:

integer.match(4)   #nomethoderror: undefined method `match' integer:class 

that's because had not defined class method class integer. if had written

class integer   def self.match(x)     2 * x   end end 

ruby have been happy clam:

integer.match(4) #=> 8 

let's move on method, rubyists write two-space indents.

def resolve   array = @problem.split(" ")   @array = array   operations = ["+", "-", "*"]   @operations = operations   queue = []   @queue = queue    array.map! { |x| /[0-9]/.match(x) ? x.to_i : x}    array.each |x|      #this i'm building & failing      array.shift(x)     queue << x   end      end 

there problem right away in first line:

array = @problem.split(" ")   #nomethoderror: undefined method `split' nil:nilclass 

what error message mean? first, let's see irb can tell receiver of method split:

@problem       #=> nil @problem.class #=> nilclass 

now once again @ error message. says have not defined method split instance nil of class nilclass. split defined class string (i.e., string#split), receiver of split must string.

there more problems code, difficult deal without foundation of comes before. hope comments may have cleared part of problem, however.

if working on problem exercise, think should stand , ask effective , efficient way learn ruby. think should concentrate on problems have been designed teach ruby principles, starting easy ones , progressing ones more challenging. there many ruby teaching sites on internet. , people them. free offer no mentor support. if feel benefit mentoring, recommend ruby learing's core ruby course. see new 1 begins later month. it's not free, it's not expensive. study notes course here.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -