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
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
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
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