Created
October 11, 2012 13:41
-
-
Save orend/3872362 to your computer and use it in GitHub Desktop.
tricks from jeg2 talk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| p (1..10).inject(:*) # instead of: inject(&:*) | |
| # case on ranges | |
| age = rand(1..100) | |
| p age | |
| case age | |
| when -Float::INFINITY..20 | |
| puts "You're too young." | |
| when 21..64 | |
| # case on date ranges | |
|   require "date" | |
| start_of_aloha_ruby_conf = Date.new(2012, 10, 8) | |
| end_of_aloha_ruby_conf = Date.new(2012, 10, 9) | |
| case Date.today | |
| when Date.new...start_of_aloha_ruby_conf | |
| puts "Anticipation is building." | |
| when start_of_aloha_ruby_conf..end_of_aloha_ruby_conf | |
| puts "Mind being blown." | |
| when (end_of_aloha_ruby_conf + 1)..Date::Infinity | |
| puts "You've learned some Ruby while in paradise." | |
| end | |
| # case on lambdas | |
| require "prime" | |
| n = rand(1..10) | |
| pn | |
| case n | |
| when lambda(&:prime?) | |
| puts "This number is prime." | |
| when l | |
| # variables from regex | |
| if /\A(?<last>\w+),\s*(?<first>\w+)\z/ =~ "Gray, James" | |
| puts "#{first} #{last}" | |
| end | |
| # cycle through an array | |
| ring = %w[one two three].cycle | |
| puts ring.take(4) | |
| # one | |
| # two | |
| # three | |
| # one | |
| # indexing into a string with regex | |
| str = "Price $24.95" | |
| p str[/\$\d+(?:\.\d+)?/] | |
| p str[/\$(\d+)(?:\.\d+)?/, 1] | |
| p str[/\$(?<dollars>\d+)(?:\.\d+)?/, :dollars] | |
| # find last match | |
| expression = "40 + 2 = 42" | |
| p expression[expression.rindex(/\d+/)..-1] | |
| p expression[expression.rindex(/\b\d+/)..-1] | |
| # iterating in lockstep | |
| letters = "a".."d" | |
| numbers = 1..3 | |
| letters.zip(numbers) do |letter, number| | |
| p(letter: letter, number: number) | |
| end | |
| #{:letter=>"a", :number=>1} | |
| #{:letter=>"b", :number=>2} | |
| #{:letter=>"c", :number=>3} | |
| #{:letter=>"d", :number=>nil} | |
| # partition | |
| Person = Struct.new(:name, :gender) | |
| people = [ Person.new("James", :male), | |
| Person.new("Dana", :female), | |
| Person.new("Summer", :female) ] | |
| males, females = people.partition { |person| person.gender == :male } | |
| puts "Males:", males.map { |male| " #{male.name}" } | |
| puts "Females:", females.map { |female| " #{female.name}" } | |
| # chunk your data | |
|  headings = [ "1.1 Compiler Tricks", | |
| "1.2 Syntax", | |
| "1.3 Data Structures", | |
| "1.4 Iterators", | |
| "2.1 Core Ruby", | |
| "2.2 The Standard Library", | |
| "2.3 Tools", | |
| "2.4 Black Magic" ] headings.chunk { |heading| heading[/\A\d+/] } .each do |chapter, headings| | |
| puts "Chapter #{chapter}:" | |
| puts headings.map { |heading| " #{heading}" } | |
| end | |
| # add with_index to any iterator | |
| votes = { "Josh" => %w[SBPP POODR GOOS], | |
| "Avdi" => %w[POODR SBPP GOOS], | |
| "James" => %w[POODR GOOS SBPP], | |
| "David" => %w[GOOS SBPP POODR], | |
| "Chuck" => %w[GOOS POODR SBPP] } | |
| tally = Hash.new(0) | |
| votes.values.each do |personal_selections| | |
| personal_selections.each_with_object(tally).with_index do |(vote, totals), i| | |
| totals[vote] += personal_selections.size - i | |
| end end | |
| p tally | |
| # struct without assignment | |
| # instead of: class Name < Struct.new(:first, :last) ... end | |
| Struct.new("Name", :first, :last) do | |
| def full | |
| "#{first} #{last}" | |
| end | |
| end | |
| james = Struct::Name.new("James", "Gray") | |
| puts james.full | |
| # between | |
| p 2.between?(1, 10) | |
| p "cat".between?("bat", "rat") | |
| # line by line reading | |
| File.foreach(__FILE__) do |line| | |
| p line | |
| end | |
| # easiest way to read from the internet | |
| require "open-uri" | |
| open("http://rubyrogues.com/feed/") do |feed| | |
| puts feed.read.scan(%r{<title>(\d+\s*RR\b[^<]*)</title>}) | |
| end | |
| # OO file interface | |
|  require "pathname" | |
| # build paths | |
| dir = Pathname.pwd # or Pathname.new(...) path = dir + __FILE__ # add paths | |
| # work with paths | |
| puts path.realpath | |
| puts path.relative_path_from(dir) | |
| puts | |
| # use paths to do work | |
| path.open do |io| | |
| 5.times do | |
| puts io.gets | |
| end | |
| end | |
| # the world's easiest database | |
| require 'pstore' | |
| db = PStore.new("accounts.pstore") | |
| db.transaction do | |
| db["james"] = 100.00 | |
| db["dana"] = 100.00 | |
| end | |
| db.transaction do | |
| db["dana"] += 200.00 | |
| db["james"] -= 200.00 | |
| db.abort if db["james"] < 0.0 | |
| end | |
| db.transaction(true) do | |
| puts "James: %p" % db["james"] | |
| puts "Dana: %p" % db["dana"] | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment