From: Rajkumar Surabhi on 25 Mar 2010 00:27 Hi all, i have an array contains no of elements. i want to concatinate 91 before every elemnt of array. example i have A= [234,456,raju] like this i want the out put arry tobe A=[91234,91456,91raju] like thissss i want to add 91 before every element how to do this -- Posted via http://www.ruby-forum.com/.
From: Mario Antonetti on 25 Mar 2010 00:38 [Note: parts of this message were removed to make it a legal post.] On Wed, Mar 24, 2010 at 10:27 PM, Rajkumar Surabhi <mailtorajuit(a)gmail.com>wrote: > Hi all, > > i have an array contains no of elements. i want to concatinate 91 before > every elemnt of array. example > > i have > A= [234,456,raju] like this > i want the out put arry tobe A=[91234,91456,91raju] like thissss > > i want to add 91 before every element > > how to do this > -- > Posted via http://www.ruby-forum.com/. > > the assignment of the variable should look more like this: ------------------------------------------------------------- a = ["234","456","raju"] ------------------------------------------------------------- because otherwise raju is a variable, which wouldn't allow you to append 91 to the beginning of it. Try this then: -------------------------------------------------------- array.collect! do |element| "91" + element.to_s end -------------------------------------------------------- Mario
From: Brandon Jones on 25 Mar 2010 01:36 And the short form > a = ["123", "456", "raju"] > a.map!{|x|"91#{x}"} # => ["91123", "91456", "91raju"] -- Posted via http://www.ruby-forum.com/.
From: Robert Klemme on 25 Mar 2010 08:20 2010/3/25 Brandon Jones <brandon.g.jones(a)gmail.com>: > And the short form > >> a = ["123", "456", "raju"] >> a.map!{|x|"91#{x}"} # => ["91123", "91456", "91raju"] Even shorter (6 chars if I'm not mistaken): a = %w{123 456 raju} a.map!{|x|"91#{x}"} ;-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Brandon Jones on 25 Mar 2010 17:53 Robert Klemme wrote: > Even shorter (6 chars if I'm not mistaken): > > a = %w{123 456 raju} > a.map!{|x|"91#{x}"} > > ;-) > > Kind regards > > robert oh yeah?!? well take this %w(123 456 raju).map{|x|"91#{x}"} not exactly the same, as there is no persistent variable, but it is a bit shorter -- Posted via http://www.ruby-forum.com/.
|
Next
|
Last
Pages: 1 2 Prev: How to create an infinite enumerable of Times? Next: Read & Write data on the web. |