-3

I will like to select a name randomly from an array list then display either in Capital or Lowercase

For example names= ["James", "John", "Jane"] Output should be: JOHN or john or jane or JAMES or JANE or james

Please help!

I have tried using the .sample() command which selects from an array. Also, I'm aware of the .upcase() and .lowercase(), the problem now is how to combine these whole methods together in order to get the desire result which is to select a name randomly for the array list then display in either Capital or Lowercase.

 def random_case(*names)
  names= ["James", "John", "Jane"]
  names.sample { |names| names.upcase  names.downcase }
  puts "Hello #{names}! How are you today?"
 end

random_case()

I expect the output to be JOHN or john or jane or JAMES or JANE or james - randomly

  • Your first two sentences are contradictory. How can you select one name and return an object (is that a string?) that references all names? You say, "...then display either in Capital or Lowercase". Well, which is it? – Cary Swoveland Jul 05 '19 at 17:25

4 Answers4

2

I would start with:

def random_case(*names)
  name = names.sample                                # pick a random name
  [true, false].sample ? name.upcase : name.downcase # return name with random format
end

random_case("James", "John", "Jane")    
#=> "JAMES"
random_case("James", "John", "Jane")
#=> "john"
spickermann
  • 100,941
  • 9
  • 101
  • 131
2

I'd write two methods. One for returning a random name out of a list of names:

def random_name(*names)
  names.sample
end

and another one for changing a given name's case: (rand < 0.5 has a 50% chance of being true)

def random_case(name)
  if rand < 0.5
    name.upcase
  else
    name.downcase
  end
end

Then combine both:

5.times do
  puts random_case(random_name("James", "John", "Jane"))
end

Output:

JOHN
JOHN
jane
JAMES
james

If you don't like the rand < 0.5 approach, you could also put both variants into an array and pick one randomly:

def random_case(name)
  [name.upcase, name.downcase].sample
end

Or you could put the method names :upcase and :downcase in an array, pick one of them randomly and use public_send to invoke that method:

def random_case(name)
  name.public_send([:upcase, :downcase].sample)
end
Stefan
  • 109,145
  • 14
  • 143
  • 218
0

Try this

 def random_case(names)  
   names.flat_map{|name| [name.upcase, name.downcase, name.capitalize]}.sample
 end

 names= ["James", "John", "Jane"]

 puts random_case names

I think the code is quite simple to understand. Any trouble? leave a comment to let me know

quyetdc
  • 1,485
  • 1
  • 14
  • 24
  • Thank you for your help. Please see further question and your help is highly need -` https://stackoverflow.com/q/56864526/6651760 – Christopher Ezimoha Jul 03 '19 at 06:58
  • Hi @ChristopherEzimoha it's good to know it helps. Could you mark this as solution first, and then, reformat your code in your new question, so other can see it better – quyetdc Jul 03 '19 at 07:02
  • Please don't ask the OP to select your answer, it's unbecoming. Had the OP been an SO veteran they would have merely disregarded the request; this OP, clearly new to SO, may not realise that there is no rush to select an answer, that most members wait at least a couple of hours before doing so. Christopher, invoking each method on the each name, then selecting one array randomly, is generally poor practice, better to select one name randomly then invoke each method on that name. – Cary Swoveland Jul 05 '19 at 17:16
0

You can do like this.

 names= ["James", "John", "Jane"]
 random = [names.sample.upcase, names.sample.downcase].sample
 puts "Hello #{random}! How are you today?"
Axel Blaz
  • 29
  • 7