Ruby learning 3

Methods and so on 3

Currently, I am studying to get the Ruby engineer certification exam silver. I still have a lot of understanding of the language, so I will output from the basics.

Array generation

Here's how to generate a normal array

mr_children = ["GIFT", "himawari", "simple"] #Store elements in square brackets
p mr_children

=> ["GIFT", "himawari", "simple"]

When using the% notation, commas and quotation marks disappear and it is a little refreshing.

mr_children = %w(GIFT himawari simple)
p mr_children

=> ["GIFT", "himawari", "simple"]

Note-Adding and inserting elements

mr_children = %w(GIFT himawari simple)
mr_children << "over" #Add element to the very end of the array
p mr_children

=> ["GIFT", "himawari", "simple", "over"]

mr_children.insert(1, "Esora") #Specify the subscript in the first argument and insert the element there
p mr_children

=> ["GIFT", "Esora", "himawari", "simple", "over"]

Array operation (*)

mr_children = %w(GIFT himawari simple)
p mr_children * 3 #When multiplied by a numerical value

=> ["GIFT", "himawari", "simple","GIFT", "himawari", "simple","GIFT", "himawari", "simple"]
mr_children = %w(GIFT himawari simple)
p mr_children * ";" #When multiplied by a character string
=> "GIFT;himawari;simple"
#A string that concatenates the elements with the specified string in between is returned.

Regular expressions and scan method notes

s = "To be or not to be, that is the question." #Define a string in the variable s
hash = #Create an instance of Hash class (empty inside)
s.scan(/\w+/) {|i| hash[i] += 1}
#Get the matched string as an array. Pass the value of the array to the block variable i and define it as a hash key. Add the number 1 to the corresponding value.
p hash

=> {"To"=>1, "be"=>2, "or"=>1, "not"=>1, "to"=>1, "that"=>1, "is"=>1, "the"=>1, "question"=>1}
#The number of occurrences for each word was calculated.

This → / \ w + / memo \ w => Alphanumeric, underscore

The scan method returns an array of strings that match the regular expression. What it looks like before it is stored in the hash key

s = "To be or not to be, that is the question."
p s.scan(/\w+/)

=> ["To", "be", "or", "not", "to", "be", "that", "is", "the", "question"]

A state like sashimi carved by words.


Recommended Posts

Ruby learning 4
Ruby learning 5
Ruby learning 3
Ruby learning 2
Ruby learning 6
Ruby learning 1
Ruby Learning # 25 Comments
Ruby Learning # 1 Introduction
Ruby Learning # 34 Modules
Ruby Learning # 14 Hashes
Ruby Learning # 33 Inheritance
Ruby Learning # 15 Methods
Ruby Learning # 30 Initialize Method
Ruby learning points (basic)
Ruby Learning # 29 Classes & Objects
Ruby Learning # 24 Exponent Method
Ruby Learning # 17 If Statements
Ruby Learning # 21 While Loops
Ruby Learning # 31 Object Methods
Ruby Learning # 27 Writing Files
Ruby Learning # 35 Interactive Ruby (irb)
Ruby Learning # 9 Math & Numbers
Ruby Learning # 28 Handling Errors
Ruby Learning # 26 Reading Files
Ruby Learning # 23 For Loops
Ruby Learning # 16 Return Statement
Ruby Learning # 2 Drawing a Shape
Ruby on rails learning record -2020.10.03
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.09
Ruby Learning # 18 If Statements (Con't)
Ruby on Rails basic learning ①
Ruby Learning # 11 Building a Calculator
Ruby Learning # 99 Personal Programming Notebook
Ruby on rails learning record-2020.10.07 ②
Ruby Learning # 10 Getting User Input
Ruby Learning # 32 Building a Quiz
Ruby Learning # 8 Working With String
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
Java learning (0)
Ruby Learning # 19 Building a Better Calculator
[Ruby] Array
Ruby basics
Ruby basics
Ruby Review 2
Ruby addition
Refactoring Ruby
Completed Progate Ruby Learning Course II
Servlet learning
Ruby Learning # 22 Building a Guessing Game
Learning output ~ 11/3 ~
Ruby setting 2
Ruby problem ⑦
Maven learning
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby settings 1
Refactoring Ruby
Learning output