Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
11
Task/Collections/Ruby/collections-1.rb
Normal file
11
Task/Collections/Ruby/collections-1.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# creating an empty array and adding values
|
||||
a = [] #=> []
|
||||
a[0] = 1 #=> [1]
|
||||
a[3] = "abc" #=> [1, nil, nil, "abc"]
|
||||
a << 3.14 #=> [1, nil, nil, "abc", 3.14]
|
||||
|
||||
# creating an array with the constructor
|
||||
a = Array.new #=> []
|
||||
a = Array.new(3) #=> [nil, nil, nil]
|
||||
a = Array.new(3, 0) #=> [0, 0, 0]
|
||||
a = Array.new(3){|i| i*2} #=> [0, 2, 4]
|
||||
22
Task/Collections/Ruby/collections-2.rb
Normal file
22
Task/Collections/Ruby/collections-2.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# creating an empty hash
|
||||
h = {} #=> {}
|
||||
h["a"] = 1 #=> {"a"=>1}
|
||||
h["test"] = 2.4 #=> {"a"=>1, "test"=>2.4}
|
||||
h[3] = "Hello" #=> {"a"=>1, "test"=>2.4, 3=>"Hello"}
|
||||
h = {a:1, test:2.4, World!:"Hello"}
|
||||
#=> {:a=>1, :test=>2.4, :World!=>"Hello"}
|
||||
|
||||
# creating a hash with the constructor
|
||||
h = Hash.new #=> {} (default value : nil)
|
||||
p h[1] #=> nil
|
||||
h = Hash.new(0) #=> {} (default value : 0)
|
||||
p h[1] #=> 0
|
||||
p h #=> {}
|
||||
h = Hash.new{|hash, key| key.to_s}
|
||||
#=> {}
|
||||
p h[123] #=> "123"
|
||||
p h #=> {}
|
||||
h = Hash.new{|hash, key| hash[key] = "foo#{key}"}
|
||||
#=> {}
|
||||
p h[1] #=> "foo1"
|
||||
p h #=> {1=>"foo1"}
|
||||
22
Task/Collections/Ruby/collections-3.rb
Normal file
22
Task/Collections/Ruby/collections-3.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# creating a struct
|
||||
|
||||
Person = Struct.new(:name, :age, :sex)
|
||||
|
||||
a = Person.new("Peter", 15, :Man)
|
||||
p a[0] #=> "Peter"
|
||||
p a[:age] #=> 15
|
||||
p a.sex #=> :Man
|
||||
p a.to_a #=> ["Peter", 15, :Man]
|
||||
p a.to_h #=> {:name=>"Peter", :age=>15, :sex=>:Man}
|
||||
|
||||
b = Person.new
|
||||
p b #=> #<struct Person name=nil, age=nil, sex=nil>
|
||||
b.name = "Margaret"
|
||||
b["age"] = 18
|
||||
b[-1] = :Woman
|
||||
p b.values #=> ["Margaret", 18, :Woman]
|
||||
p b.members #=> [:name, :age, :sex]
|
||||
p b.size #=> 3
|
||||
|
||||
c = Person["Daniel", 22, :Man]
|
||||
p c.to_h #=> {:name=>"Daniel", :age=>22, :sex=>:Man}
|
||||
21
Task/Collections/Ruby/collections-4.rb
Normal file
21
Task/Collections/Ruby/collections-4.rb
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
require 'set'
|
||||
|
||||
# different ways of creating a set
|
||||
p s1 = Set[1, 2, 3, 4] #=> #<Set: {1, 2, 3, 4}>
|
||||
p s2 = [8, 6, 4, 2].to_set #=> #<Set: {8, 6, 4, 2}>
|
||||
p s3 = Set.new(1..4) {|x| x*2} #=> #<Set: {2, 4, 6, 8}>
|
||||
|
||||
# Union
|
||||
p s1 | s2 #=> #<Set: {1, 2, 3, 4, 8, 6}>
|
||||
# Intersection
|
||||
p s1 & s2 #=> #<Set: {4, 2}>
|
||||
# Difference
|
||||
p s1 - s2 #=> #<Set: {1, 3}>
|
||||
|
||||
p s1 ^ s2 #=> #<Set: {8, 6, 1, 3}>
|
||||
|
||||
p s2 == s3 #=> true
|
||||
|
||||
p s1.add(5) #=> #<Set: {1, 2, 3, 4, 5}>
|
||||
p s1 << 0 #=> #<Set: {1, 2, 3, 4, 5, 0}>
|
||||
p s1.delete(3) #=> #<Set: {1, 2, 4, 5, 0}>
|
||||
24
Task/Collections/Ruby/collections-5.rb
Normal file
24
Task/Collections/Ruby/collections-5.rb
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
require 'matrix'
|
||||
|
||||
# creating a matrix
|
||||
p m0 = Matrix.zero(3) #=> Matrix[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
|
||||
p m1 = Matrix.identity(3) #=> Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
|
||||
p m2 = Matrix[[11, 12], [21, 22]]
|
||||
#=> Matrix[[11, 12], [21, 22]]
|
||||
p m3 = Matrix.build(3) {|row, col| row - col}
|
||||
#=> Matrix[[0, -1, -2], [1, 0, -1], [2, 1, 0]]
|
||||
|
||||
p m2[0,0] #=> 11
|
||||
p m1 * 5 #=> Matrix[[5, 0, 0], [0, 5, 0], [0, 0, 5]]
|
||||
p m1 + m3 #=> Matrix[[1, -1, -2], [1, 1, -1], [2, 1, 1]]
|
||||
p m1 * m3 #=> Matrix[[0, -1, -2], [1, 0, -1], [2, 1, 0]]
|
||||
|
||||
# creating a Vector
|
||||
p v1 = Vector[1,3,5] #=> Vector[1, 3, 5]
|
||||
p v2 = Vector[0,1,2] #=> Vector[0, 1, 2]
|
||||
p v1[1] #=> 3
|
||||
p v1 * 2 #=> Vector[2, 6, 10]
|
||||
p v1 + v2 #=> Vector[1, 4, 7]
|
||||
|
||||
p m1 * v1 #=> Vector[1, 3, 5]
|
||||
p m3 * v1 #=> Vector[-13, -4, 5]
|
||||
23
Task/Collections/Ruby/collections-6.rb
Normal file
23
Task/Collections/Ruby/collections-6.rb
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
require 'ostruct'
|
||||
|
||||
# creating a OpenStruct
|
||||
ab = OpenStruct.new
|
||||
p ab #=> #<OpenStruct>
|
||||
ab.foo = 25
|
||||
p ab.foo #=> 25
|
||||
ab[:bar] = 2
|
||||
p ab["bar"] #=> 2
|
||||
p ab #=> #<OpenStruct foo=25, bar=2>
|
||||
ab.delete_field("foo")
|
||||
p ab.foo #=> nil
|
||||
p ab #=> #<OpenStruct bar=2>
|
||||
|
||||
p son = OpenStruct.new({ :name => "Thomas", :age => 3 })
|
||||
#=> #<OpenStruct name="Thomas", age=3>
|
||||
p son.name #=> "Thomas"
|
||||
p son[:age] #=> 3
|
||||
son.age += 1
|
||||
p son.age #=> 4
|
||||
son.items = ["candy","toy"]
|
||||
p son.items #=> ["candy","toy"]
|
||||
p son #=> #<OpenStruct name="Thomas", age=4, items=["candy", "toy"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue