Ȼ/09

Top / Ȼ / 09

Ruby:

Ruby Processing Ͼܳɤʥץߥ󥰸ǡ䲻ν׻ʸŪư˸Ƥ¤ Ĥޤꡤ



Ruby Ǥõʤϻزʸʡ

ʬǽѤ뤿μʤɤ򾯤ݤ˴뤫⤷ʤΤǿۤפ



: ѤΥǥ쥯ȥäƤ

Processing κǽλƱ͡Ruby Ѥ˥ǥ쥯ȥۡǥ쥯ȥβ˺äƤ web ǤϤΥǥ쥯ȥ̾ lec_ruby ȤƤ(ϼʬǹ̾Ĥ褦. ե٥åȤʳȤȤȤϫ뤫).



Ruby: Ƥ Ruby

Ruby ǤΥץߥ󥰤Τϰʲη֤



  1. ruby ץ¸Ƥƥȥե lec_ruby ˺롥
  2. ƥȥǥǤΥե˥ץ񤭹(or )
    warning.png ƥȥǥΤˤƤʤȤǺ롥줫褦ʵǤϤ꺤͡
  3. ruby ˤΥƥȥեϤȡruby ɤ߹Ǽ¹Ԥ롥
  4. η̤˰ͫ
  5. 2. äƷ֤

ޤ괷Ȥ櫓ǡʲ˱äƤäƤߤ褦

notes.png μ˽äơºݤˤäƤߤ褦.

  1. ޤ Ruby ƤӽФưƤ餦Ķ
    ؤ PC ȤΡִĶפȤ cygwin ȤΤǡcygwin Ω夲褦(˥塼õƤߤ褦) ƻͳѤǤõʤ̤ɡʤʤɤơĤϤƯԤʤΤ
    öΩ夲顤ȤޤǤΤޤΩ夲Ƥ

  2. cygwin ˡruby ѥǥ쥯ȥ˰ưƤ餪
    cygwin ϡ֤ɤΥǥ쥯ȥǺȤΤפȤǰäƤΤǡǼʬѰդǥ쥯ȥѤ褦 ˤϡcygwin ưƽФƤ̤

    cd ~/lec_ruby

    ȤФ褤(äǥ쥯ȥ̾lec_rubyξ硥ʲƱ͡) ʤߤˡ cd change directory άǡ~ ϥۡǥ쥯ȥΤȤ

  3. ˥ץ񤯾 : ΥץƤƥȥե
    test.rb Ȥ̾ΥեꤿСcygwin

    touch test.rb

    Ȥɤ

  4. Υƥȥե˥ץ񤭹ि˥ƥȥǥΩ夲褦.
    ˼ʬλȤƥȥǥǤϤư褦
    ؤ PC äƤʤΤͤ⤤뤫⤷ʤ ͤŬʤΤܤ

  5. ƤΥץ񤳤
    ƥȥǥǼΤ褦˰Ԥ񤭤ǡ¸褦.

    p 3+5

    warning.png ιԤ򤭤ȡֲԡפƤ! ԤƤʤԤǧ줺˥ץȤɤ߹ޤʤ⤷ʤ

  6. ץưƤߤ褦
    cygwin ǡ

    ruby -w test.rb

    ǤäƤߤ褦. ޤǤκȤ꤬ʤ
    8
    Ȥ֤äƤϤ



OK.png Ȥϡץɤɤɤ뤿
֥եΥץľ¸ ruby -w ե̾
򹥤ʤ֤Ф褤



ĶǤ Ruby ʸˡ(1)

Ƥǡɬפμȡ ˳ؤǰ Ruby Τȡ Τʤäɤ⤹狼μ 񤤤Ƥ

ʬɤ, Processing ӤȽʸˡ˴ؤƸС



ƱǽˤĤƤϡ Ruby ñ˽񤱤

ɬפμ

̾,ץ
ؤط׻ԤʤץƬԤˤĤⱦΤ褦˽񤳤(1Ԥ)include Math
warning.png ѿפʤ补ʤǻϤ褦. ѿ˼¿ȼ¿ǡˤʤ补
warning.png ; ʤ󤫤Ϥפʤ补
warning.png ѿϤᡤruby Ǥʸξʸ϶̤
warning.png ѿ̾ʸǽ񤯤ȡȤѹϤǤʤ
warning.png 礭ʥץΤǻȤѿ $ ǻϤ᤿̾ˤ

⤦ΤäƤ복ǰ

̾,ץ
Ƭ # ǻϤȡruby ˤιԤ̵뤵# ۤˤ
ɽ1̤ƤФp ۤˤ
ѿʤɤa = 3.5
ѿ­-a += 1 (a 1­)
ѿ򥳥ԡ-a = b (a b Ȥ򥳥ԡ)
ݤ-a*b
¿γ껻-5.0/2 (2.5 ˤʤ)
γ껻-5/2 (2 ˤʤ)
;-5 % 2 (1 ˤʤ)
٤-2**3 (8ˤʤ)
ʤ鷫֤롼פ. ϰϤ 1..5 ߤ˽񤱤for i in 1..5 do ۤˤ end
פƤ?Ӥa == b
郎ʤưif ۤˤ then ۤ else ۤ end
ñʤ뽸(ȻפäƤ), Ƕڤäơ[] Ȥ¤٤[1.2,a,b,3] ʤ
warning.png äѤ1Τդa[3] 4ܤǤؤ
0ʾ1̤rand()

Τʤäɤ⤹ʬ

̾,ץ
-a.abs (a )
ڤ夲-a.ceil (a ڤ夲)
ппlog(a)
ؿ-exp(a)
ʸDzʸs.length
硤ʸ򥳥ԡʸԡüa = b.dup(a b Ȥ򥳥ԡ)
κ(1)Ȥ˷ޤäƤȤa = [1,8,2.3]
κ(2)礭ϷޤäƤɡĤȤȤa = Array.new(3){0}(3ĤǤ硥Ǥϲ 0 ȤƤ)
κ(3)ȤꤢФȤȤa = []
ǿ-group.size
ɲ(ǽ)-group.unshift(a)(Ƭ a ɲ)
ɲ(Ǹ)-group.push(a)(κǸ a ɲ)
Ǻ(ǽ)-group.shift(ƬǤ)
Ǻ(Ǹ)-group.pop(κǸ)
, ǾȤκ, Ǿ[3,5,2].min ʤ

ñʥץ: ʿ

οʿѤȤäƤߤ褦( 1.3 ˤʤϤ).

1.2, -3.0, 3.3, 4.1, 0.9

ä񤷤르ꥺȤߤͤɬפϤʤ



­ơǿdzäɽ

Ǥ.

ruby ץ˾Ҳ𤷤ǽǽ񤤤ƤߤȼΤ褦ʤΤˤʤ.

  1. include Math
  2.  
  3. a = [1.2, -3.0, 3.3, 4.1, 0.9]
  4.  
  5. sum = 0.0
  6. for i in 1..a.size do
  7.   sum += a[i-1]
  8.   end
  9.  
  10. average = sum/a.size
  11.  
  12. p average

notes.png ½
ΥץưƤߤ褦

notes.png ½
Υץ򤷤褦
餯­פȤؼԤˤ񤷤äꤹ. ѤäȤ狼ʤƤ褤顤Υץ򤸤äɤ

Ƥ뤫򤷤Ƥ




ʬ׻ץ񤤤ư
οʬȤäƤߤ褦( 6.1 ˤʤϤ).

1.2, -3.0, 3.3, 4.1, 0.9




ߤʤˤʤ롤кͤ׻ץ񤤤ư
10ͤΤ褦ʾ硤줾кͤ׻Ƥߤ褦(кͤΤʤͤϸ褦äγǰ)

72, 30, 23, 41, 9, 55, 18, 80, 15, 2



ݡ

ʲβˤĤơ긭Ĵȹͻȼ¹Ԥ򤷡

ExpMath1-Report-09

Ȥ̾Ĥ e-mail ˤƶ˥ݡȤȤФ补ʤƼ

  1. °(ز)
  2. ֹ
  3. ǯ
  4. ̾

񤯤Τ˺ʤ褦.

warning.png ʬΥݡȺġ륻åȤ򵭺ܤ(󡤺ΥݡȤ⤽Υġ륻åȤȤäƺ뤳)

ݡȲ

  1. ½ǽФƤФƤΡʬβץȡη̤򼨤
  2. 1. Υץ־ܺ٤ˡײ⤻补

about Icons, ClipArts

For details, see JNorth_arrow-right-sm.png this.