Archive for category Ruby

Cross Product in Ruby with Array

I was digging around the Ruby standard library on Enumerable and Arrays to find if there was a method to get the cross product of two arrays in Ruby and found Array.product.

I was showing the code that used this to another teammate and they didn’t realize what the product method was doing, and upon telling them it returns the cross product of two arrays, he was pleasantly surprised to hear about it, so I realized I should share this for anyone else who may not know about it.

Below is an example usage of the product method, and more can be found on the ruby-doc site for Array.

[1] pry(main)> [1, 2, 3, 4, 5].product [:a, :b, :c]
=> [[1, :a],
 [1, :b],
 [1, :c],
 [2, :a],
 [2, :b],
 [2, :c],
 [3, :a],
 [3, :b],
 [3, :c],
 [4, :a],
 [4, :b],
 [4, :c],
 [5, :a],
 [5, :b],
 [5, :c]]
[2] pry(main)> 

Hope someone else can find this useful as well.

–Proctor

Advertisements

Leave a comment

Software Development Podcasts – 2013 Edition

I was recently chatting with some coworkers about podcasts I listen to, so I thought I should document that list for easy sharing and to find some gems I am missing.

I have taken advantage of my commute time and turned my commute into Automobile University as talked about by Zig Ziglar. I heard this idea via some fitness blogs I was reading where the trainers were talking about ways to continuously improve, and decided I would apply that idea to my commute, walks, or even running errands.

The other thing I have started taking advantage of is the ability of podcast players to play at double speed. Most podcasts out there do well at one-and-a-half or double speed, and have heard that some players even support three-times speed. This allows you to dramatically increase your consumption rate if you can follow along at those speeds. You may not understand everything that is said, but you can always go back and re-listen to sections if needed, let it broaden your known unknowns, and at the least it should help to remove some of your unknown unknowns.

I did a listing of Software Development Podcasts previously, and am going to try and make this a yearly or bi-yearly update based off how frequently this list of podcasts change in my rotation.

.NET Podcasts

Ruby Podcasts

  • Ruby Rogues – Panel discussion on various Ruby related topics and projects.

Clojure Podcasts

  • The Cognicast – Formerly Think Relevance podcast
  • Mostly λazy – Infrequent updates, but enjoyed the episodes that have been released

JavaScript Podcasts

  • JavaScript Jabber – Panel discussion on JavaScript topics, started by the host who started Ruby Rogues. The first episodes were hard to listen to due to some negativity, but have picked up listening to it again in the 50’s episode numbers, and working my way back as I get a chance.

Erlang Podcasts

  • Mostly Erlang – Panel discussion mostly about Erlang, but touches on related topics and other functional programming languages and how they relate to Erlang.

General

  • The Changelog – Podcast about Open Source projects from The Changelog
  • The Wide Teams Podcast – Hosted by one of the panelists of Ruby Rogues, with a focus on distributed software development, with the goal to find out the good and the bad experiences and help share information on how distributed teams work.
  • Software Engineering Radio – Recently I have only been finding a few shows on topics that seem interesting, but have a large backlog of shows with interesting topics.
  • GitMinutes – Podcast covering Git source control management.

New Comers

These are podcasts that I have only listened to a couple of episodes of, either because they have only released a couple, or have just started trying them.

On my list to check out

  • Food Fight – Podcast on DevOps
  • The Freelancers Show – Started by the same host of JavaScript Jabber and Ruby Rogues about freelancing. I would think the information would be relevant to full time employees even for working to build ones career.

If you have any other podcasts that are good please feel free to add your list of podcasts that I have left out to the comments.

**Updated 2013-10-24 7:54 CDT to include link to previous list of Software Development Podcasts
**Updated 2013-10-24 22:13 CDT to include The Changelog, a “podcast covering what’s new and interesting in open source”
**Updated 2013-10-24 22:28 CDT to include GitMinutes

, , , , , , , , , , ,

Leave a comment

Ruby and Puma – Read error: #<NoMethodError: undefined method `each’ for #<String:0x00000001611790>>

Was standing up a Puma web service late into the work day yesterday, and could not figure out why I was getting the below error when I should be seeing my results as a CSV result.

2013-05-28 19:00:59 -0500: Read error: #<NoMethodError: undefined method `each' for #<String:0x00000001611790>>
/home/reporting/reporting_mux/shared/bundle/ruby/1.9.1/gems/puma-2.0.1/lib/puma/server.rb:482:in `handle_request'
/home/reporting/reporting_mux/shared/bundle/ruby/1.9.1/gems/puma-2.0.1/lib/puma/server.rb:243:in `process_client'
/home/reporting/reporting_mux/shared/bundle/ruby/1.9.1/gems/puma-2.0.1/lib/puma/server.rb:142:in `block in run'
/home/reporting/reporting_mux/shared/bundle/ruby/1.9.1/gems/puma-2.0.1/lib/puma/thread_pool.rb:92:in `call'
/home/reporting/reporting_mux/shared/bundle/ruby/1.9.1/gems/puma-2.0.1/lib/puma/thread_pool.rb:92:in `block in spawn_thread'

The original return value of the call method was setup as:

[200, {"Content-Type" => "text/html"}, str]

For those with some more experience in using Puma, or Rack, you may even see what the problem was right away. Being my first attempt at standing up a Puma instance, I chased down a few red herrings, but at least they were needed updates. I first tried to update the MIME type to be text/comma-separated-values instead of text/html, and had the results setup to return as an attachment. These changes were kept, as they were the behavior of an existing implementation we are mirroring, but the call was still erroring out.

I finally stumbled across Puma’s Example config.rb file on Github.com and buried in there I saw the issue. The last element of the array returned by a Rack call method, has to be an Array, or at least Enumerable. I was returning a String, and that is what was causing the #<NoMethodError: undefined method `each' for #<String:0x00000001611790>>. I changed the last element to be the string wrapped in an array, and voilà, everything worked. So below is what I wound up with after the too long bug hunt.

[200, {"Content-Type" => "text/comma-separated-values",
       "Content-Disposition" => "attachment; filename=#{filename}" }, [str + "\n"]]

Hope this helps someone else with a similar problem, and can save you the long evening of debugging that I went through.
–Proctor

, ,

1 Comment

Error installing iconv

Today I was trying to install the iconv gem, and was getting this error

$gem install iconv
Building native extensions.  This could take a while...
ERROR:  Error installing iconv:
	ERROR: Failed to build gem native extension.

        <rvm_dir>/rubies/ruby-2.0.0-preview2/bin/ruby extconf.rb
checking for rb_enc_get() in ruby/encoding.h... yes
checking for iconv() in iconv.h... no
checking for iconv() in -liconv... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=<rvm_dir>/rubies/ruby-2.0.0-preview2/bin/ruby
	--with-iconv-dir
	--with-iconv-include
	--without-iconv-include=${iconv-dir}/include
	--with-iconv-lib
	--without-iconv-lib=${iconv-dir}/
	--enable-config-charset
	--disable-config-charset
	--with-config-charset
	--without-config-charset
	--with-iconvlib
	--without-iconvlib


Gem files will remain installed in <rvm_dir>/gems/ruby-2.0.0-preview2/gems/iconv-1.0.2 for inspection.
Results logged to <rvm_dir>/gems/ruby-2.0.0-preview2/gems/iconv-1.0.2/ext/iconv/gem_make.out

After a bit of searching, I found a number of answers suggesting that I would need to reinstall ruby via RVM, but in the following StackOverflow question
failed-to-build-iconv-gem-on-ruby-1-9-2.

In it was the solution:
gem install iconv -- --with-iconv-dir=/usr/local/Cellar/libiconv/1.13.1

After that, all was well.

–Proctor

, ,

Leave a comment

Looking for a new job

The company I have been working for had to just go through a round of layoffs, due to not getting one of their main contracts renewed do to lack of funding by the state, and I have been included in that round.

My primary experience has been in C# on the Microsoft .Net framework for the past 10 years. Of those 10 years, 9 of them were working against the same product, and product suite allowing me to learn how some inconsequential decisions are not so inconsequential long term, and the value of care and commitment to a code base can be.

I am thinking that this would be a good opportunity to open myself up in trying to venture into a new language and toolset such as Ruby or Clojure. This is my open announcement for anybody who would like to poach a .Net developer if you are having problems finding developers fluent in your programming language.

–Proctor

, , , ,

Leave a comment