proc_to_lambda 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ac8537308f2b72d03b641f01eb23ea614deac38e
4
+ data.tar.gz: 4a35226dac5a19098a37d90b3fa1222f3aa13bd4
5
+ SHA512:
6
+ metadata.gz: 384049eb6093f34955be10590eec890b17089e4336d78de6121488efaa2bfde0ae527367ada93cd56adea2a7522c14d73ad4f5ad9d3eb2d5d8c564e97d4bafd3
7
+ data.tar.gz: 4983898ebfd9e63e899586336097fa86f9f781cc6b3a6d554043aa66933f4a2dd2a30ebbbe57313fbd38b4b6a8748969290dfa646432f796b5f7d66b17b61e31
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ *.gem
3
+
4
+ Gemfile.lock
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - ruby-head
6
+ - jruby-19mode
7
+ - rbx-19mode
8
+
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: ruby-head
12
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,88 @@
1
+ ## Proc To Lambda
2
+
3
+ [![Build Status](https://travis-ci.org/schneems/proc_to_lambda.png)](https://travis-ci.org/schneems/proc_to_lambda)
4
+
5
+ Convert your procs to lambdas for all the Rubies.
6
+
7
+ ## Why?
8
+
9
+ I love anonomyous functions ( lambdas & procs <3 ). I also love "confident" style ruby functions that have early returns:
10
+
11
+ ```ruby
12
+ def foo(num)
13
+ return false if num.nil?
14
+ # ...
15
+ ```
16
+
17
+ Unfortunately procs do not play nice with early returns:
18
+
19
+ ```ruby
20
+ def bar(num, &block)
21
+ block.call(num)
22
+ end
23
+
24
+ bar(nil) do |num|
25
+ return false if num.nil?
26
+ # ...
27
+ end
28
+ LocalJumpError: unexpected return
29
+ from (irb):25:in `block in irb_binding'
30
+ from (irb):21:in `call'
31
+ from (irb):21:in `bar'
32
+ from (irb):24
33
+ from /Users/schneems/.rbenv/versions/2.0.0-p247/bin/irb:12:in `<main>'
34
+ ```
35
+
36
+ But lambdas do:
37
+
38
+ ```ruby
39
+ bar(nil, &lambda {|num|
40
+ return false if num.nil?
41
+ # ...
42
+ })
43
+ # => false
44
+ ```
45
+
46
+ What I want is the pretty syntax of using a proc, but with the flexibility of explicit returns of a lambda. Enter: ProcToLambda
47
+
48
+
49
+ ## Can't you do this in Ruby already?
50
+
51
+ Yes, but the implementation is not standard across all the rubies :(
52
+
53
+ ## Install
54
+
55
+ In your `Gemfile`:
56
+
57
+ ```rb
58
+ gem 'proc_to_lambda'
59
+ ```
60
+
61
+ ## Use
62
+
63
+ In your code, call `ProcToLambda.to_lambda()` and pass in a block:
64
+
65
+ ```ruby
66
+ def bar(num, &block)
67
+ block = ProcToLambda.to_lambda(block)
68
+ block.call(num)
69
+ end
70
+ ```
71
+
72
+ If you're risky you can extend the `Proc` class directly:
73
+
74
+ ```ruby
75
+ Proc.extend(ProcToLambda)
76
+ ```
77
+
78
+ Then you can call `Proc.to_lambda`:
79
+
80
+ ```ruby
81
+ def bar(num, &block)
82
+ block.to_lambda.call(num)
83
+ end
84
+ ```
85
+
86
+ ## License
87
+
88
+ MIT
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+
7
+ task :default => [:test]
8
+
9
+ test_task = Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = false
14
+ end
@@ -0,0 +1,15 @@
1
+ module ProcToLambda
2
+ def to_lambda
3
+ ProcToLambda.to_lambda(self)
4
+ end
5
+
6
+ def self.to_lambda(block)
7
+ if RUBY_ENGINE && RUBY_ENGINE == "jruby"
8
+ return lambda(&block)
9
+ else
10
+ obj = Object.new
11
+ obj.define_singleton_method(:_, &block)
12
+ return obj.method(:_).to_proc
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module ProcToLambda
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'proc_to_lambda/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "proc_to_lambda"
8
+ gem.version = ProcToLambda::VERSION
9
+ gem.authors = ["Richard Schneeman"]
10
+ gem.email = ["richard.schneeman+rubygems@gmail.com"]
11
+ gem.description = %q{ Turn your procs to lambdas in no time }
12
+ gem.summary = %q{ Turn your procs to lambdas in less time than you can read this sentence. }
13
+ gem.homepage = "https://github.com/schneems/proc_to_lambda"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency "rake"
22
+ end
23
+
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ def pass_to_block(pass, &block)
4
+ block.call(pass)
5
+ end
6
+
7
+ def pass_to_block_with_convert(pass, &block)
8
+ block = ProcToLambda.to_lambda(block)
9
+ pass_to_block(pass, &block)
10
+ end
11
+
12
+ def pass_to_block_with_include(pass, &block)
13
+ pass_to_block(pass, block.to_lambda)
14
+ end
15
+
16
+ class ProcToLambdaTest < Test::Unit::TestCase
17
+ def test_raises
18
+ assert_raise do
19
+ pass_to_block(nil) do |num|
20
+ return false if num.nil?
21
+ end
22
+ end
23
+ end
24
+
25
+ def test_module_method_works
26
+ pass_to_block_with_convert(nil) do |num|
27
+ return false if num.nil?
28
+ end
29
+ end
30
+
31
+ def test_include
32
+ Proc.extend ProcToLambda
33
+ pass_to_block(nil) do |num|
34
+ return false if num.nil?
35
+ end
36
+ assert Proc.new.to_lambda
37
+ Proc.send(:undef_method, :to_lambda)
38
+ end
39
+ end
@@ -0,0 +1,4 @@
1
+ Bundler.require
2
+
3
+ require 'test/unit'
4
+ require 'proc_to_lambda'
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proc_to_lambda
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Schneeman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: ' Turn your procs to lambdas in no time '
28
+ email:
29
+ - richard.schneeman+rubygems@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - .travis.yml
36
+ - Gemfile
37
+ - README.md
38
+ - Rakefile
39
+ - lib/proc_to_lambda.rb
40
+ - lib/proc_to_lambda/version.rb
41
+ - proc_to_lambda.gemspec
42
+ - test/proc_to_lambda_test.rb
43
+ - test/test_helper.rb
44
+ homepage: https://github.com/schneems/proc_to_lambda
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.0.3
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Turn your procs to lambdas in less time than you can read this sentence.
68
+ test_files:
69
+ - test/proc_to_lambda_test.rb
70
+ - test/test_helper.rb