Converting data types in arrays with multiple data types

Hey all, I've run into a bit of a snag. I have an array something like this:
[["+", "609"], ["*", "48"], ["+", "345"]]
What I need to do is go through and convert the numbers to integers while
leaving the symbols (+, *) as strings. Ive I've tried a couple different
variations of this:
input = Array.new(File.read("input.txt").split("\n").map {|l| l.split("
")}).map {|e| e[-1].to_i}

and the txt file is formatted like:
+ 609
* 48
+ 345
etc...

Like I said, I tried a couple different variations of the code above,
mostly just changing the last map block, but nothing I can think of is
working. any help please!?!?

[["+", "609"], ["*", "48"], ["+", "345"]].map { |x| x.map { |y| y.to_i == 0
&& y != "0" ? y : y.to_i }}

#=> [["+", 609], ["*", 48], ["+", 345]]

Abejide Ayodele
It always seems impossible until it's done. --Nelson Mandela

···

On Tue, Nov 15, 2016 at 6:59 PM, Micky Scandal <mickyscandal@gmail.com> wrote:

Hey all, I've run into a bit of a snag. I have an array something like
this:
[["+", "609"], ["*", "48"], ["+", "345"]]
What I need to do is go through and convert the numbers to integers while
leaving the symbols (+, *) as strings. Ive I've tried a couple different
variations of this:
input = Array.new(File.read("input.txt").split("\n").map {|l| l.split("
")}).map {|e| e[-1].to_i}

and the txt file is formatted like:
+ 609
* 48
+ 345
etc...

Like I said, I tried a couple different variations of the code above,
mostly just changing the last map block, but nothing I can think of is
working. any help please!?!?

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

$ file=sample.txt ; cat $file ; ruby -lne 'BEGIN{a=};
$_.scan(/(\S)\s+(\d+)/){|op,num| a<<[op,num.to_i]} ;END{p a}' < $file
+ 609
* 48
+ 345
[["+", 609], ["*", 48], ["+", 345]]

best regards
--botp

···

On Wed, Nov 16, 2016 at 8:59 AM, Micky Scandal <mickyscandal@gmail.com> wrote:

input = Array.new(File.read("input.txt").split("\n").map {|l| l.split("
")}).map {|e| e[-1].to_i}

and the txt file is formatted like:
+ 609
* 48
+ 345
etc...

alright, thank you very much! that definitely works, I don't really
understand this part though:
  y.to_i == 0 && y != "0" ? y : y.to_i
I see that it's a ternary (is that right?) statement, I guess it's just the
0's I don't understand. I'm guessing it has more to do with the quotes
around the second 0 and not the first than it actually does with the 0's
themselves? then if that statement is true leave the value alone if false
then convert to an integer? I guess its more like just this part I don't
get then:
  y.to_i == 0 && y != "0"

but again, thanks for the quick reply and thanks in advance to anyone who
can explain that statement to me! lol, I just really hate using code that i
don't understand!

···

On Tue, Nov 15, 2016 at 5:08 PM, À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]]

Abejide Ayodele
It always seems impossible until it's done. --Nelson Mandela

On Tue, Nov 15, 2016 at 6:59 PM, Micky Scandal <mickyscandal@gmail.com> > wrote:

Hey all, I've run into a bit of a snag. I have an array something like
this:
[["+", "609"], ["*", "48"], ["+", "345"]]
What I need to do is go through and convert the numbers to integers while
leaving the symbols (+, *) as strings. Ive I've tried a couple different
variations of this:
input = Array.new(File.read("input.txt").split("\n").map {|l| l.split("
")}).map {|e| e[-1].to_i}

and the txt file is formatted like:
+ 609
* 48
+ 345
etc...

Like I said, I tried a couple different variations of the code above,
mostly just changing the last map block, but nothing I can think of is
working. any help please!?!?

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;

oh... wait... is it because if i were to try to convert the symbols to
integers they would be converted to a 0? so that is saying if y.to_i == 0
then the value it just tried to convert wasn't a number and the second part
is making sure that the value isn't already a "0"?

···

On Tue, Nov 15, 2016 at 5:35 PM, Micky Scandal <mickyscandal@gmail.com> wrote:

alright, thank you very much! that definitely works, I don't really
understand this part though:
  y.to_i == 0 && y != "0" ? y : y.to_i
I see that it's a ternary (is that right?) statement, I guess it's just
the 0's I don't understand. I'm guessing it has more to do with the quotes
around the second 0 and not the first than it actually does with the 0's
themselves? then if that statement is true leave the value alone if false
then convert to an integer? I guess its more like just this part I don't
get then:
  y.to_i == 0 && y != "0"

but again, thanks for the quick reply and thanks in advance to anyone who
can explain that statement to me! lol, I just really hate using code that i
don't understand!

On Tue, Nov 15, 2016 at 5:08 PM, À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]]

Abejide Ayodele
It always seems impossible until it's done. --Nelson Mandela

On Tue, Nov 15, 2016 at 6:59 PM, Micky Scandal <mickyscandal@gmail.com> >> wrote:

Hey all, I've run into a bit of a snag. I have an array something like
this:
[["+", "609"], ["*", "48"], ["+", "345"]]
What I need to do is go through and convert the numbers to integers
while leaving the symbols (+, *) as strings. Ive I've tried a couple
different variations of this:
input = Array.new(File.read("input.txt").split("\n").map {|l| l.split("
")}).map {|e| e[-1].to_i}

and the txt file is formatted like:
+ 609
* 48
+ 345
etc...

Like I said, I tried a couple different variations of the code above,
mostly just changing the last map block, but nothing I can think of is
working. any help please!?!?

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;

Yep, that's a ternary if-else statement. Before the "?" Is the off
statement, and immediately after the "?" Is the code ran if the conditions
for the if statement are meant. Else, the code after ":" is ran.

The if case y.to_i==0 && y != "0" are to determine whether or not the
element mapped is a string, or in your case a mathematical operator in
string form. Anything string doesn't begin with a number, when called .to_i
gets converted to a zero, which is checked by the first case y.to_i == 0.
The second if case, y != "0" is to cover scenarios where the number might
actually be 0, and not a symbol that was called to_i and converted to 0
because it wasn't a number.

Hope that helps!

···

On Tuesday, November 15, 2016, Micky Scandal <mickyscandal@gmail.com> wrote:

alright, thank you very much! that definitely works, I don't really
understand this part though:
  y.to_i == 0 && y != "0" ? y : y.to_i
I see that it's a ternary (is that right?) statement, I guess it's just
the 0's I don't understand. I'm guessing it has more to do with the quotes
around the second 0 and not the first than it actually does with the 0's
themselves? then if that statement is true leave the value alone if false
then convert to an integer? I guess its more like just this part I don't
get then:
  y.to_i == 0 && y != "0"

but again, thanks for the quick reply and thanks in advance to anyone who
can explain that statement to me! lol, I just really hate using code that i
don't understand!

On Tue, Nov 15, 2016 at 5:08 PM, Àbéjídé Àyodélé <abejideayodele@gmail.com > <javascript:_e(%7B%7D,'cvml','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]]

Abejide Ayodele
It always seems impossible until it's done. --Nelson Mandela

On Tue, Nov 15, 2016 at 6:59 PM, Micky Scandal <mickyscandal@gmail.com >> <javascript:_e(%7B%7D,'cvml','mickyscandal@gmail.com');>> wrote:

Hey all, I've run into a bit of a snag. I have an array something like
this:
[["+", "609"], ["*", "48"], ["+", "345"]]
What I need to do is go through and convert the numbers to integers
while leaving the symbols (+, *) as strings. Ive I've tried a couple
different variations of this:
input = Array.new(File.read("input.txt").split("\n").map {|l| l.split("
")}).map {|e| e[-1].to_i}

and the txt file is formatted like:
+ 609
* 48
+ 345
etc...

Like I said, I tried a couple different variations of the code above,
mostly just changing the last map block, but nothing I can think of is
working. any help please!?!?

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
<javascript:_e(%7B%7D,'cvml','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
<javascript:_e(%7B%7D,'cvml','ruby-talk-request@ruby-lang.org');>
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Sorry correction for a typo, "before the '?' Is the IF statement,..."

···

On Tuesday, November 15, 2016, Bao Le <baole6989@gmail.com> wrote:

Yep, that's a ternary if-else statement. Before the "?" Is the off
statement, and immediately after the "?" Is the code ran if the conditions
for the if statement are meant. Else, the code after ":" is ran.

The if case y.to_i==0 && y != "0" are to determine whether or not the
element mapped is a string, or in your case a mathematical operator in
string form. Anything string doesn't begin with a number, when called .to_i
gets converted to a zero, which is checked by the first case y.to_i == 0.
The second if case, y != "0" is to cover scenarios where the number might
actually be 0, and not a symbol that was called to_i and converted to 0
because it wasn't a number.

Hope that helps!

On Tuesday, November 15, 2016, Micky Scandal <mickyscandal@gmail.com > <javascript:_e(%7B%7D,'cvml','mickyscandal@gmail.com');>> wrote:

alright, thank you very much! that definitely works, I don't really
understand this part though:
  y.to_i == 0 && y != "0" ? y : y.to_i
I see that it's a ternary (is that right?) statement, I guess it's just
the 0's I don't understand. I'm guessing it has more to do with the quotes
around the second 0 and not the first than it actually does with the 0's
themselves? then if that statement is true leave the value alone if false
then convert to an integer? I guess its more like just this part I don't
get then:
  y.to_i == 0 && y != "0"

but again, thanks for the quick reply and thanks in advance to anyone who
can explain that statement to me! lol, I just really hate using code that i
don't understand!

On Tue, Nov 15, 2016 at 5:08 PM, À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]]

Abejide Ayodele
It always seems impossible until it's done. --Nelson Mandela

On Tue, Nov 15, 2016 at 6:59 PM, Micky Scandal <mickyscandal@gmail.com> >>> wrote:

Hey all, I've run into a bit of a snag. I have an array something like
this:
[["+", "609"], ["*", "48"], ["+", "345"]]
What I need to do is go through and convert the numbers to integers
while leaving the symbols (+, *) as strings. Ive I've tried a couple
different variations of this:
input = Array.new(File.read("input.txt").split("\n").map {|l|
l.split(" ")}).map {|e| e[-1].to_i}

and the txt file is formatted like:
+ 609
* 48
+ 345
etc...

Like I said, I tried a couple different variations of the code above,
mostly just changing the last map block, but nothing I can think of is
working. any help please!?!?

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;

It's just a variable basically, you go from |x| to |y| in the map. So then you use the, let's call it a "placeholder variable" for the ternary, yes that's correct it is a ternary.

So here's what he pseudo code for it:

If variable y to integer equals zero
And
Variable y as a string equal "zero"
Return y
Else
Return y to integer

Make sense?

- Thomas Perkins

···

On Nov 15, 2016, at 7:35 PM, Micky Scandal <mickyscandal@gmail.com> wrote:

alright, thank you very much! that definitely works, I don't really understand this part though:
  y.to_i == 0 && y != "0" ? y : y.to_i
I see that it's a ternary (is that right?) statement, I guess it's just the 0's I don't understand. I'm guessing it has more to do with the quotes around the second 0 and not the first than it actually does with the 0's themselves? then if that statement is true leave the value alone if false then convert to an integer? I guess its more like just this part I don't get then:
  y.to_i == 0 && y != "0"

but again, thanks for the quick reply and thanks in advance to anyone who can explain that statement to me! lol, I just really hate using code that i don't understand!

On Tue, Nov 15, 2016 at 5:08 PM, À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]]

Abejide Ayodele
It always seems impossible until it's done. --Nelson Mandela

On Tue, Nov 15, 2016 at 6:59 PM, Micky Scandal <mickyscandal@gmail.com> wrote:
Hey all, I've run into a bit of a snag. I have an array something like this:
[["+", "609"], ["*", "48"], ["+", "345"]]
What I need to do is go through and convert the numbers to integers while leaving the symbols (+, *) as strings. Ive I've tried a couple different variations of this:
input = Array.new(File.read("input.txt").split("\n").map {|l| l.split(" ")}).map {|e| e[-1].to_i}

and the txt file is formatted like:
+ 609
* 48
+ 345
etc...

Like I said, I tried a couple different variations of the code above, mostly just changing the last map block, but nothing I can think of is working. any help please!?!?

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;

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

Correction to my pseudo code:

If variable y to integer equals zero
And
Variable y as a strong does not equal "zero"
Return y
Else
Return y to integer

Sorry about that

- Thomas Perkins

···

On Nov 15, 2016, at 7:45 PM, Bao Le <baole6989@gmail.com> wrote:

Sorry correction for a typo, "before the '?' Is the IF statement,..."

On Tuesday, November 15, 2016, Bao Le <baole6989@gmail.com> wrote:
Yep, that's a ternary if-else statement. Before the "?" Is the off statement, and immediately after the "?" Is the code ran if the conditions for the if statement are meant. Else, the code after ":" is ran.

The if case y.to_i==0 && y != "0" are to determine whether or not the element mapped is a string, or in your case a mathematical operator in string form. Anything string doesn't begin with a number, when called .to_i gets converted to a zero, which is checked by the first case y.to_i == 0. The second if case, y != "0" is to cover scenarios where the number might actually be 0, and not a symbol that was called to_i and converted to 0 because it wasn't a number.

Hope that helps!

On Tuesday, November 15, 2016, Micky Scandal <mickyscandal@gmail.com> wrote:
alright, thank you very much! that definitely works, I don't really understand this part though:
  y.to_i == 0 && y != "0" ? y : y.to_i
I see that it's a ternary (is that right?) statement, I guess it's just the 0's I don't understand. I'm guessing it has more to do with the quotes around the second 0 and not the first than it actually does with the 0's themselves? then if that statement is true leave the value alone if false then convert to an integer? I guess its more like just this part I don't get then:
  y.to_i == 0 && y != "0"

but again, thanks for the quick reply and thanks in advance to anyone who can explain that statement to me! lol, I just really hate using code that i don't understand!

On Tue, Nov 15, 2016 at 5:08 PM, À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]]

Abejide Ayodele
It always seems impossible until it's done. --Nelson Mandela

On Tue, Nov 15, 2016 at 6:59 PM, Micky Scandal <mickyscandal@gmail.com> wrote:
Hey all, I've run into a bit of a snag. I have an array something like this:
[["+", "609"], ["*", "48"], ["+", "345"]]
What I need to do is go through and convert the numbers to integers while leaving the symbols (+, *) as strings. Ive I've tried a couple different variations of this:
input = Array.new(File.read("input.txt").split("\n").map {|l| l.split(" ")}).map {|e| e[-1].to_i}

and the txt file is formatted like:
+ 609
* 48
+ 345
etc...

Like I said, I tried a couple different variations of the code above, mostly just changing the last map block, but nothing I can think of is working. any help please!?!?

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;

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

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

···

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]]

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

Nice and condensed but I prefer to be a bit more verbose

array = ARGF.each_line.each_with_object() do |line, a|
  if /^([+*])\s+(\d+)\s*$/ =~ line
    a << [$1, Integer($2)]
  end
end

p array

$ ruby x.rb x
[["+", 609], ["*", 48], ["+", 345]]

Note, I use Integer() vs #to_i because it throws if the input is non
numeric. Does not matter with the regex used here but helps catch
issues if the regex is change incompatibly in some way.

Kind regards

robert

···

On Wed, Nov 16, 2016 at 9:59 AM, botp <botpena@gmail.com> wrote:

On Wed, Nov 16, 2016 at 8:59 AM, Micky Scandal <mickyscandal@gmail.com> wrote:

input = Array.new(File.read("input.txt").split("\n").map {|l| l.split("
")}).map {|e| e[-1].to_i}

and the txt file is formatted like:
+ 609
* 48
+ 345
etc...

$ file=sample.txt ; cat $file ; ruby -lne 'BEGIN{a=};
$_.scan(/(\S)\s+(\d+)/){|op,num| a<<[op,num.to_i]} ;END{p a}' < $file
+ 609
* 48
+ 345
[["+", 609], ["*", 48], ["+", 345]]

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

alright, i definitely got it now. I figured out what was really confusing
me. I didn't know if you try to convert something other than a number to an
integer it will still convert it but just give you a 0. I guess I figured
it would throw an error or something. all very good to know! thank you
everyone for all the help!

···

On Tue, Nov 15, 2016 at 5:50 PM, thomas Perkins <thomas.perkins23@icloud.com > wrote:

Correction to my pseudo code:

If variable y to integer equals zero
And
Variable y as a strong does not equal "zero"
Return y
Else
Return y to integer

Sorry about that

- Thomas Perkins

On Nov 15, 2016, at 7:45 PM, Bao Le <baole6989@gmail.com> wrote:

Sorry correction for a typo, "before the '?' Is the IF statement,..."

On Tuesday, November 15, 2016, Bao Le <baole6989@gmail.com> wrote:

Yep, that's a ternary if-else statement. Before the "?" Is the off
statement, and immediately after the "?" Is the code ran if the conditions
for the if statement are meant. Else, the code after ":" is ran.

The if case y.to_i==0 && y != "0" are to determine whether or not the
element mapped is a string, or in your case a mathematical operator in
string form. Anything string doesn't begin with a number, when called .to_i
gets converted to a zero, which is checked by the first case y.to_i == 0.
The second if case, y != "0" is to cover scenarios where the number might
actually be 0, and not a symbol that was called to_i and converted to 0
because it wasn't a number.

Hope that helps!

On Tuesday, November 15, 2016, Micky Scandal <mickyscandal@gmail.com> >> wrote:

alright, thank you very much! that definitely works, I don't really
understand this part though:
  y.to_i == 0 && y != "0" ? y : y.to_i
I see that it's a ternary (is that right?) statement, I guess it's just
the 0's I don't understand. I'm guessing it has more to do with the quotes
around the second 0 and not the first than it actually does with the 0's
themselves? then if that statement is true leave the value alone if false
then convert to an integer? I guess its more like just this part I don't
get then:
  y.to_i == 0 && y != "0"

but again, thanks for the quick reply and thanks in advance to anyone
who can explain that statement to me! lol, I just really hate using code
that i don't understand!

On Tue, Nov 15, 2016 at 5:08 PM, À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]]

Abejide Ayodele
It always seems impossible until it's done. --Nelson Mandela

On Tue, Nov 15, 2016 at 6:59 PM, Micky Scandal <mickyscandal@gmail.com> >>>> wrote:

Hey all, I've run into a bit of a snag. I have an array something like
this:
[["+", "609"], ["*", "48"], ["+", "345"]]
What I need to do is go through and convert the numbers to integers
while leaving the symbols (+, *) as strings. Ive I've tried a couple
different variations of this:
input = Array.new(File.read("input.txt").split("\n").map {|l|
l.split(" ")}).map {|e| e[-1].to_i}

and the txt file is formatted like:
+ 609
* 48
+ 345
etc...

Like I said, I tried a couple different variations of the code above,
mostly just changing the last map block, but nothing I can think of is
working. any help please!?!?

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;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe
<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;

I really like this solution.

···

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;

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;

There is a method that raises an exception if you try to convert
something that is not a number, Kernel#Integer. Compare:

    irb(main):001:0> "A".to_i
    => 0
    irb(main):002:0> Integer("A")
    ArgumentError: invalid value for Integer(): "A"
                   from (irb):2:in `Integer'
                   from (irb):2
                   from /usr/bin/irb:11:in `<main>'
    irb(main):003:0>

This is just for your information so you know there are multiple
possibilities to convert strings to numbers which show different
behaviour.

Greetings
Marvin

···

On Tue, Nov 15, 2016 at 06:13:42PM -0800, Micky Scandal wrote:

I didn't know if you try to convert something other than a number to
an integer it will still convert it but just give you a 0. I guess I
figured it would throw an error or something.

--
Blog: http://www.guelkerdev.de
PGP/GPG ID: F1D8799FBCC8BC4F