・ It is a memorandum of knowledge caught up. ・ I would appreciate it if you could point out any mistakes.
・ To_a
method
A method to convert hashes, range objects, etc. into arrays. Source: [Ruby] Let's understand how to use the to_a method
・ Shuffle
method
The shuffle method randomly swaps the elements of the array to get it. Source: First Ruby! Randomly acquire / replace arrays | sample / shuffle
-Join
method
The join method is used when you want to combine the elements of an array into a single string. The join method converts each element of the array to a string and joins the arguments as delimiters. Source: Active engineers explain how to use Ruby's join method [for beginners]
('a'..'z').to_a.shuffle[0..7].join
Try running the command in the terminal. Enter ʻirb` and then enter the command.
Terminal
munetomokazushi@mba ~ % irb
irb(main):001:0> ('a'..'z').to_a.shuffle[0..7].join
=> "yahxwgti"
An 8-character random alphabet was output.
Disassemble.
irb(main):001:0> ('a'..'z')
First, describe that it is in the range from the alphabet string 'a'
to'z'
.
..
is a Range object. ..
includes the last character, but...
does not include the last character. Caution.
irb(main):001:0> ('a'..'z').to_a
=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
It became an array with the .to_a method.
irb(main):001:0> ('a'..'z').to_a.shuffle
=> ["o", "c", "q", "i", "m", "a", "k", "l", "t", "x", "z", "w", "n", "v", "b", "s", "h", "u", "r", "e", "g", "y", "f", "j", "p", "d"]
The array was shuffled with the shuffle method. (Shuffled every time you run)
irb(main):001:0> ('a'..'z').to_a.shuffle[0..7]
=> ["r", "f", "b", "x", "y", "m", "g", "t"]
The shuffled array is acquired for 8 characters from the beginning. Note that the array starts at 0.
irb(main):001:0> ('a'..'z').to_a.shuffle[0..7].join
=> "yahxwgti"
Finally, the join method was used to join the elements of the array into a single string.
I received a comment, so please quote and add it. Thank you, @ nodai2h_ITC!
This method does not generate a string that has the same character more than once, such as "agrjesao". For example, if you write a program for the third game of rock-paper-scissors, and if you write a program that doesn't make a move once, you wouldn't call it a "random move". Besides, this method cannot create a random alphabet string of 30 characters, for example, which exceeds the number of alphabet characters. So, how about a way to generate a "random alphabet string" that allows the same characters?
chars = ('a'..'z').to_a # = *'a'..'z'But yes
8.times.map{ chars.sample }.join
This result looks like this.
irb(main):009:0> chars = ('a'..'z').to_a
=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
irb(main):010:0> 8.times.map{ chars.sample }.join
=> "bjtaniyw"
irb(main):011:0> 8.times.map{ chars.sample }.join
=> "ntbtrdml"
irb(main):012:0> 8.times.map{ chars.sample }.join
=> "ajsgpcwn"
irb(main):013:0> 8.times.map{ chars.sample }.join
=> "vxdgakuc"
irb(main):014:0> 8.times.map{ chars.sample }.join
=> "xknqtmcq" #The same alphabet appears twice. It is displayed properly at random
# string_Define shuffle method
def string_shuffle(s)
s.split('').shuffle.join
end
#Try to call
string_shuffle("foobar")
# string_The result of calling the shuffle method
=> "fboora"
irb(main):005:0> ('Ah'..'Hmm').to_a.shuffle[0..7].join
=> "Gofukuporekariso"
You can also use hiragana.
-It can also be used when creating random subdomains.
-If you want to run it on the console, enter rails c
in the terminal to start the console.
[Ruby] Let's understand how to use the to_a method First Ruby! Randomly acquire / replace arrays | sample / shuffle Active engineers explain how to use Ruby's join method [for beginners]
Recommended Posts