SlideShare uma empresa Scribd logo
1 de 51
Baixar para ler offline
require 'win32ole'

app = WIN32OLE.new('Excel.Application')
book = app.Workbooks.Open(app.GetOpenFilename)

for row in book.ActiveSheet.UsedRange.Rows do
 for cell in row.Columns do
   p cell.Name
   p cell.Age
 end
end

book.close(false)
app.quit

    via http://jp.rubyist.net/magazine/?0027-ExcellentRuby
require "yaml"

people = []
for row in book.ActiveSheet.UsedRange.Rows
do
 for cell in row.Columns do
   people << {:name => cell.Name, :age =>
cell.Age}
 end
end

File.open 'people.yml', 'w' do |file|
 YAML.dump(people, file)
end
require 'yaml'

people = YAML.load_file 'people.yml'
people.each do |person|
 p = Person.new person
 p.save
end

       import.rb


ruby script/runner import.rb
u='           '
u.encoding # => #<Encoding:UTF-8>




s = u.encode(‘Shift_JIS’)
s.encoding # => #<Encoding:Shift_JIS>
# Ruby 1.9
"            "[0..2] # => "     "

# Ruby 1.8
"            "[0..2] # => "343201214"
$ cat hello.rb
p"                    "

$ ruby1.9 hello.rb
hello.rb:1: invalid multibyte char
(US-ASCII)
$ cat hello.rb
# vim:fileencoding=utf-8
p"                  "

$ ruby1.9 hello.rb
"                  "
vim
# vim:fileencoding=utf-8

emacs
# -*- coding: utf-8 -*-
[1, 2, 3].combination(2).to_a
# => [[1, 2], [1, 3], [2, 3]]


[1, 2, 3].permutation(2).to_a
# => [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1],
[3, 2]]
[1, 2].product([3, 4])
# => [[1, 3], [1, 4], [2, 3], [2, 4]]

flatten


[1, [2, [3]]].flatten(1) # => [1, 2, [3]]
# Ruby 1.8
# => [1, 2, 3]
[0, 1, 2, 3].shuffle # => [1, 3, 0, 2]
[0, 1, 2, 3].shuffle # => [2, 0, 3, 1]



[0, 1, 2, 3].sample    # => 2
[0, 1, 2, 3].sample(2) # => [3, 2]
[0, 1, 2, 3].take(2) # => [0, 1]


[0, 1, 2, 3].drop(1) # => [1, 2, 3]
# Ruby 1.8
[1,2,3,4,5].inject(0) {|result, i| result + i} # => 15

# 1.9
[1,2,3,4,5].inject(:+) # => 15
#            reduce          alias
[1,2,3,4,5].reduce(:+) # => 15
2                                                                 w

       data = (1..100).map{rand(5)+1}.map{|id| {:id => id, :value => rand(10000)}}
(1..5).map{|i| {:id => i, :sum => data.select{|h| h[:id] == i}.map{|h| h[:value]}.reduce(:+)}}

Mais conteúdo relacionado

Mais procurados

Mais procurados (7)

Pro bun-fighting - Working with JavaScript projects
Pro bun-fighting - Working with JavaScript projectsPro bun-fighting - Working with JavaScript projects
Pro bun-fighting - Working with JavaScript projects
 
Functional testing with capybara
Functional testing with capybaraFunctional testing with capybara
Functional testing with capybara
 
Embracing Capybara
Embracing CapybaraEmbracing Capybara
Embracing Capybara
 
[2019] 아직도 돈 주고 DB 쓰나요? for Developer
[2019] 아직도 돈 주고 DB 쓰나요? for Developer[2019] 아직도 돈 주고 DB 쓰나요? for Developer
[2019] 아직도 돈 주고 DB 쓰나요? for Developer
 
Creating Beautiful, Accessible, and User-Friendly Forms
Creating Beautiful, Accessible, and User-Friendly FormsCreating Beautiful, Accessible, and User-Friendly Forms
Creating Beautiful, Accessible, and User-Friendly Forms
 
The Secret Life of Forms
The Secret Life of FormsThe Secret Life of Forms
The Secret Life of Forms
 
Extending Operators in Perl with Operator::Util
Extending Operators in Perl with Operator::UtilExtending Operators in Perl with Operator::Util
Extending Operators in Perl with Operator::Util
 

Destaque (7)

OSCNagoya 2011「App inventor入門」
OSCNagoya 2011「App inventor入門」OSCNagoya 2011「App inventor入門」
OSCNagoya 2011「App inventor入門」
 
2080 1 yoga-rev1_2002_portugues
2080 1 yoga-rev1_2002_portugues2080 1 yoga-rev1_2002_portugues
2080 1 yoga-rev1_2002_portugues
 
Airbus a380 details 9939393
Airbus a380 details 9939393Airbus a380 details 9939393
Airbus a380 details 9939393
 
Catalogo Daisy´s Miniatures
Catalogo Daisy´s MiniaturesCatalogo Daisy´s Miniatures
Catalogo Daisy´s Miniatures
 
Dtv, survey paper
Dtv, survey paperDtv, survey paper
Dtv, survey paper
 
MULTISCALE MODELLING OF URBAN CLIMATE
MULTISCALE MODELLING OF URBAN CLIMATE MULTISCALE MODELLING OF URBAN CLIMATE
MULTISCALE MODELLING OF URBAN CLIMATE
 
Cwe2010 paper 172
Cwe2010 paper 172Cwe2010 paper 172
Cwe2010 paper 172
 

Semelhante a 名古屋Ruby会議01 - Rubyでライフハッキング10連発♪

Useful javascript
Useful javascriptUseful javascript
Useful javascript
Lei Kang
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutos
Edgar Suarez
 
Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In Ruby
Ross Lawley
 
Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02
Apoorvi Kapoor
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015
qmmr
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)
Kerry Buckley
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
Siva Arunachalam
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 

Semelhante a 名古屋Ruby会議01 - Rubyでライフハッキング10連発♪ (20)

Useful javascript
Useful javascriptUseful javascript
Useful javascript
 
01_introduction_lab.pdf
01_introduction_lab.pdf01_introduction_lab.pdf
01_introduction_lab.pdf
 
Elixir pattern matching and recursion
Elixir pattern matching and recursionElixir pattern matching and recursion
Elixir pattern matching and recursion
 
Into Clojure
Into ClojureInto Clojure
Into Clojure
 
Ruby 1.9
Ruby 1.9Ruby 1.9
Ruby 1.9
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutos
 
Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In Ruby
 
jRuby: The best of both worlds
jRuby: The best of both worldsjRuby: The best of both worlds
jRuby: The best of both worlds
 
Slaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in RubySlaying the Dragon: Implementing a Programming Language in Ruby
Slaying the Dragon: Implementing a Programming Language in Ruby
 
Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015
 
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to Elixir
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)
 
Elixir
ElixirElixir
Elixir
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
 
7li7w devcon5
7li7w devcon57li7w devcon5
7li7w devcon5
 
Ken20150417
Ken20150417Ken20150417
Ken20150417
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
 
Ruby2.0でlazyな n-Queen
Ruby2.0でlazyな n-QueenRuby2.0でlazyな n-Queen
Ruby2.0でlazyな n-Queen
 

Mais de Shoya Tsukada

ABC 2011 Summer App Inventor入門ワークショップ
ABC 2011 Summer App Inventor入門ワークショップABC 2011 Summer App Inventor入門ワークショップ
ABC 2011 Summer App Inventor入門ワークショップ
Shoya Tsukada
 
ARお絵描きツール「Feel Sketch」 at Gadget1
ARお絵描きツール「Feel Sketch」 at Gadget1ARお絵描きツール「Feel Sketch」 at Gadget1
ARお絵描きツール「Feel Sketch」 at Gadget1
Shoya Tsukada
 

Mais de Shoya Tsukada (7)

Google I/O 2013に行ったついでにラスベガス戦果報告
Google I/O 2013に行ったついでにラスベガス戦果報告Google I/O 2013に行ったついでにラスベガス戦果報告
Google I/O 2013に行ったついでにラスベガス戦果報告
 
高専カンファレンス in 岐阜 基調講演2 @gabu
高専カンファレンス in 岐阜 基調講演2 @gabu高専カンファレンス in 岐阜 基調講演2 @gabu
高専カンファレンス in 岐阜 基調講演2 @gabu
 
NGK2011B 昼の部 スマートフォンアプリ 開発とコミュニティと 勉強会と
NGK2011B 昼の部 スマートフォンアプリ 開発とコミュニティと 勉強会とNGK2011B 昼の部 スマートフォンアプリ 開発とコミュニティと 勉強会と
NGK2011B 昼の部 スマートフォンアプリ 開発とコミュニティと 勉強会と
 
「Camelog」Android開発秘話
「Camelog」Android開発秘話「Camelog」Android開発秘話
「Camelog」Android開発秘話
 
ABC 2011 Summer App Inventor入門ワークショップ
ABC 2011 Summer App Inventor入門ワークショップABC 2011 Summer App Inventor入門ワークショップ
ABC 2011 Summer App Inventor入門ワークショップ
 
ABC2011S コミュニティトラック デ部+つ部(江川崇&がぶ) つ部プレゼンツ「いま名古屋が暑い!」
ABC2011S コミュニティトラック デ部+つ部(江川崇&がぶ) つ部プレゼンツ「いま名古屋が暑い!」ABC2011S コミュニティトラック デ部+つ部(江川崇&がぶ) つ部プレゼンツ「いま名古屋が暑い!」
ABC2011S コミュニティトラック デ部+つ部(江川崇&がぶ) つ部プレゼンツ「いま名古屋が暑い!」
 
ARお絵描きツール「Feel Sketch」 at Gadget1
ARお絵描きツール「Feel Sketch」 at Gadget1ARお絵描きツール「Feel Sketch」 at Gadget1
ARお絵描きツール「Feel Sketch」 at Gadget1
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 

名古屋Ruby会議01 - Rubyでライフハッキング10連発♪

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7. require 'win32ole' app = WIN32OLE.new('Excel.Application') book = app.Workbooks.Open(app.GetOpenFilename) for row in book.ActiveSheet.UsedRange.Rows do for cell in row.Columns do p cell.Name p cell.Age end end book.close(false) app.quit via http://jp.rubyist.net/magazine/?0027-ExcellentRuby
  • 8. require "yaml" people = [] for row in book.ActiveSheet.UsedRange.Rows do for cell in row.Columns do people << {:name => cell.Name, :age => cell.Age} end end File.open 'people.yml', 'w' do |file| YAML.dump(people, file) end
  • 9. require 'yaml' people = YAML.load_file 'people.yml' people.each do |person| p = Person.new person p.save end import.rb ruby script/runner import.rb
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23. u=' ' u.encoding # => #<Encoding:UTF-8> s = u.encode(‘Shift_JIS’) s.encoding # => #<Encoding:Shift_JIS>
  • 24. # Ruby 1.9 " "[0..2] # => " " # Ruby 1.8 " "[0..2] # => "343201214"
  • 25. $ cat hello.rb p" " $ ruby1.9 hello.rb hello.rb:1: invalid multibyte char (US-ASCII)
  • 26. $ cat hello.rb # vim:fileencoding=utf-8 p" " $ ruby1.9 hello.rb " "
  • 28.
  • 29.
  • 30.
  • 31. [1, 2, 3].combination(2).to_a # => [[1, 2], [1, 3], [2, 3]] [1, 2, 3].permutation(2).to_a # => [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
  • 32. [1, 2].product([3, 4]) # => [[1, 3], [1, 4], [2, 3], [2, 4]] flatten [1, [2, [3]]].flatten(1) # => [1, 2, [3]] # Ruby 1.8 # => [1, 2, 3]
  • 33. [0, 1, 2, 3].shuffle # => [1, 3, 0, 2] [0, 1, 2, 3].shuffle # => [2, 0, 3, 1] [0, 1, 2, 3].sample # => 2 [0, 1, 2, 3].sample(2) # => [3, 2]
  • 34. [0, 1, 2, 3].take(2) # => [0, 1] [0, 1, 2, 3].drop(1) # => [1, 2, 3]
  • 35. # Ruby 1.8 [1,2,3,4,5].inject(0) {|result, i| result + i} # => 15 # 1.9 [1,2,3,4,5].inject(:+) # => 15 # reduce alias [1,2,3,4,5].reduce(:+) # => 15
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51. 2 w data = (1..100).map{rand(5)+1}.map{|id| {:id => id, :value => rand(10000)}} (1..5).map{|i| {:id => i, :sum => data.select{|h| h[:id] == i}.map{|h| h[:value]}.reduce(:+)}}