6 Replies - 585 Views - Last Post: 05 January 2014 - 01:24 PM Rate Topic: -----

#1 heaphyg   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

regex question

Posted 03 January 2014 - 05:26 PM

What is a good way to take all the multi digit numbers in an array and split them ( 20 become 2, 0)
example_array = [1, 20, 3, 18] becomes [1, 2, 0, 3, 1, 8]

Do I need to to use a regular expression for this?
Is This A Good Question/Topic? 0
  • +

Replies To: regex question

#2 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: regex question

Posted 03 January 2014 - 05:55 PM

arr = Array.new
arr = example_array.join("").split("").map { |x| x.to_i }
print arr;    # [1, 2, 0, 3, 1, 8]

Never really used Ruby.. amazing what a few minutes on the internet can produce :)

This post has been edited by andrewsw: 03 January 2014 - 06:08 PM

Was This Post Helpful? 2
  • +
  • -

#3 heaphyg   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: regex question

Posted 03 January 2014 - 06:02 PM

I found this which looks like a step in the right direction. 1234.to_s.chars.map(&:to_i) => [1,2,3,4]. What does the & symbol mean in the map argument?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: regex question

Posted 03 January 2014 - 06:22 PM

It's a pretzel :)

http://stackoverflow...me-mean-in-ruby

..kinda like a lambda ;)

1234.to_s.chars.map(&:to_i)

  • Takes the number 1234
  • converts it to a string
  • splits it into a character array
  • maps each element (each character) by converting to an integer.

So you can think of the ampersand & as meaning each, or perhaps as a map operator - but I've just made these terms up. Disclaimer: I don't use Ruby so my terminology is inaccurate.

I think this
map { |x| x.to_i }

is much clearer.

This post has been edited by andrewsw: 03 January 2014 - 06:25 PM

Was This Post Helpful? 1
  • +
  • -

#5 Lemur   User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1453
  • View blog
  • Posts: 3,633
  • Joined: 28-November 09

Re: regex question

Posted 04 January 2014 - 07:15 PM

Read through this for future reference on idiomatic Ruby: https://github.com/b...uby-style-guide

The &:function syntax indicates a function to map with. The code block ( { |var| var.to_i } ) is also a function, albeit anonymous. It is the idiomatic way of doing things in Ruby.

Here's another common use I see of that:
Person = Struct.new(:name, :age, :gender)
people_list = [].tap { |list|
  list << Person.new('bob', 24, 'M')
  list << Person.new('jim', 28, 'M')
}

names = people_list.map(&:name) 



Of course I used tap on purpose as it's another nice thing to know how to use. This case is questionable, granted. Tap always returns self, the object that called it. It's especially useful to keep from tacking a variable on the end of a function for 'implied' returns.

This post has been edited by Lemur: 04 January 2014 - 07:15 PM

Was This Post Helpful? 1
  • +
  • -

#6 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: regex question

Posted 04 January 2014 - 10:49 PM

I believe (I read that) the :function combination is a symbol, from the Symbol class. But what's the ampersand called in this context? I made up the term map-operator but there must be an official title for it?
Was This Post Helpful? 0
  • +
  • -

#7 Lemur   User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1453
  • View blog
  • Posts: 3,633
  • Joined: 28-November 09

Re: regex question

Posted 05 January 2014 - 01:24 PM

The & treats the following as a method name to call. It's shorthand for method(:method_name), and in this case, shorter for { |x| method(:method_name).call(x) }
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1