6 Replies - 2837 Views - Last Post: 01 August 2019 - 08:13 PM Rate Topic: ****- 1 Votes

#1 benardmanuh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-June 19

How do i randomly select from two different methods?

Posted 22 June 2019 - 11:55 AM

My explanation might be hard to understand because English is not my first language.

I am trying to randomly select from two different methods. One method that has "name" called "Jordan" in all uppercase letters and another method that has the "name" called "Jordan" in all lowercase letters.

What i tried to do to fix the issue myself is use a method called random_case and in that method wrote down the method names "upper" and "lower" inside a array and use the sample method to randomly select between the two of them.

Here below is a snippet of how i tried to do it.

name = "Jordan"

def upper(string)
  string.upcase
end

def lower(string)
  string.downcase
end

def random_name
  ["Ollie", "Ana"].sample
end

# Write code between this line...
def random_case(name)
  [upper, lower].sample
 end
# ...and between this line

puts "My name is #{random_case(name)}"


And this is the error message that i am getting:

wrong number of arguments (given 0, expected 1)
(repl):3:in `upper'
(repl):17:in `random_case'
(repl):21:in `<main>'

What i am trying to display on my console when i run my code is a random selection of the lower method which will be displayed as My name is jordan or the upper method which will be displayed as My name is JORDAN.

Please can you ONLY edit the code that is between "# Write code between this line..." and "# ...and between this line" comments

Is This A Good Question/Topic? 0
  • +

Replies To: How do i randomly select from two different methods?

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15268
  • View blog
  • Posts: 61,211
  • Joined: 12-June 08

Re: How do i randomly select from two different methods?

Posted 22 June 2019 - 12:42 PM

I would thing.. using Ruby's random - with a max and knowing what is the specific upper bounds - you could get it to show either 0 or 1. If 0 do what ever.. if 1 do the other thing.

https://ruby-doc.org...2.0/Random.html

Quote

can you ONLY edit the code that is between "#

Being the site has rules about asking folk to do your work, I will let you take a stab at it.
Was This Post Helpful? 0
  • +
  • -

#3 benardmanuh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-June 19

Re: How do i randomly select from two different methods?

Posted 22 June 2019 - 12:55 PM

View Postmodi123_1, on 22 June 2019 - 12:42 PM, said:

.
I would thing.. using Ruby's random - with a max and knowing what is the specific upper bounds - you could get it to show eiter 0 or 1. If 0 do what ever.. if 1 do the other thing.

https://ruby-doc.org...2.0/Random.html

Quote

can you ONLY edit the code that is between "#

Being the site has rules about asking folk to do your work, I will let you take a stab at it.


I am sorry but i dont understand what is written in the link or what you are talking about. Is there a way where you can show me or edit my code. I know that you are not allowed to do that on this website but I really want to fix this.

and yeah i can only edit that is between the two comments that i mentioned earlier.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15268
  • View blog
  • Posts: 61,211
  • Joined: 12-June 08

Re: How do i randomly select from two different methods?

Posted 22 June 2019 - 01:22 PM

Here's an example of the pseudo code. You will have to read the link I provided to understand if the max is inclusive or exclusive.. as that could change your max value choice.

variable CHOICE = random(max value)

IF CHOICE equals 0 THEN
   Do one function.
ELSE
   do another function
END IF



I know that you are not allowed to do that on this website but I really want to fix this. 

You acknowledge the rules about giving you the answer, but persist. Ha!

I really want you to try this.
Was This Post Helpful? 1
  • +
  • -

#5 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4556
  • View blog
  • Posts: 19,233
  • Joined: 24-September 08

Re: How do i randomly select from two different methods?

Posted 01 July 2019 - 07:30 AM

It's saying wrong number of arguments because there are.

def random_case(name)
    [upper(name), lower(name)].sample
end



This should get you what you want, however, it'd be making 2 different method calls then sampling, vs just having an if/else with a random number from a random number generator.
Was This Post Helpful? 1
  • +
  • -

#6 BobRodes   User is offline

  • Product Manager
  • member icon

Reputation: 603
  • View blog
  • Posts: 3,085
  • Joined: 19-May 09

Re: How do i randomly select from two different methods?

Posted 01 August 2019 - 08:06 PM

Put this on line 17:

send([:upper, :lower].sample, random_name)


The send method has this syntax: `send(method_name, args)` where `method_name` is the name of the method as a symbol. So, you're calling one of the two methods at random, and passing as an argument one of the names in your array of names, also selected at random. I believe that's what you wanted.

This post has been edited by BobRodes: 21 August 2019 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

#7 BobRodes   User is offline

  • Product Manager
  • member icon

Reputation: 603
  • View blog
  • Posts: 3,085
  • Joined: 19-May 09

Re: How do i randomly select from two different methods?

Posted 01 August 2019 - 08:13 PM

View Postmodi123_1, on 22 June 2019 - 03:22 PM, said:

Here's an example of the pseudo code. You will have to read the link I provided to understand if the max is inclusive or exclusive.. as that could change your max value choice.

variable CHOICE = random(max value)

IF CHOICE equals 0 THEN
   Do one function.
ELSE
   do another function
END IF



I know that you are not allowed to do that on this website but I really want to fix this. 

You acknowledge the rules about giving you the answer, but persist. Ha!

I really want you to try this.

Succinctly:
rand(2).zero? ? upper(random_name) : lower(random_name)

I really want him to read this. LOL

This post has been edited by BobRodes: 01 August 2019 - 08:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1