From: Donnel Graham Date: 2012-08-06T03:20:26+09:00 Subject: Re: how to split user input into variables --0015175dda0261669004c688d023 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Aug 5, 2012 at 12:10 PM, Fosiul Alam wrote: > Hi > Suppose if i do this > > puts "Inter Groups name" > > groups= gets > > User will pass value like this > > ops,admin,account > > now how can i > > (a) split those values (ops,admin,account) > > (b)loop through those values exmaple > > if value=ops > > do this > if value = admin > do this > if value = account > do this > > if you just highlight few steps , that would be fine > > thanks > > -- > Posted via http://www.ruby-forum.com/. > > Here is one way you can look at it: --------------------------------------------------- puts "Inter Group name" groups = gets.chomp.split(', ') groups.each do |value| if value == 'admin' do this elsif value == 'ops' do that elsif value == 'accounts' do something else end end ------------------------------------------------- I'm using the String#split method to split the values on a comma. Then I use the each method to loop over those values. Hopes this helps ;) --0015175dda0261669004c688d023 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Sun, Aug 5, 2012 at 12:10 PM, Fosiul = Alam <lists@ruby-forum.com> wrote:
Hi
Suppose if i do this

puts "Inter Groups name"

groups=3D gets

User will pass value like this

ops,admin,account

now how can i

(a) split those values =A0(ops,admin,account)

(b)loop through those values exmaple

if value=3Dops

=A0do this
if value =3D admin
do this
if value =3D account
do this

if you just highlight few steps , that would be fine

thanks

--
Posted via http://= www.ruby-forum.com/.


Here is one way you can = look at it:

--------------------------------------= -------------

puts "Inter Group name&quo= t;

groups =3D gets.chomp.split(', ')
groups.each do |value|
if value =3D=3D 'admin'
do this=
elsif= value =3D=3D 'ops'
do that=A0
elsif value =3D=3D 'accounts&#= 39;
do s= omething else
end
end

-----------------= --------------------------------

I'm using the=A0String#split=A0method to split the = values on a comma. Then I use the each method =A0to loop over those values.= =A0

Hopes this helps ;)=A0
--0015175dda0261669004c688d023--