4 Replies - 1388 Views - Last Post: 05 February 2014 - 08:56 PM Rate Topic: -----

#1 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

[Gem] Izzy

Post icon  Posted 24 January 2014 - 09:11 PM

Gem: https://rubygems.org/gems/izzy
Github: https://github.com/baweaver/izzy

You know, I get really tired of writing things like this:
puts 'You can buy an R movie!' if person.is_18? || person.is_with_legal_guardian?
puts 'Programmer stereotypes!' if person.is_18? && person.is_male? && person.is_geek?



So I made it to where I can write this instead:
puts 'You can buy an R movie!' if person.any_of? :older_than_18, :with_legal_guardian
puts 'Programmer stereotypes!' if person.all_of? :older_than_18, :male, :geek



Reads cleaner to me, and enough of those long things and it gets impossible to tell what's going on, so I made this.

Is This A Good Question/Topic? 1
  • +

Replies To: [Gem] Izzy

#2 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: [Gem] Izzy

Posted 31 January 2014 - 05:46 PM

Izzy just learned a few new tricks! Maybe booleans aren't your cup of tea. How about we get some more power on that for you?

brandon.matches_all?  name: /^br/, age: (20..30) # => true

brandon.matches_any?  name: /br$/, age: (20..30) # => true

brandon.matches_none? name: /br&/, age: (30..40) # => true



Introducing matchers!
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10365
  • View blog
  • Posts: 38,396
  • Joined: 27-December 08

Re: [Gem] Izzy

Posted 01 February 2014 - 09:30 AM

Congrats to Lemur on having Izzy featured on Ruby Weekly!

http://rubyweekly.com/issues/180
Was This Post Helpful? 0
  • +
  • -

#4 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: [Gem] Izzy

Posted 02 February 2014 - 01:37 AM

Thank ya kindly! Now I've gone and taught it a few new tricks again for v3.0.0:

longer_than_5 = -> n { n.length > 5 }
greater_than_20 = -> a { a > 20 }

brandon.matches_all?(
  name: [/br/, /an/, longer_than_5],
  age: [(20..30), greater_than_20]
)

# => true



Multi matchers! With this we can effectively say goodbye to excessive boilerplating integrity checks on objects.

Let's take a look at how you'd normally try and do that:
brandon.name =~ /br/ && brandon.name =~ /an/ && brandon.name.length > 5 && (20..30) === brandon.age && brandon.age > 20



With this you can be far more terse about your checks. Granted this is a slightly confounded example, but you can get away with things like this in Ruby.

Of course you can put the lambdas inline, but I prefer it this way as it reads cleaner.
Was This Post Helpful? 0
  • +
  • -

#5 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: [Gem] Izzy

Posted 05 February 2014 - 08:56 PM

v3.1 is out, and we have some new tricks to play with:

class Array; include IzzyArray end

[0,0,0].all_are :zero?       # => true
[0,nil,'foo'].any_are :zero? # => true
[0,0,0].none_are :nil?       # => true



Combine that with methods in rails like #present? and #empty? and you can see how this would get real handy.

Compare to:
0.zero? && 0.zero? && 0.zero?       # all_are
0.zero? || nil.zero? || 'foo'.zero? # any_are, but crashes as #zero? isn't a method of non numbers
!(0.nil? && 0.nil? && 0.nil?)       # none_are



You even get the added benefit of a #respond_to? guard in case the method doesn't know of that method! Huzzah!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1