The purpose of purchase is the following two. ① Creating a Rails application on Cloud9 ② Deploy
qiita.rb
a = [Variable 1,Variable 2,Variable 3]
・ Subscripts start from 0 ・ To retrieve variable 2, a [1]
○ Empty judgment
qiita.rb
a.empty
⇨ true if the judgment is empty
○ Search character judgment
qiita.rb
a.include?('Search character')
⇨ Returns true if the array contains search characters
○ Storage of continuous values in an array
qiita.rb
aa = (0..100).to_a
⇨ Store n or more specified information in an array
○ Operation to array
qiita.rb
Array name.pop
Array name.shift
Array name<<value
Array name.join (' ')
Array name.sort!.reverse!
From top to bottom ・ Erase the last value ・ Erase the first value ・ Add a value at the very end ・ Combine with one space for each subscript ・ Reverse the contents of the array
You can create an array of 2 variables by specifying the key.
qiita.rb
hash= {Key 1=>Value 1,Key 2=>Value 2,Key 3=>Value 3}
qiita.rb
puts hash['Key 1']
hash['Key 4'] =Value 4
hash['Key 1'] =Value 1
hash.delete('Key 1')
From top to bottom ・ Extraction of value ・ Add value -Overwrite value -Delete value
String on the source code Treated internally as an integer ⇨ Faster access inside the hash
qiita.rb
hash= {Key 1:Value 1,Key 2:Value 2,Key 3:Value 3}
qiita.rb
puts hash[:Key 1]
hash[:Key 4] =Value 4
hash[:Key 1] =Value 1`
hash.delete(:Key 1)
hash.has_key?(:The key you want to find n)
hash.size
From top to bottom ・ Extraction of value ・ Add value -Overwrite value -Delete value ・ Do you have the key you want to find? Confirm ・ How many keys do you have? Confirmation
qiita.rb
numbers = [1,2,3,4,5]
numbers.each do |number|
qiita.rb
scores = {luke: 100,jack: 90,robert: 70}
scores.each do |k,v|
puts v
puts "#{k},#{v}"
end
qiita.rb
class Car
def initialize(name)
puts 'initialize'
@name = name
end
def hello
puts "Hello! I am #{@name}."
end
end
boxter = Car.new('boxter')
boxter.hello
carrera = Car.new('carrera')
carrera.hello
[Explanation] -Variables when creating a class are capitalized for the first character -The argument specified when creating an instance can be specified as the return value of initialze. -Instance creation is like creating a clone when running a class. ⇨ Variable = class name.new () when declaring -Def initialize is the process executed when the instance is created. -The instance variable is the @ variable defined in initialize. ⇨ Can be used anywhere in Class
qiita.rb
class Car
def initialize(name)
@name = name
end
def hello
puts "Hello! I am #{@name}."
end
end
def name
@name
end
def name = (value)
@name = value
end
boxter = Car.new('boxter')
boxter.hello
boxter.name
boxter.name = '718boxter'
puts boxter.name
[Explanation] ・ If there is no def name part, @name cannot be accessed from outside the class. (The instance variable (@name) cannot be accessed from outside the class) -Accessible by executing the .name method after creating the instance ・ The value @name corresponding to the .name method can be obtained. -Call these'instance methods' (getter methods)
・ About def nama = (value) ⇨ By setting boxter.name ='boxter' after creating the instance Any value can be specified for @name. It can also be output (Description method peculiar to ruby)
qiita.rb
def name
@name
end
def name = (value)
@name = value
end
All
qiita.rb
attr_accessor :name
Can be achieved with
attr_accessor method ⇨ How developers don't have to write their own instance methods (Called property in other languages)
[When executing only reading]
qiita.rb
attr_reader :name
def name part can be described in one line ⇨ You can just pick up @name
[When executing only writing]
qiita.rb
attr_writer :name
def name = (value) part can be described in one line ⇨ You can only write to @name
qiita.rb
class Car
@@count
def initialize(name)
puts 'initialize'
@name = name
@@count += 1
end
def self.info
puts "Test"
end
def hello
puts "Hello! I am #{@name}."
end
end
-Variables used in Class ・ The starting character starts with @@ 2 characters -Variables required when dealing with variables that you do not want to be reset for each def ⇨ Effective when you want to count the number of instantiations ⇨ Effective when you want to count the number of method executions
qiita.rb
class Car
def initialize(name)
puts 'initialize'
@name = name
end
def self.info
puts "Test"
end
def hello
puts "Hello! I am #{@name}."
end
end
Car.info
boxter = Car.new('boxter')
boxter.hello
carrera = Car.new('carrera')
carrera.hello
[Explanation] -Write the method as self. method name -Methods that can be used without instantiation, such as Car.info ・ If it is a normal method, it should not be usable unless it is boxter.info
Recommended Posts