2011年5月29日日曜日

eclipseでRuby環境を作成

色々とeclipseとRubyの開発環境構築のサイトはありますが、
情報がまちまちだったり
ダウンロードサイトが変わっていたりと、、、、
一応 私が2011年05月に開発環境を構築した手順を掲載いたします。

①http://mergedoc.sourceforge.jp/より
Full All in One (JRE あり)の Ultimateをダウンロードします。
すでに日本語化もしてあります。

②ダウンロードしたeclipse.exeをクリックしインストします

③ 次にeclipseのメニューより「ウインドウ」→「設定」→「一般」→「機能」→「クラシック更新」→
「適用」 →「Ok」

④ 次にeclipseのメニューより「ヘルプ」→「ソフトウエアの更新」→「検索とインストール」
→「追加」→「http://download.aptana.com/tools/radrails/plugin/install/radrails-bundle」と入力し
後は指示にしたがってインストールします。
これで完了です。
おつかれさまでした。


2011年5月27日金曜日

スタックとキュー とは?と多重配列



arr = [],[]
p arr[0][0] = 12
p arr[0][1] = 11
p arr[0][2] = 11
p arr[0][3] = 13

p arr[1][0] = 02
p arr[1][1] = 01
p arr[1][2] = 01
p arr[1][3] = 03
p arr
p arr << [1,2]
p arr 


###結果####
12
11
11
13
2
1
1
3
[[12, 11, 11, 13], [2, 1, 1, 3]]
[[12, 11, 11, 13], [2, 1, 1, 3], [1, 2]]
[[12, 11, 11, 13], [2, 1, 1, 3], [1, 2]]

#####用語#####
スタックポインターとは?
データの位置を格納しているポインター

プッシュとは?
データをスタック領域に格納する作業

キューとは?
行列です

エンキューとは?
 配列に追加

デキューとは?
配列から削除

2011年5月23日月曜日

Ruby言語 でじゃんけん

pc = ["グー","チョキ","パー"]
pcnum = rand(3)
puts "PCは#{pc[pcnum]}"
puts "私は#{pc[name]}"
puts "シャンケンの判定は"
ans = ((name - pcnum)+3)%3//
if(ans == 0)
puts "アイコ"
elsif(ans == 2)
puts "勝ち"
elsif(ans == 1)
puts "負け"
end

Ruby言語 ループ

p "■■■hello1■■■"
def hello1(num,i=0)
(i..num-1).each do |i|
puts "Hello world"
end
end
hello1(3)

p "■■■hello2■■■"
def hello2(num)
array = ["Hello world"]*num
array.each do |item|
p item
end
end
hello2(3)

p "■■■hello3■■■"
def hello3(num)
num.times do
puts "Hello World"
end
end
hello3(3)

p "■■■hello4■■■"

def hello4 (num,str,i=0)
while i < num puts str i +=1 end end hello4(3,"Hello") p "■■■hello5■■■" def hello5(num) 1.upto(num) do puts "Hello World" end end hello5(3) p "■■■file■■■" File.open("hello.txt","r") do |txt| puts txt.read end p "■■■file2■■■" File.open("hello.txt","r") do |txt| txt.each_line do |line| puts line end end


2011年5月17日火曜日

Ruby 変数と定数とスコープ

puts "###############試験対策ソース###############"

puts "###############変数と定数とスコープ1###############"
x = 1
def ans
x = 4 #xを初期設定していない場合、xを呼び出す場合NameErrorが返されます。
puts x #=>4 xのスコープはメソッド内だけです。
end
ans
puts x #=>1


puts "###############変数と定数とスコープ2###############"

def ans2
x = 100 if false #この場合xへの代入は実行されないが、宣言はされます。
puts x #=>nil
end

ans2

puts "###############変数と定数とスコープ3###############"

def ans3
x = 0 #ブロックの外で初期設定
(0 ... 11).each do |i|
puts x += i #ブロック内でも有効です
end
end

ans3

puts "###############変数と定数とスコープ4###############"
def ans4
o = 0 #初期化を行っていない変数に代入を行うとNameErrarが吐き出されます。
for i in 1..10
o += i
end
puts o
end

ans4
★★★★★★実行結果★★★★★★
###############試験対策メ[ス###############
###############変数と定数とスコープ1###############
4
1
###############変数と定数とスコープ2###############
nil
###############変数と定数とスコープ3###############
0
1
3
6
10
15
21
28
36
45
55
###############変数と定数とスコープ4###############
55

2011年5月16日月曜日

Ruby技術者認定試験対策 その①

puts "##############Ruby真偽#################2
puts "■■■■■■■■真偽ソース■■■■■■■■■"
puts "########  0  #######"
num = 0
if num
    puts "0 は true"
else
    puts "0 は false"
end

puts "########  ''  #######"


str = ''
if str
    puts "空文字列はtrue"
else
    puts "空文字列はfalse"
end

puts "########  nil  #######"

ni = nil

if ni
    puts "nilはtrue"
else
    puts "nilはfalse"
end

puts "########  false  #######"

fal = false

if fal
    puts "falseはtrue"
else
    puts "flaseはfalse"
end

puts "########  true  #######"

tr = true

if tr
    puts "trueはtrue"
else
    puts "trueはfalse"
end

puts "########  null  #######"

nulls = "null"

if nulls
    puts "nullはtrue"
else
    puts "nullはfalse"
end

puts "★★★★★★★ 結果 ★★★★★★★"
########  0  #######
0 は true
########  ''  #######
空文字列はtrue
########  nil  #######
nilはfalse
########  false  #######
flaseはfalse
########  true  #######
trueはtrue
########  null  #######
nullはtrue

///////  まとめ  ////////
Rubyではnilとfalse以外はtrueになります。

最近見たWeb開発で役にたつサイトをまとめた

#############################注目PC##############################
9,800円のAndroidノートが発売CPUはVIA製
http://akiba-pc.watch.impress.co.jp/hotline/20110514/etc_via.html 

#############################注目HTML便利サイト##############################
CSSの知識をもっと深める30+2の小技テクニック集
http://weboook.blog22.fc2.com/blog-entry-260.html



ブラウザのデフォルトのスタイルをCSSでリセットする方法
http://ps60.blog109.fc2.com/blog-entry-31.html



#############################注目統合開発便利サイト##############################
TML/CSS/PHP等のコーディングに、無料のNetBeansが快適過ぎる件
http://stocker.jp/diary/netbeans/


#############################注目SEO対策サイト##############################
WEB屋が使う最高の無料SEOツール40個
http://swat9.com/seo/seo%E3%83%84%E3%83%BC%E3%83%AB40/


#############################注目仮想環境便利サイト##############################
無料の仮想化ソフト「VirtualBox」を使ってWindows上でLinuxなどを動作させる方法
http://9jp.info/archives/9319


#############################注目サーバ管理サイト##############################
ウェブ開発者のための、1時間でできるLAMP環境構築術(CentOS編)http://tanaka.sakura.ad.jp/2011/05/centos-linux-apache-php-perl-mysql-lamp.html

#############################注目Rubyサイト##############################
Ruby入門勉強ルームhttp://www.mapee.jp/ruby/

2011年5月12日木曜日

XHTMLの注意点

##############XHTM1.0宣言文 ###########
<?xml version=”1.0″ encoding=UTF-8″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<!-- 空間名の指定 -->
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”ja” lang=”ja”>




############## 終了タグ ###########

XHTMLでは終了タグの省略はできません。
ですので
<br /><img src="xxxx" />と記述し、
pタグなどもきちんと閉じましょう。

############## 属性値 ###########

属性値はダブルコーテイションで囲みましょう。
また、属性名と属性値が同じ時、HTMLは省略記述できますが、
XHTMLでは省略できません、

############## 要素名 属性名 ###########
XHTMLでは要素名と属性名は小文字で記述しましょう。




2011年5月11日水曜日

Ruby言語 ファイル

puts "################出入力###################"
puts "引数に1 2 3を渡す"
expression = ARGV.join(' + ')   #joinで文字列の結合
total = ARGV.inject(0){|subtotal, arg|   #inject(0)はsubtotalの初期値で、argに要素を渡します。
    p subtotal
    p arg
    subtotal + arg.to_i
}

puts expression   #=>1 + 2 + 3
puts total    #=>6


puts "################ファイル###################"

i = 0
File.open("opentext.txt"){|text|  #openでファイルを開きます
    puts "読み込み開始します"
    contents = text.read
    print contents + "\n"
    puts "読み込み終了します"
}


puts "################ファイル###################"

File.open("opentext.txt",'a'){|text|
    puts "書き込み開始します"
    text.puts Time.now
    puts "書き込み終了します"
}

puts "################ファイル###################"

File.open("opentext2.txt",File::WRONLY|File::CREAT){|text|
    puts "書き込み開始します"
    text.puts Time.now
    puts "書き込み終了します"
}


puts "################ファイル###################"
File.open("opentext2.txt"){|text|
    puts "読み込み開始します"
    text.each_line do |line|   #一行ずつ読み込み ブロックに行の内容を渡す。
        p text.lineno    #現在の行番号
        if(line == "\n")
            puts "\\nです"
        end
        p line#.chomp    #現在の行を表示chompは改行を削除します
    end
    puts "読み込み終了します"
}


puts "################ファイル###################"
File.open("opentext2.txt",'r+b'){|text|
    print "#{text.pos}"    #ファイルポインタの現在の位置を整数で返します
    print text.gets
    print "#{text.pos}"
    puts bytes = text.read(3)
    print "#{text.pos}:\n"
    text.pos -= 3
    print "#{text.pos}:\n"
    text.write bytes
    print "#{text.pos}"
    text.pos = 0
    print text.gets    #1行取得
    print "#{text.pos}:"
    text.seek(-3,File::SEEK_END)#最後の行へ移動し3バイト戻る
    print "#{text.pos}"
    puts text.getc     #1バイト読み込み
    print "#{text.pos}"
    p text.getc
    print "#{text.pos}"
    #readcharは読み取るバイトが存在しないとき例外を発生することを除き同じ動き
    puts text.readchar #読み取り行がない場合例外
}


puts "################ファイル###################"

p $stdin
p $stdout
p $stderr
$stderr.printf("%X\n",0xcafe)


puts "################ファイル###################"
File.open("opentext2.txt",'w'){|text|
    p text
    $stdout = text    #標準出力先をコマンドラインから指定ファイルに変更
    p $stdout
    puts "Welcome to Glubbdubdrib"
    p text
    p STDOUT
    $stdout = STDOUT  #標準出力をもとにもどす
    p $stdout
}

puts "################ファイルIO###################"

require 'stringio'#stringioの読み込み、StringIOを使うため
buffer = String.new#StringIOで使うStringオブジェクト bufferとはデータの蓄積先

sio = StringIO.new(buffer)#StringIOに渡す
p $stdout
$stderr = $stdout = sio#出力先を変更
puts "untuh"
warn "outed"
$stdout = STDOUT
puts buffer
puts sio
$stderr = STDOUT

Ruby 単語説明

名前の検索とは?
 オブジェクトにメッセージが渡された時の振る舞い、狭いスコープ範囲から広い範囲へと探していく。

*特異メソッドとは


*メソッドのオーバーロード? Rubyはオーバーロードはありません


*メタプログラミングとは?


*特異クラスとは?


*ダックタイピングとは? オブジェクトが同じように振舞うなら区別する必要はないと考える意味

Ruby言語 オブジェクトとクラス

puts "##########アクセス########1"      

class Yapoo

    def public_method; end
   
    private
    def internal_use
        puts "成功"
     end
   
    public
    def public_api
        return internal_use
    end
end
    yapoo = Yapoo.new
    yapoo.public_api
    #yapoo.internal_use
   
   
puts "##########特異メソッド 特異クラス########"
     
message = "Hello"
def message.build_greeting(target)  #特異メソッド オブジェクト専用のメソッド
    #return "#{self}, #{target}."

end

p message.build_greeting("world")
p message2 = "Hello"
#p message2.build_greeting("world")  #エラーが出ます。メソッドはありません「undefined method....」と


puts "##########クラスメソッドとメタクラス########"

class Duration1
    def initialize(since,till)
        puts "kita"
        puts since
        puts till
        @since = since
        @till = till
    end
    #attr_accessor :since, :till
    class << self  #特異クラスを定義Duration1
        p self #デバック
        def week_from(from)#クラスメソッド
            puts "kita2"
            return self.new(from, from+7*24*60*60)#一週間の秒数
        end
    end
end

#Duration1.new(Time.now,Time.now)
p  Duration1.week_from(Time.now)




Ruby言語 クラス定義

puts "##########クラスメソッドの定義########1"      

class Duration
    def display; puts self end    #インスタンスメソッド
end

duration = Duration.new    #インスタンスの生成
duration.display      #=>#<Duration:0x2855850>

puts "##########クラスメソッドの定義########2"

class Duration1
    def Duration1.print(x); p x end    #クラスメソッド
end

Duration1.print 2     #=>2


puts "##########クラスメソッドの定義########3"

class Duration3
    p self
   
    def self.print(x); #selfはクラス自身を指す
    p x
    p self
    end    #クラスメソッド
    def self.test(x ); p x += x end    #クラスメソッド
end

Duration3.print 2   #=>1
Duration3.test 2   #=>1
Duration3.test 2   #=>1

puts "リファクタリングとはプログラムの機能を変更しない修正の事"


puts "##########インスタンス化########4"

class Duration4
    def initialize(since,till)  #javaで言うコンストラクター的なもの
        @since = since
        @until = till
    end
    attr_accessor :since, :until    #指定したインスタンス変数に、seter/getterメソッドを組み込む
end

duration4 = Duration4.new(Time.now, Time.now + 3600)
p duration4.until
p duration4.since
p duration4.since = Time.now + 4600

puts "##########インスタンス化########5"

class Duration5
    def initialize (since,till)
        @since = since
       @till = till
 end

 def since=(value); @since = value end


 def till=(value);@till = value end

 def since
     return @since
 end

 def till
     return @till
 end

end


duration5 = Duration5.new(Time.now,Time.now + 3600)
p duration5.since
p duration5.till
p duration5.since = Time.now + 3600
p duration5.since


puts "##########クラス定義の追加########6"

class String
    def caesar; tr 'a-zA-Z', 'n-za-mN-ZA-M' end
end

puts "Learning Ruby".caesar

puts "##########クラス定義の追加########7"


class Fixnum
    alias original_addition + #元の定義を別のメソッドで退避、(別名で定義)
    def +(rhs) #再定義
        original_addition(rhs).succ #succは次の整数を返す
    end
end

p 1+ 1 #=>3
p 5 + 2  #=>8


puts "##########クラス定義の追加########8"

class Duration8
    def initialize (since,till)
        @since = since
       @till = till
 end
attr_accessor :since, :till  #アクセサメソッド
    def display(target=$>)
        super #Objyectクラスのdisplayをよびだす オーバーライドする
        target.write "(#{self.since}-#{self.till})" #displayの引数に渡す
    end
end


duration8 = Duration8.new(Time.now,Time.now + 3600)
duration8.display

puts "#########インスタンス変数########"

class Duration9
    def initialize (since,till)
        @since = since
       @till = till
 end
    def print_since;p @since end
end

duration9 = Duration9.new(Time.now-7,Time.now)
duration10 = Duration9.new(Time.now+7,Time.now + 14)
duration9.print_since
duration10.print_since


puts "#########クラス変数########"

class Foo
    @@class_variable = "foo"
    def ttt
        p @@class_variable
    end
end

class Bar < Foo
    p @@class_variable
    @@class_variable = "bar"
    def print
        p @@class_variable
    end
end

#foo = Foo.new
#foo.ttt
#bar = Bar.new
#bar.print

2011年5月6日金曜日

Ruby メソッドとは

puts "##########メソッド########1"     


str = "abcd<efgh>ijkl mnop"

esca_str = str.gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/, '&gt;').gsub(/ /, '&ensp;')

puts esca_str

puts "##########レシーバの省略########1"

class Laputa
    def hover
        puts "vibrateを呼び出します。"
        vibrate
        puts "vibrateをselfで呼び出します。"
        p self
        self.vibrate
      
        puts "名前の衝突開始"
        vibrate = 1  #名前の衝突
        p vibrate
      
        puts "selfを使い自分自身のメソッドの呼び出し"
        self.vibrate #selfを使い自分自身のメソッドの呼び出し
    end
  
    def vibrate
        p self
        puts "vibrateが呼ばれた"
        #何かの処理
    end
end

def vibrate
    puts "Laputa外の関数"
end

takalamakhan = Laputa.new #コンストラクタの呼び出し。インスタンスの生成。
taka1 = takalamakhan.hover

takalamakhan2 = Laputa.new
takalamakhan2.hover
p takalamakhan.equal? takalamakhan2   #オブジェクトが同じものか?
vibrate   #Laputa外の関数を呼び出し

puts "########## 関数的 メソッド ########1"

def functional_method(a, b)
    return [a, b, a+b]
end
p functional_method(1, 2) #=>[1, 2, 3]



puts "########## 引数展開 ########1"

def some_method(a, b)  #仮引数
    p [a, b]
end

params = [1, 2]
x, y = params    #多重代入の場合は暗黙変換される
some_method(x, y)     #=>[1, 2]
#some_method(params) #配列から実引数リストへの自動的な展開は行われない。

puts "########## メソッドの定義 ########1"
puts "メソッド定義がdefを使う"

def sum(x, y)
    puts x + y
end

def diff x, y #括弧を省略スタイル
    puts x - y
end

def prod(x, y) puts x * y end #1行にマトメルスタイル
def quo x, y ; puts x / y end #括弧省略の1行マトメスタイル セミコロンは引数渡しの終わり

sum(1, 2)     #=>3
diff(1, 2)    #=>-1
prod(1, 2)    #=>2
quo(1, 2)     #=>0


puts "########## メソッドの定義 return ########"

def fact(n)
    return 1 if n == 0
    product = 1
    (1 .. n).each do |i|
        product *= i
        puts product
    end
    return product  #returnは必須ではありません。returnがない場合はメソッド末尾に到達すると最後の式が戻り値になります。
end

puts fact(10)   #=>3628800


puts "一行で記述した場合 メソッドの最後の式が戻り"
def fact2(n); (1 .. n).inject(1){|fe,i|fe*i} end  #fe,iはfeにiを代入している
puts fact2(10)   #=>3628800


puts "########## メソッドの定義 多値の返却 ########"

def some_method(a)
    return a, 1, 2 ,3
end
 a , b , *c = *some_method(0)
p a
p b
p c


puts "########## メソッドの定義 引数展開 ########"

def some_method(a , b , c)
    return a , b  , *c
end


array = [3 , 4 , 5]
te = some_method(1 , 2 , array)
p te


puts "########## メソッドの定義 デフォルト値 ########"

def some_method(a, b, c = 3)
    p a, b, c
end
some_method(1,2,4)  #デフォルト値を変更
some_method(1,2)    #デフォルト値がある仮引数は省略可能

puts "########## メソッドの定義 デフォルト値の評価コンテキスト ########"

def print_time(time = Time.now)
    return time
end

(0 .. 3).each do |i|
    p print_time
    sleep 0.1
end

puts "########## メソッドの定義 可変長引数 ########"


def some_method2(a, b, *c) #残りは配列で渡し
     return a, b, *c #cは展開渡し
end
some = some_method2(1,2,3,4,5,6,7,8,9,0)
p some  #=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]


puts "########## メソッドの定義 ブロックつきメソッド ########"


verse = "点士気喪 字時字時字時\nTESR TETTTTT\n"

count = 0 #

verse.each_line do |line|
    print line
  
    count += 1 #=>点士気喪 字時字時字時 改行されてTESR TETTTT
    p count
end

p count


puts "########## メソッドの定義 クローじゃ ########"


def some_method
    3.times {p self}  #=>main
end

some_method

puts "########## メソッドの定義 クローじゃ ########"

def create_count
    count = 1
    return Proc.new do
        count += 1
        p count
    end
end

counter = create_count #メソッドの呼び出し
p counter.class        #Procクラスの表示
counter.call  #=>2
counter.call  #=>3


counter2 = create_count
p counter2.class  
counter2.call  #=>2
counter2.call  #=>3


puts "########## メソッドの定義 クローじゃ ########"
count = 0
[[1,2],[3,4]].each do |num , nums|
    p [num , nums]
    p count += 1
end

#p [num , nums]     #ブロック変数へはブロックローカル変数外部よりできません


puts "########## メソッドの定義 ブロックつきメソッドの定義 ########"

def foo_bar_baz
 yield "foao"
 yield "barsss"
 yield "bazsss"
end

g = 0
foo_bar_baz do |item|
    p g+=1
    puts item
end


puts "########## メソッドの定義 イテレータの中のyield ########"


def foos_bars_bars2
    %w[foos bars1 bars2].each do |items|
        yield items
    end
end
g = 0
foos_bars_bars2 do |items|
    p g+=1
    puts items
end
puts "########## メソッドの定義 Enumeratorとyield ########"


def foos_bars_bars3
    p block_given? #trueかfalseを返す
    return enum_for(:foo_bar_baz3) unless block_given?  #メソッドにブロックが渡されたかで動きを変える
    %w[foo bar baz].each do |item|
        yield item
    end
end


p foos_bars_bars3   #ブロック無し

foos_bars_bars3 do |item|            #ブロックあり
     p item
end

puts "########## メソッドの定義 Enumeratorとyield ########"
def my_map
    [yield(1),yield(2),yield(3)]
end
p my_map {|i| i+1}

my_map do |i|
     p i+1
end