# 安定板を入れるならrvm get stable rvm get latest # 呼ばなくても大丈夫そうですが rvm reload # Ruby 2.0.0のインストール rvm install 2.0.0 # 2.0.0に切り替えて、バージョン確認 rvm 2.0.0 ruby -v => ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]
class MyClass def hello puts "Hello World!" end end MyClass.new.instance_eval do hello end
require 'RMagick' require 'tempfile' # アップロードされたファイル uploaded_file = params[:file] # TempfileからImageを作成する # from_blobはArrayを返すので、shiftして先頭の要素を取得 image = Magick::Image.from_blob(File.read(uploaded_file.path)).shift # 画像フォーマットの確認 puts image.format # なんかいろんな処理 # Tempfileの作成 tempfile = Tempfile.open(temp_filename) # Tempfileへの書き込み # フォーマットの指定が便利 image.write("png:" + tempfile.path) # なんかいろんな処理 # Tempfileの削除 tempfile.close!
module FinalClass class << self def included(base) base.extend ClassMethods end end module ClassMethods def method_added(name) remove_method name raise TypeError, "You cannot define a new method!" end def inherited(subklass) raise TypeError, "You cannot define a subclass!" end end end class A include FinalClass end
gem install amazon-ecs
require 'amazon/ecs' Amazon::Ecs.options = { :associate_tag => '[your associate tag]', :AWS_access_key_id => '[your developer token]', :AWS_secret_key => '[your secret access key]' } # API呼び出し res = Amazon::Ecs.item_lookup("4088767624", :response_group => 'Small, ItemAttributes, Images', :country => 'jp') # 返ってきたXMLを表示(res.doc.to_sでも多分OK) puts res.marshal_dump
amazon_tag = '[your associate tag]' res.items.each do |item| element = item.get_element('ItemAttributes') data = { :asin => item.get('ASIN'), :title => element.get("Title"), :page_url => "http://www.amazon.co.jp/dp/#{item.get('ASIN')}?tag=#{amazon_tag}", :isbn => element.get("ISBN"), :author => element.get_array("Author").join(", "), :product_group => element.get("ProductGroup"), :manufacturer => element.get("Manufacturer"), :publication_date => element.get("PublicationDate"), # URL, Width, Heightの要素を持っている :small_image => item.get_hash("SmallImage"), :medium_image => item.get_hash("MediumImage"), :large_image => item.get_hash("LargeImage") } end # dataを表示する(省略)
bundle gem hello-akasata
# -*- encoding: utf-8 -*- require File.expand_path('../lib/hello-akasata/version', __FILE__) Gem::Specification.new do |gem| gem.authors = ["akasata"] gem.email = ["*@*.*"] gem.description = %q{Test Gem.} gem.summary = %q{Hello World!} gem.homepage = "" gem.files = `git ls-files`.split($\) gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.name = "hello-akasata" gem.require_paths = ["lib"] gem.version = Hello::Akasata::VERSION # 依存するgemがある場合 # gem.add_dependency('log4r', '>= 1.0.5') gem.add_development_dependency('rspec') end
module Hello module Akasata VERSION = "0.0.1" end end
require "hello-akasata/version" module Hello module Akasata def self.hello "Hello akasata World!" end end end
rake build # ビルドします rake install # ビルドしてシステムにインストールします rake release # これでRubyGems.orgに公開されます。今回はやってませんw
require 'rubygems' require 'bundler/setup' require 'hello-akasata' RSpec.configure do |config| config.mock_framework = :rspec end
require 'spec_helper' describe Hello::Akasata, "::hello" do it "should return 'Hello akasata World!'" do Hello::Akasata.hello.should eq("Hello akasata World!") end end
rspec
gem install aws-sdk
# encoding: utf-8 require 'rubygems' require 'aws-sdk' require 'nkf' ses = AWS::SimpleEmailService.new( :access_key_id => 'YOUR_ACCESS_KEY_ID', :secret_access_key => 'YOUR_SECRET_ACCESS_KEY') body_text = NKF.nkf '-jw', <<-EOS こんにちは。 このメールはテストメールです。 おしまい EOS subject = NKF.nkf('-Mw', 'サブジェクトです') from_email = "FROM_EMAIL_ADDRESS" to_email = "TO_EMAIL_ADDRESS" p "Verified?(FROM Email): " + ses.identities[from_email].verified?.to_s p "Verified?(TO Email): " + ses.identities[to_email].verified?.to_s p "Verified?(Domain): " + ses.identities['YOUR_DOMAIN'].verified?.to_s ses.send_email( :subject => subject, :to => to_email, :from => from_email, :body_text => body_text, :body_text_charset => 'ISO-2022-JP' )
gem install aws-sdk
require 'aws-sdk' AWS.config( :access_key_id => 'YOUR_ACCESS_KEY_ID', :secret_access_key => 'YOUR_SECRET_ACCESS_KEY') s3 = AWS::S3.new # 以下も可 # s3 = AWS::S3.new( # :access_key_id => 'YOUR_ACCESS_KEY_ID', # :secret_access_key => 'YOUR_SECRET_ACCESS_KEY')
bucket = s3.buckets['yourbucketname'] filename = "fullpath/filename.ext" basename = File.basename(filename) o = bucket.objects[basename] o.write(:file => filename)
# キーの生成 my_key = OpenSSL::Cipher.new("AES-256-ECB").random_key # オブジェクトの取得 o = bucket.objects[name] # 暗号化して書込 o.write("MY TEXT", :encryption_key => my_key) # 復号化して読込 puts o.read(:encryption_key => my_key)
# キーの生成 my_key = OpenSSL::PKey::RSA.new(1024) # オブジェクトの取得 o = bucket.objects[name] # 暗号化して書込 o.write("MY TEXT", :encryption_key => my_key) # 復号化して読込 puts o.read(:encryption_key => my_key)
o = bucket.objects[name] # Canned ACL o.acl = :public_read # writeメソッドに渡すことも可能 o.write(:file => filename, :acl => :public_read)
o.acl.change do |acl| acl.grant(:full_control).to(:canonical_user_id => "...") end
without dci 3024223.4 (±1.7%) i/s - 15139474 in 5.007655s with dci 768491.0 (±6.6%) i/s - 3833440 in 5.011732s with dci(send) 1586461.1 (±2.1%) i/s - 7977320 in 5.030680s with dci(forwardable) 1712079.6 (±1.8%) i/s - 8580546 in 5.013597s with dci(delegate) 1134862.9 (±1.3%) i/s - 5701904 in 5.025205s
require 'rubygems' require 'benchmark/ips' require 'forwardable' require 'delegate' class ExampleClass def foo; 42; end end module ExampleMixin def foo; 43; end end class ExampleClassSend def initialize(role) @role = role end def method_missing(action, *args) @role.__send__(action, *args) end end class ExampleClassForwardable extend Forwardable def initialize(role) @role = role end def_delegator :@role, :foo, :foo end class ExampleClassDelegate < SimpleDelegator end class ExampleRole def foo 43; end end @role = ExampleRole.new Benchmark.ips do |bm| bm.report("without dci") { ExampleClass.new.foo } bm.report("with dci") do obj = ExampleClass.new obj.extend(ExampleMixin) obj.foo end bm.report("with dci(send)") do ExampleClassSend.new(@role).foo end bm.report("with dci(forwardable)") do ExampleClassForwardable.new(@role).foo end bm.report("with dci(delegate)") do ExampleClassDelegate.new(@role).foo end end