samedi 27 juin 2015

Using arrays in regular expressions?

Does anyone know if there is a way to use an array in a regular expression? suppose I want to find out if somefile.txt contains one of an array's elements. Obviously the code below doesn't work, but is there something similar that does work?

array = [thing1 thing2 thing3]
file = File.open("somefile.txt")

file.each_do |line|
if /array/.match(line)
puts line
end

Basically I've got a file that contains a list of words that I need to use as search terms in another large file, and I'd like to avoid something like this:

($somefile =~ /(thing1|thing2|thing3)/)

How do I test fulltext fields using sunspot_matchers in rspec?

My search controller does this:

search = Sunspot.search(Item) do
  fulltext params[:q] do
    fields(:foo, :bar, :bletch)
  end
end

And in my rspec controller test I have the following:

get :show, :q => q

expect(Sunspot.session).to have_search_params(:fulltext, q, proc do
  fields(:foo, :bar, :bletch)
end)

Side note: when I tried using a block instead of a proc, the block was never executed:

have_search_params(:fulltext, q) do
  # This block was never executed
end

My support/sunspot.rb contains only the following:

Sunspot.session = SunspotMatchers::SunspotSessionSpy.new(Sunspot.session)

When I run the test, I get the following error:

  1) SearchController GET search when a user runs a search performs a fulltext search on the right fields
     Failure/Error: fields(:foo, :bar, :bletch)
     NoMethodError:
       undefined method `fields' for #<Sunspot::DSL::Search:0x007fa0f0da0168>
     # ./spec/controllers/search_controller_spec.rb:28:in `block (5 levels) in <top (required)>'
     # ./spec/controllers/search_controller_spec.rb:27:in `block (4 levels) in <top (required)>'

What's the right way to test that my controller is supplying the correct params when doing a fulltext search? Google yields no relevant results when querying this problem.

How to get stand-alone ohai to recognize custom plugin_path?

I have chef configured to add "/etc/chef/ohai_plugins" to Ohai::Config[:plugin_path]. However, the Chef documentation says:

"The Ohai executable ignores settings in the client.rb file when Ohai is run independently of the chef-client."

So, how can I get a stand-alone run of ohai to load and use the plugins in that custom path?

(Background: I have a custom plugin that reports some information that we keep track of for a fleet of servers, like whether a server has been patched for heartbleed or shellshock. I want to be able to run "ssh somehost ohai", parse the JSON that gets sent back, and extract the information I need.)

Thanks.

Finding smallest prime factor

I am trying to create a function that returns the smallest prime factor of a given number:

require 'prime'

def findSmallestPrimeFactor(number)
  return 2 if number.even?
  return number if Prime.prime? number
  arrayOfFactors = (1..number).collect { |n| n if number % n == 0 }.compact
  arrayOfFactors.each { |n| arrayOfFactors.pop(n) unless Prime.prime? n }
  return arrayOfFactors[0]
end

findSmallestPrimeFactor(13333) returns 1, which should not be happening since 1 should be removed from arrayOfFactors during line 7, as Prime.prime? 1 returns false

It sometimes returns nothing:

puts findSmallestPrimeFactor(13335) # => returns empty line

This issue only occurs when working with a number that is not even and is not prime, i.e lines 4 and 5 are ignored.

Also, when this is finished I will be passing some very large numbers through it. Is there any shorter or more efficient way to do lines 6-8 for larger numbers?

What's the difference between a secure compare and a simple ==(=)

Github's securing webhooks page says:

Using a plain == operator is not advised. A method like secure_compare performs a “constant time” string comparison, which renders it safe from certain timing attacks against regular equality operators.

I use bcrypt.compare('string', 'computed hash') when comparing passwords.

What makes this a "secure compare" and can I do this using the standard crypto library in Node?

Failure/Error: visit movie_url(movie) ActionView::Template::Error: wrong number of arguments (3 for 0..1)

This question sounds very similar to those that have already been asked and answered thought I can't seem to figure it out.

I have the following error message that makes my spec test fail.

Failures:

  1) Navigating movies allows navigation from the detail page to the listing page
     Failure/Error: visit movie_url(movie)
     ActionView::Template::Error:
       wrong number of arguments (3 for 0..1)
     # ./app/views/movies/show.html.erb:24:in `_app_views_movies_show_html_erb__1138068182152565203_70152862507580'
     # ./spec/navigate_movies_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 0.20747 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/navigate_movies_spec.rb:5 # Navigating movies allows navigation from the detail page to the listing page

Randomized with seed 14064

My actual spec test looks like this:

require 'spec_helper'
include Rails.application.routes.url_helpers

  describe "Navigating movies" do
    it "allows navigation from the detail page to the listing page" do
    ...
    visit movie_url(movie)

    click_link "All Movies"

    expect(current_path).to eq(movies_path)
  end
end

My show page:

<article>
  <p>
     <%= link_to "All Movies", movies_path %>
  </p>
</article>

Edited:

rake routes
Prefix Verb URI Pattern           Controller#Action
movies GET  /movies(.:format)     movies#index
 movie GET  /movies/:id(.:format) movies#show

Mobile app development using ruby

[I]s there any way to develop android apps using ruby? [I]f any[,] please enlighten me on this,[ ]and please let me know the frameworks used.