ȥå   Խ ʬ Хåå ź ʣ ̾ѹ   ñ측 ǽ   إ   ǽRSS

Ruby/ץ

Last-modified: 2010-03-08 () 00:34:22 (2813d)
Top / Ruby / ץ

Ruby

ץ

Hello, world!᥽å

hello.rb

def hello(name = "world")
  print "Hello, ", name, "!\n"
end
hello("Ruby")

DOSץץȤ¹Ԥˡ

>ruby hello.rb

餫ruby.exe˥ѥ̤Ƥޤ

Hello, world!饤֥

helloLib.rb

def hello(name = "world")
  print "Hello, ", name, "!\n"
end

hello.rb

require "helloLib"
hello("Ruby")
  • 饤֥̥եǺrequire ""ɤ߹ߤޤ
  • 饤֥⳰ѤǤ륰Хѿϡ$ѿ̾פȤޤ

Hello, world!饹

HelloWorld.rb

class HelloWorld
  def initialize(name = "world")
    @name = name;
  end
  def hello
    print "Hello, ", name, "!\n"
  end
end

hello.rb

require "HelloWorld"
ruby = HelloWorld.new("Ruby")
world = HelloWorld.new

ruby.hello
world.hello
  • ϲդVERSION = 1פʤɤơHelloWorld::VERSIONפȤѤޤ
  • 饹ѿϡ@@var = 0פʤɤơѤϡ᥽åɤƥޤ
  • 饹᥽åɤϡdef HelloWorld.hello(name)פʤɤơѤϡHelloWorld.hello("world")פȤѤޤ
  • 饹Υ󥹥ѿϡ@פǻϤޤޤ
  • Ԥȡ饹γ饤󥹥ѿ@name˥Ǥޤ
    attr_reader :nameȤΤ߲ǽ
    attr_write :nameѹΤ߲ǽ
    attr_accessor :nameȤѹǽ
    attr_accessorȡΥ᥽åɤȤˤʤޤ
    def name
      return @name
    end
    
    def name=(value)
      @name = value
    end

arLanguage = ["Ruby", "PHP", "C", "java"]
arLanguage.each { |value|
  puts value
end

grepޥ

grepr.rb

pattern = Regexp.new(ARGV[0])
filename = ARGV[1]
file = open(filename)
while line = file.gets
  if pattern =~ line
    print line
  end
end
file.close

¹ˡ

ruby grepr ѥ ե̾

case - whenʸ ʸ

arTag = ["a", "img", "pre"]
arTag.each { |tag|
  case tag
  when "p", "a", "i", "b", "blockquote"
    print tag, " has child.\n"
  when "img", "br"
    print tag, " has no child.\n"
  else
    print tag, " cannot be used.\n"
  end
}

case - whenʸ ɽ

while line = gets
  case line
  when /^From:/i
    puts "find sender!"
  when /^To:/i
    puts "find receiver!"
  when /^Subject:/i
    puts "find subject!"
  when /^$:/
    puts "finish!"
  else
    ## ɤФ
  end
end

case - whenʸ ֥ȼ

arObject = ["a", 1, nil]
arObject.each { |item|
  case item
  when String
    puts "item is String."
  when Numeric
    puts "item is Numeric."
  else
    puts "item is something."
  end
}
  • if item.instance_of?(String)פȤäӤԤޤ

Хʥե

fp = open("./hoge")
fp.binmode
while(buf = fp.read(4))
  #32bit4ХȡintDz
  iTmp =  buf.unpack("i*")
  print iTmp
end

Web֤줿եɤ߹ߥ󥽡˽Ϥ륵ץ

sample_open_uri.rb

# Web֤줿եɤ߹ߥ󥽡˽Ϥ륵ץ
require 'open-uri'
open('http://www.google.co.jp/') do |fpWeb|
	fpWeb.each do |line|
		puts line
	end
end