It's dirty with my own memo, but I'll summarize the points that are easy to make a mistake.
――Is there about 5 questions that are exactly the same as the mock? ――If you do a mock test, there are almost no problems at first sight. Even if I've been developing Ruby for 10 years, I think it will fail if I don't prepare for the test. ――Even if you arrive early, the test site will not be accepted. The name will be called when the time comes, and it will be accepted at this timing --When you finish the test, a pass / fail message will appear on the screen. ――If you repeat the mock test, you will be able to pass it. I thought I had to read the textbook, and I didn't have any difficult problems in the mock exam. It seems to be the exam you want to pass. Seems easier than the basic information test --The test cost is too high. Make it 5000. I know if the gold is 10,000. --The test was completed in 30 minutes. If you think that it is 16000 in 30 minutes, it feels like you are sucked in from pachislot. ――There are times when we run a free exam campaign for those who have failed once. --You can go home after the exam --There were many problems with array ――The venue in Shinjuku was crowded. ――It took about 50 hours. --I didn't know how to rearrange the array from the back, the character code of 1. If I exit with rescue after raise, does ensure work? ――12 You can make a mistake. 38 correct answers Suruhitsuyougaaru --In Ruby 2.0 and later, the default script encoding is UTF-8 --The block parameter of Hash # each is Array. --Hash # size returns the number of elements in hash --Enumerable # select Self collects and returns the elements that meet the block conditions. --Enumerable # find self returns the first element that meets the block condition. --Delete the argument element from Array # delete self. Returns the deleted element. Destructive method.
--split ('d'), split (/; |: /) Separate and put in the array --split No arguments are separated by a space --split ("") Split character by character --- split (",", 2) 2 is the number to separate ["1", "2,3,4,5"] --About exception handling --Pick up standard error and its subclasses if omitted in the Rescue clause -Ensure clause is always executed --RuntimeError exception if raise method does not specify an argument Occurs --There is also else. Executed when rescue is not executed. Write under resucue.
--Equivalent notation --Things starting from 0 --Error with Puts 090 etc.
--Operator --A && B ... B moves when A is true --A && B ... If A is false, B will move. - A || B ...B moves when A is false - A || B ...B does not work if A is true --A B or A | B ... A is true Demo false Demo B works
--Operator precedence is \ ** higher than \ *
--Ruby constants start with a capital letter --Can be changed but a warning is issued -No warning when adding with <<
--Ruby variable name ――The first letter must start with a lowercase letter or _. --Use alphabets or numbers after the second letter
--Variadic variable -Declare with * a etc. --Any number of arguments can be taken and the array is stored in a
--Initialize method is read and rewritten many times --The description location of Super can be any position. If you are overriding a method with the same name, the super is called.
--About loading external modules --When using math PI etc., it is necessary to load the Math module --Example of reading method - Include Math - Math::PI
--Singular methods are executed with priority over class extension methods
--..,… About operators - a = [1,2,3,4] - a[0..3] => [1,2,3,4] - a[0..-1] => [1,2,3,4] - a[0,3] => [1,2,3] --Time object, datetime object to manage date and time to format srtftime --Subtraction between Time objects is in seconds --File.join ('/', "aaa", "bbb")-> / aaa / bbb / does not mean to join with /. --File.join ("aaa", "bbb")-> aaa / bbb Usually this one
--When you want to change 65 to "A" - 65.chr --The opposite is "A" .ord --For statement and scope are not created, so internal and external variables can be accessed. --Each statement and scope are created. If you access the variables inside each from the outside, an error will occur. Variables outside each can be seen from inside
'abcdefg'['bc'] -> bc
'abcdefg'.slice('bc') -> bc
'abcdefg'['bd'] -> nil --'abcdefg'.slice! ('bc')-> bc, the contents change'adefg'
'abcdefg'[/bc/] -> bc
'abcdefg'.slice(/bc/) -> bc --'abcdefg'.slice! (/ bc /)-> bc, contents change'adefg' --sub ... Non-destructive, replace one! --gsub ... Non-destructive, replace all!
a='aabbccddeeffgg',
--a.delete ('be')-> aaccddffgg that returns the deleted result. Variable a does not change, non-destructive
--a.delete ('a-f') Range can be specified
--a.delete ('a-c','c-g') When there are multiple conditions, the character c that is common to both disappears.
a='abcd' --a.replace ('xyz') => xyz, a ='xyz' --Concatenation + << concat * --Category conversion ,! Destruction if attached --downcase, upcase, swapcase are inverted --Delete chomp ... \ r \ r \ n \ n, non-destructive --If there are multiple, one type disappears --If it disappears with chop, \ also disappears
reverse,reverse!
"abc\ndef\nghi".each{|c| puts c} --By default, every line break spans blocks. , Options can be changed --Same for each_line and line methods
a=1,2,3 -> [1,2,3] --Refer to array = [1,2,3], the contents do not change
[1,2,3].join(",") -> "1,2,3"
array=[1,2,3] --a [10] = 10, Tosult a [4-9] Hanil Gakhail -[] .uniq-> Duplicate deletion, non-destructive ,! Destruction if attached -[] .compact-> nil Delete, non-destructive ,! Destruction if attached
[1,2,3,4,5].map{|n| n*2} -> [2,4,6,8,10]
[1,2]*4 -> [1,2,1,2,1,2,1,2] --How to make a hash -{"foo" => 1} If the value is a string, add "". This is the only way to make it a string key. -{10 => 1} is also possible, a [10]-> 1 tonal
File.delete('readme' --FIle.rename ('before','after') after --IO.read ('readme', 5) Output 5 bytes from the beginning, read all files if not specified, and enter all as string in variable
IO.foreach('readme'7){ |line| puts line}Read line by line --open ('readme'). Readlines Read all files into an array line by line --io.gets Read one line --io.readline Read one line --IO output C write puts print --lineno => the number of times the gets method was called --rewind, file pointer to top, lineno = 0 --pos =? Move file pointer --io.seek (10) = io.seek (10, IO :: SEEK_SET) Move the pointer 10 bytes from the beginning --io.seek (10, IO :: SEEK_CUR) Move from current position --io.seek (10, IO :: SEEK_END) Move from the end of the file --Time.now, Time.new Returns a time object at the current time
[1,2,3,4,5].inject(0){|result,v| result + v ** 2} ->55 Tashizan Shitakekka
each_with_index{|item,i| i=0 color
(1..10).each_cons(3) ->
(1..10).each_slice(3) ->
[1,2,3,4,5].include?(3)-> true
find == detect
[1,2,3,4,5].seletc{|i| i % 2 == 0} ->[2,4]
["aaa","b","cc"].sort{|a,b| a.length <==> b.length} -> [ "b","cc","aaa"] --Here document -It is possible to indent the end line by writing with-'like <<-identifier'. --Usually an error occurs when opening a space
--Erase one \ r \ n with Chomp method --Erase the last character with the chop method --IO # rewind moves the file pointer to the beginning
--The Split method can specify a specific character string as a delimiter with an argument. You can also specify the number of arrays generated by the second argument.
--"0-5" is a range specification, but "8-" is not a range specification and 8 and-are deleted.
--eql is true if the strings are the same --equal sees if the objects are the same
-inject assigns an element to a block ...|i,j|
foo = [1,2,3]
bar = foo
p foo === bar # => true //Is it the same as equal?
Module Foo
def foo //Defined as an instance method
puts("foo")
end
end
Class Bar
extend Foo //Define the instance method of Foo module as a singular method of Bar class
end
Bar.foo #=>Can be used as foo
-Constants starting with the :: operator like :: Foo are constants defined at the top level
-slice! Refers to after slicing
--How to react to the end of file (EOF) --In case of gets, nil returns --For Readline, EOFError exception is thrown
--File # mtime method saves updates, but File :: Stat # mtime method does not save updates --File :: Stat class is a new method only Class method
--When removing an arbitrary extension from a file name whose file name and extension are separated by ".", Call File.basename with ". *" As the second argument. -"% 2d" of "% 2d% s" is a specification to insert a blank space if the number you want to output is less than 2 digits
(1..5).each_cons(3) {|arr| p arr }
# <Execution result>
# [1, 2, 3]
# [2, 3, 4]
# [3, 4, 5]
(1..10).each_slice(3) {|arr| p arr }
# <Execution result>
# [1, 2, 3]
# [4, 5, 6]
# [7, 8, 9]
# [10]
arr = [1,2].zip([3,4])
p arr
# <Execution result>
[[1, 3], [2, 4]]
a1 = [1,2,3]
a2 = [4,2,3]
p a1 - a2
# <Execution result>
[1]
p ({a: 100, b: 100}).invert
# <Execution result>
# {100 => :b}
1: s = ["one", "two", "three"]
2: s.shift
3: s.shift
4: s.unshift
5: s.push "four"
6: p s
# <Execution result>
# 1: ["one", "two", "three"]
# 2: ["two", "three"]
# 3: ["three"]
# 4: ["three"]
# 5: ["three", "four"]
p "Ruby on Rails".delete("Rails")
# <Execution result>
# "uby on "
a, b = 0 # a => 0, b => nil
c, a = 1 # c => 1, a => nil
a, d = 1, 2 # a => 1, d => 2
b, c = 3 # b => 3, c => nil
class Blog
def foo #Instance method
end
def self.foo #Class method(This self is the class itself(Blog)Point to)
end
def bar #Instance method
self.foo #Instance method foo is called(This self is the instance itself(Blog.new)Point to)
foo #Instance method foo is called
self.class.foo #Class method foo is called(With such a description, the class itself can be pointed to even in the instance method.)
end
def self.bar #Class method(This self refers to the class itself)
self.foo #Class method foo is called
foo #Class method foo is called
end
end
self = "My job is a sales"
class String
def change_job
self.replace("I will become an engineer!")
end
end
s = "My job is a sales"
s.change_job
p s
self is 4
class Interger
def aaaa
return self
end
end
print 4.aaaa
One character match matches
a = ['3p', '913', 'Zx', 'ssr', 'M7', 'W']
a.grep(/[A-Z0-9]/)
["3p", "913", "Zx", "M7", "W"]
Key sort sort1, non-destructive
h = { "def" => 2, "ghi" => 1, "abc" => 3 }
p h.sort
[["abc", 3], ["def", 2], ["ghi", 1]]
h = { "def" => 2, "ghi" => 1, "abc" => 3 }
p h.sort.reverse
[["ghi", 1], ["def", 2], ["abc", 3]]
value sort, non-destructive
ascending order
h = { "def" => 2, "ghi" => 1, "abc" => 3 }
p h.sort{ | a, b | a[1] <=> b[1] }
p h
[["ghi", 1], ["def", 2], ["abc", 3]]
descending order
h = { "def" => 2, "ghi" => 1, "abc" => 3, "ddd" => 5 }
p h.sort{ | a, b | b[1] <=> a[1] }
[["ddd", 5], ["abc", 3], ["def", 2], ["ghi", 1]]
a,to b-I don't understand the meaning of the pattern
What can be read with include? Add to the class that defines the instance method, instance variable, and constant information.
module Foo
Bar = "bar"
end
class Baz
include Foo
end
puts Baz::Bar #=> bar //Since it is a constant, the constant can be used for some reason. The method is new
What can be read by extend? Add the instance method of to the class as a singular method. Class method treatment
module Foo
def foo
puts("foo")
end
end
class Bar
extend Foo
end
Bar.foo #=> foo
a = "foo"
b = a
b.slice!(0, 1) ->f comes out.!Become a nano de oo
print(a, b) -> oooo
--The size of the machine pointer is 31 bits wide. 31 bits will be bignum --When you call the attr_accessor method in the definition of the Foo class, an instance variable with the name specified in the argument is created, and the write and read accessor methods for that instance variable are defined in the Foo class. --attr_accessor: foo-> instance variables
Dunce \ space does not break
File.open("hats.txt", "w") do |f|
f.puts(%w[Bowler Deerstalker Dunce\ cap Fedora Fez])
end
hats.txt
Bowler
Deerstalker
Dunce\ cap
Fedora
Fez
File.stat is a snapshot when opened
file = File.open("hello.rb", "w")
stat = file.stat
mtime1 = stat.mtime //onaji
file.puts("new data")
file.flush
mtime2 = stat.mtime // onaji
Current, parent
["tmp", "tmp/lang", "tmp/lang/ruby", "tmp/lang/python"].each do |dir|
Dir.mkdir(dir)
end
Dir.chdir("tmp/lang")
Dir.new(".").each do |entry| // . , .. , ruby,4 directories for python
filename = File.join(entry, "rocking.rb")
File.open(filename, "w")
end
Dir.rmdir("python")
--File.basename (filename, “. *”) // Take the extension from the file name
File.open("fancy.txt", "w") do |f|
f.write("R u b y\n")
f.puts(["u","b","y"]) //Array 1 line break
end
puts File.read("fancy.txt")
R u b y
u
b
y
File.split splits the string object's arguments into a directory part and a file part, removing trailing slashes from both. The two parts are returned as an array with two elements. The program in question returns the array [“/ home / john”, “bookmarks”]. The join method is called in an array that is the value returned by File.split. That is, Array # join is called, not File.join. Array # join simply joins a string to the elements of the array, so (d) is the correct answer.
p File.split("/home/john/bookmarks/").join
“/home/johnbookmarks”
--Do not mistake it for File.join
- [“/home/john”, “bookmarks”].join -> /home/johnbookmarks
- [“/home/john”, “bookmarks”].join("_") -> /home/
--odd? Is it odd?
--even? Is it even?
-In the block|a,0| |b,1| |c,2| .... --If the break method argument is specified, that value will be the value returned by the blocked method call. The index starts counting from 0, and the first odd index is 1. The each_with_index method ends the iteration by returning a "b" with an index of 1. Then, the returned "b" is assigned to the local variable foo.
foo = ('a'..'z').each_with_index {|i, n| break(i) if n.odd? }
p foo.succ
--0% 3 ... too much 0 ―― 3% 3 ... Not much 0 -6% 5 ... too much 1
--Sort of array ――One dimension --array.sort Ascending order - array.sort.reverse ――2D --In the case of "self <=> other" method, self and other are compared, and when self is large, it returns a positive integer, when it is equal, it returns a negative integer, and when it is small, it returns a negative integer. -
score = [["kobayashi", 86],["murata", 54],["azuma", 72]]
puts "a-shoujun"
p score.sort { |a, b| a[0] <=> b[0] }
p score.sort { |a, b| -a[0] <=> -b[0] } #The numbers are minus, but I don't understand the meaning
puts "a-koujun"
p score.sort { |a, b| a[0] <=> b[0] }.reverse
p score.sort { |a, b| b[0] <=> a[0] }
p score.sort { |a, b| -b[0] <=> -a[0] } # musi
puts "b-shoujun"
p score.sort { |a, b| a[1] <=> b[1] }
p score.sort { |a, b| -b[1] <=> -a[1] } # musi
puts "b-koujun"
p score.sort { |a, b| a[1] <=> b[1] }.reverse
p score.sort { |a, b| b[1] <=> a[1] }
p score.sort { |a, b| -a[1] <=> -b[1] } #Oboete Okou
a-shoujun
[["azuma", 72], ["kobayashi", 86], ["murata", 54]]
[["azuma", 72], ["kobayashi", 86], ["murata", 54]]
a-koujun
[["murata", 54], ["kobayashi", 86], ["azuma", 72]]
[["murata", 54], ["kobayashi", 86], ["azuma", 72]]
[["murata", 54], ["kobayashi", 86], ["azuma", 72]]
b-shoujun
[["murata", 54], ["azuma", 72], ["kobayashi", 86]]
[["murata", 54], ["azuma", 72], ["kobayashi", 86]]
b-koujun
[["kobayashi", 86], ["azuma", 72], ["murata", 54]]
[["kobayashi", 86], ["azuma", 72], ["murata", 54]]
[["kobayashi", 86], ["azuma", 72], ["murata", 54]]
Slash Ha doesn't seem to increase. /// A trigger that does not become
https="https://"
domain="example.jp"
dir="index.html"
puts File.join(https, domain, dir)
https://example.jp/index.html
# https:/ ->Sonomama
# https: -> https:/
true & a = "We" #Execute on the right side regardless of the left side
puts a # We
false | b = "are" #Execute on the right side regardless of the left side
puts b # are
2 == 1 && c = "Ruby" #Left side is false Nanode, Owar
puts "c=nil" if c.nil? #nil
5 > 3 || d = "Engineer" #The left side is true Nano Deowal
puts "d=nil" if d.nil? # nil
p = a + b + c + d # we + are +I get an error with nil
h = {:name => 'sato', :club => 'tennis'}.fetch(:name, 'error')
print h # sato
h = {:name => 'sato', :club => 'tennis'}.fetch(:nam, 'error')
print h # error
Recommended Posts