Converting data types in arrays with multiple data types

I would go with this

data = [
  ['+', '609'],
  ['*', '48'],
  ['+', '345']
]

def convert(data)
  if data.respond_to?(:map)
    data.map(&method(:convert))
  else
    data.to_s =~ /^\d/ ? data.to_i : data
  end
end

···

2016-11-16 21:46 GMT+02:00 Micky Scandal <mickyscandal@gmail.com>:

    irb(main):002:0> a.map {|x,y| [x, Integer(y)]}
Robert, thanks for this solution! this is where my thoughts were going
before I asked the question, but I must have done something wrong because it
threw an error for me. but again, thank you! that actually cleared a lot up
for me. for more than just this exorcize too. thanks again everyone!

On Tue, Nov 15, 2016 at 11:27 PM, Robert Klemme <shortcutter@googlemail.com> > wrote:

On Wed, Nov 16, 2016 at 2:08 AM, Àbéjídé Àyodélé >> <abejideayodele@gmail.com> wrote:
> [["+", "609"], ["*", "48"], ["+", "345"]].map { |x| x.map { |y| y.to_i
> == 0
> && y != "0" ? y : y.to_i }}
>
> #=> [["+", 609], ["*", 48], ["+", 345]]

Since we seem to know the structure this seems overly complex. What about

irb(main):001:0> a = [["+", "609"], ["*", "48"], ["+", "345"]]
=> [["+", "609"], ["*", "48"], ["+", "345"]]
irb(main):002:0> a.map {|x,y| [x, Integer(y)]}
=> [["+", 609], ["*", 48], ["+", 345]]

Kind regards

robert

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

joining the fxnal fun : ),

def convert2 data
   data.map(&method(:convert2))
rescue
   begin
      Integer data
   rescue
      data
   end
end

best regards
--botp

···

On Thu, Nov 17, 2016 at 5:38 PM, Michael Lutsiuk <michael.lutsiuk@gmail.com> wrote:

data = [
  ['+', '609'],
  ['*', '48'],
  ['+', '345']
]

def convert(data)
  if data.respond_to?(:map)
    data.map(&method(:convert))
  else
    data.to_s =~ /^\d/ ? data.to_i : data
  end
end

That's a really broad rescue clause..

- Thomas Perkins

···

On Nov 17, 2016, at 11:50 AM, botp <botpena@gmail.com> wrote:

On Thu, Nov 17, 2016 at 5:38 PM, Michael Lutsiuk > <michael.lutsiuk@gmail.com> wrote:

data = [
['+', '609'],
['*', '48'],
['+', '345']
]

def convert(data)
if data.respond_to?(:map)
   data.map(&method(:convert))
else
   data.to_s =~ /^\d/ ? data.to_i : data
end
end

joining the fxnal fun : ),

def convert2 data
  data.map(&method(:convert2))
rescue
  begin
     Integer data
  rescue
     data
  end
end

best regards
--botp

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

indeed. recursion is heavy and so is exception handing.

but the beauty of michael's soln is that we dont have to think a lot
about the structure, we just know that it responds to map or integer.

otherwise, if its just the compactness/cleanness of the form we're
concerned of, ruby always shines. other possible forms follow..

def convert data
   data.map(&method(__method__))
rescue
   Integer data rescue data
end

def convert2 data
  data.map(&method(__method__)) rescue (
     Integer data rescue (
        data
     )
  )
end

def convert3 data
  data.map(&method(__method__)) rescue
  Integer data rescue
  data
end

def convert4 data
  data.map(&method(__method__)) rescue Integer data rescue data
end

best regards --botp

ps: when it comes to recursion, i still prefer the old long form since
it's easy to insert debugging statements.

···

On Fri, Nov 18, 2016 at 5:17 AM, thomas Perkins <thomas.perkins23@icloud.com> wrote:

That's a really broad rescue clause..