[resolved] Can string "+' convert to operator +

RT
can the string “2+5” convert to: 2 + 5 ,at last get the result :7
as string “2” can use “2”.to_i convert to integer 2
how to convert “+” to operator +

Yes, you could use eval function to achieve this,

Here you go

puts eval"2+5"

Raja gopalan wrote in post #1171255:

Yes, you could use eval function to achieve this,

Here you go

puts eval"2+5"

Thank you ,Raja gopalan it helped me

Here’s a way without using eval:

s = ‘2+5’
=> “2+5”

a = s.split(’’)
=> [“2”, “+”, “5”]

a[0].to_i.send( a[1], a[2].to_i )
=> 7

Great Idea Joel.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs