ryuchan00 / basic_programing

UEC
0 stars 0 forks source link

9 スピッツのライブ最高でした。。。。。 #9

Open hayato0718 opened 4 years ago

hayato0718 commented 4 years ago

まちがい


def experiment
    if x % 2 == 0
        100.times do
            x = rand(6) + 1
            y = rand(6) + 1
        end
        return n / 100
    end
end
hayato0718 commented 4 years ago

せいかい


ア def experiment 
エ   n = 0
ウ   100.times do 
コ     x = rand(6) + 1
サ     y = rand(6) + 1
ス     if x == y 
オ       n = n + 1
イ     end 
イ   end 
キ   return n
イ end 
hayato0718 commented 4 years ago

まちがい


ア def experiment
ケ     n = 0
ウ     1000.times do
カ         x = rand(6)
オ         y = rand
ネ         if x < 0.3
ハ             if x < y
イ             end
イ         end
イ     end
フ     return n
イ end
hayato0718 commented 4 years ago

せいかい


ア def experiment
キ   a = [0,0,0,0,0,0]
ウ   1000.times do
カ     x = rand(6)
ツ     a[x] = a[x] + 1
イ   end
ヘ   return a
イ end
ryuchan00 commented 4 years ago

伊東さんがテストを解けるようになるには

メソッドの形を作る

def hoge
end

この形を最初に作る

returnを入れる

上のコードにreturnを追加する

def hoge
  return a
end

変数を初期化する、つまりreturnで返すような変数などを定義する

def hoge
  a = [0,0]
  return a
end

繰り返しを定義する

def hoge 
  a = [0,0]
  10.times do
    # 何か処理
  end
  return a
end
end
hayato0718 commented 4 years ago

すみません。 この形を頭に叩き込みます。。。

ryuchan00 commented 4 years ago
class Dog
  def initialize(name)
    @name = name
    @speed = 0.0
    @count = 3
  end

  def talk
    puts "my name is #{@name}"
  end

  def addspeed(d)
    @speed = @speed + d
    puts "speed = #{@speed.to_s}"
  end

  def bark
    @count.times do
      puts 'bark!'
    end
  end

  def setcount(n)
    @count = n
  end
end
hayato0718 commented 4 years ago

bonchi-fried@LAPTOP-K8Q6CM8A:~$ cd /mnt/c bonchi-fried@LAPTOP-K8Q6CM8A:/mnt/c$ cd users bonchi-fried@LAPTOP-K8Q6CM8A:/mnt/c/users$ cd bonch bonchi-fried@LAPTOP-K8Q6CM8A:/mnt/c/users/bonch$ cd documents bonchi-fried@LAPTOP-K8Q6CM8A:/mnt/c/users/bonch/documents$ ls 'My Music' 'My Pictures' 'My Videos' 'Office のカスタム テンプレート' Temporal desktop.ini repo 雑多 bonchi-fried@LAPTOP-K8Q6CM8A:/mnt/c/users/bonch/documents$ cd repo bonchi-fried@LAPTOP-K8Q6CM8A:/mnt/c/users/bonch/documents/repo$ ls fp19 化学 物理 英語 bonchi-fried@LAPTOP-K8Q6CM8A:/mnt/c/users/bonch/documents/repo$ cd fp19 bonchi-fried@LAPTOP-K8Q6CM8A:/mnt/c/users/bonch/documents/repo/fp19$ ls 6 7 8 9 tex bonchi-fried@LAPTOP-K8Q6CM8A:/mnt/c/users/bonch/documents/repo/fp19$ cd 9 bonchi-fried@LAPTOP-K8Q6CM8A:/mnt/c/users/bonch/documents/repo/fp19/9$ irb irb(main):001:0> load 'ensyuu1.rb' Traceback (most recent call last): 3: from /usr/bin/irb:11:in <main>' 2: from (irb):1 1: from (irb):1:inload' LoadError (cannot load such file -- ensyuu1.rb) irb(main):002:0> load 'ensyuu1.rb' Traceback (most recent call last): 3: from /usr/bin/irb:11:in <main>' 2: from (irb):2 1: from (irb):2:inload' LoadError (cannot load such file -- ensyuu1.rb) irb(main):003:0> load 'ensyu1.rb' Traceback (most recent call last): 3: from /usr/bin/irb:11:in <main>' 2: from (irb):3 1: from (irb):3:inload' LoadError (cannot load such file -- ensyu1.rb) irb(main):004:0> quit bonchi-fried@LAPTOP-K8Q6CM8A:/mnt/c/users/bonch/documents/repo/fp19/9$ irb irb(main):001:0> load 'ensyu1.rb' => true irb(main):002:0> a = Dog.new('dio') => #<Dog:0x00007fffcd712440 @name="dio", @speed=0.0> irb(main):003:0> a.talk my name isdio => nil irb(main):004:0> a = Dog.new('Dio') => #<Dog:0x00007fffcd6da5e0 @name="Dio", @speed=0.0> irb(main):005:0> irb(main):006:0> a.talk my name isDio => nil irb(main):007:0> a.addspeed(7.0) Traceback (most recent call last): 3: from /usr/bin/irb:11:in <main>' 2: from (irb):7 1: from ensyu1.rb:11:inaddspeed' NameError (undefined local variable or method ’' for #<Dog:0x00007fffcd6da5e0 @name="Dio", @speed=7.0>) irb(main):008:0> load 'ensyu1.rb' => true irb(main):009:0> a.addspeed(7.0) Traceback (most recent call last): 3: from /usr/bin/irb:11:in

' 2: from (irb):9 1: from ensyu1.rb:11:in addspeed' NameError (undefined local variable or method’' for #<Dog:0x00007fffcd6da5e0 @name="Dio", @speed=14.0>) irb(main):010:0> a.addspeed(5.0) Traceback (most recent call last): 3: from /usr/bin/irb:11:in <main>' 2: from (irb):10 1: from ensyu1.rb:11:inaddspeed' NameError (undefined local variable or method ’' for #<Dog:0x00007fffcd6da5e0 @name="Dio", @speed=19.0>) irb(main):011:0> load 'ensyu1.rb' => true irb(main):012:0> a.addspeed(5.0) speed = 24.0 => nil irb(main):013:0> load 'ensyu1.rb' Traceback (most recent call last): 3: from /usr/bin/irb:11:in
' 2: from (irb):13 1: from (irb):13:in load' SyntaxError (ensyu1.rb:17:@' without identifiers is not allowed as an instance variable name) ensyu1.rb:17: syntax error, unexpected end-of-input, expecting keyword_end @ ^ irb(main):014:0> load 'ensyu1.rb' => true irb(main):015:0> Dog.new('a') initializeメソッドが実行された => #<Dog:0x00007fffcd6b8bc0 @name="a", @speed=0.0> irb(main):016:0> Dog.new('a').setcount(1) initializeメソッドが実行された Traceback (most recent call last): 2: from /usr/bin/irb:11:in <main>' 1: from (irb):16 NoMethodError (undefined methodsetcount' for #<Dog:0x00007fffcd6b5b50 @name="a", @speed=0.0>) Did you mean? settcount irb(main):017:0> Dog.new('a').setcount(1) initializeメソッドが実行された Traceback (most recent call last): 2: from /usr/bin/irb:11:in <main>' 1: from (irb):17 NoMethodError (undefined methodsetcount' for #<Dog:0x00007fffcd6db8c8 @name="a", @speed=0.0>) Did you mean? settcount irb(main):018:0> Dog.new('a').settcount(1) initializeメソッドが実行された => 1 irb(main):019:0> load 'ensyu1.rb' => true irb(main):020:0> bark] Traceback (most recent call last): 1: from /usr/bin/irb:11:in <main>' SyntaxError ((irb):20: syntax error, unexpected ']', expecting end-of-input) bark] ^ irb(main):021:0> a =Dog.new('a') initializeメソッドが実行された => #<Dog:0x00007fffcd67a230 @name="a", @speed=0.0, @count=3.0> irb(main):022:0> a => #<Dog:0x00007fffcd67a230 @name="a", @speed=0.0, @count=3.0> irb(main):023:0> a.bark Traceback (most recent call last): 3: from /usr/bin/irb:11:in
' 2: from (irb):23 1: from ensyu1.rb:25:in bark' NoMethodError (undefined methodtimes' for 3.0:Float) irb(main):024:0> irb(main):025:0> load 'ensyu1.rb' => true irb(main):026:0> a =Dog.new('a') initializeメソッドが実行された => #<Dog:0x00007fffcd59e7a8 @name="a", @speed=0.0, @count=3> irb(main):027:0> a.bark bark bark bark => 3 irb(main):028:0> a.setcaount Traceback (most recent call last): 2: from /usr/bin/irb:11:in <main>' 1: from (irb):28 NoMethodError (undefined methodsetcaount' for #<Dog:0x00007fffcd59e7a8 @name="a", @speed=0.0, @count=3>) Did you mean? setcount settcount irb(main):029:0> a.setcount Traceback (most recent call last): 3: from /usr/bin/irb:11:in <main>' 2: from (irb):29 1: from ensyu1.rb:15:insetcount' ArgumentError (wrong number of arguments (given 0, expected 1)) irb(main):030:0> a.setcount 2 => 2 irb(main):031:0> a.bark bark bark => 2 irb(main):032:0>

hayato0718 commented 4 years ago

class Dog
  def initialize(name)
    puts 'initializeメソッドが実行された'
    @name = name
    @speed = 0.0
    @count = 3
  end
  def talk
    puts('my name is' + @name)
  end
  def addspeed(d)
    @speed = @speed + d
    puts('speed = ' + @speed.to_s)
  end
  def setcount(n)
    #回数を渡す
    @count = n 

    return (n)

  end

  def bark
    #3回吠えさせる
    @count.times do
      puts ('bark')
    end
  end

end
ryuchan00 commented 4 years ago

Concatクラスの実行

irb#1(9/concat.rb):010:0> load '9/concat.rb'
=> true
irb#1(9/concat.rb):011:0> c = Concat.new
=> #<Concat:0x00007f955f88bd90 @sentence="">
irb#1(9/concat.rb):012:0> c.add('This')
=> "This"
irb#1(9/concat.rb):013:0> c.add('is')
=> "Thisis"
irb#1(9/concat.rb):014:0> c.get
=> "Thisis"
irb#1(9/concat.rb):015:0> c.add('a')
=> "Thisisa"
irb#1(9/concat.rb):016:0> c.reset
=> ""
irb#1(9/concat.rb):017:0> c.add('pen')
=> "pen"
irb#1(9/concat.rb):018:0> c.get
=> "pen"
irb#1(9/concat.rb):019:0>
ryuchan00 commented 4 years ago

Memory2クラスの実行

irb(main):065:0> m2 = Memory2.new
=> #<Memory2:0x00007f8acc8dc238 @numbers=[]>
irb(main):066:0> m2.put(1)
=> 1
irb(main):067:0> m2.put(3)
=> 3
irb(main):068:0> m2.put(5)
=> 5
irb(main):069:0> m2.get
=> 5
irb(main):070:0> m2.get
=> 3
irb(main):071:0> m2.get
=> nil
irb(main):072:0> m2.put(7)
=> 7
irb(main):073:0> m2.put(9)
=> 9
irb(main):074:0> m2.get
=> 9
irb(main):075:0> m2.put(11)
=> 11
irb(main):076:0> m2.get
=> 11
irb(main):077:0> m2.get
=> 7
irb(main):078:0> m2.get
=> nil
ryuchan00 commented 4 years ago

演習5向け Stringクラスの拡張

class String
  def add_exclamation
    self + "!"
  end
end
irb(main):080:0> load '9/string.rb'
=> true
irb(main):081:0> 'a'.add_exclamation
=> "a!"
ryuchan00 commented 4 years ago

演習3の実行

irb(main):002:0> a = Ratio.new(3, 5)
=> #<Ratio:0x00007fcbfa8539f0 @a=3, @b=5>
irb(main):003:0> a
=> #<Ratio:0x00007fcbfa8539f0 @a=3, @b=5>

irb(main):007:0> b = Ratio.new(8, 7)
=> #<Ratio:0x00007fcbf9109880 @a=8, @b=7>
irb(main):008:0> b
irb(main):015:0> puts a + b
61/35
=> nil
irb(main):016:0> puts a - b
-19/35
=> nil
irb(main):017:0> puts a * b
24/35
=> nil
irb(main):018:0> puts a / b
21/40
=> nil