Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete quotation marks in an array in Ruby

Tags:

ruby

rspec

I am working on Test First Ruby with rspec examples testing...

which I need to pass this test.

it "tokenizes a string" do
    calculator.tokens("1 2 3 * + 4 5 - /").should ==
      [1, 2, 3, :*, :+, 4, 5, :-, :/]
  end

And here is my code

def tokens(str)
    data = str.split(' ')
    outcomes = []
    data.collect do |x|
      if x.to_i != 0
        outcomes.push(x.to_i)
      elsif x.to_i == 0
        temp = x.gsub('"', '')
        outcomes.push(":#{temp}")
      end
    end
    outcomes
  end

However, I got these feedback. Have no idea how to get rid of the quotation mark.

Failure/Error: [1, 2, 3, :*, :+, 4, 5, :-, :/]                                                                                                                               
       expected: [1, 2, 3, :*, :+, 4, 5, :-, :/]                                                                                                                                  
            got: [1, 2, 3, ":*", ":+", 4, 5, ":-", ":/"] (using ==)   
like image 364
Yumiko Avatar asked Jul 01 '26 13:07

Yumiko


1 Answers

Try this:

outcomes.push(:"#{temp}")

":#{temp}" is string but this :"#{temp}" symbol with string interpolation.

=>:"+".class
#> Symbol
=> ":+".class
#> String
like image 118
Philidor Avatar answered Jul 04 '26 14:07

Philidor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!