vendredi 8 mai 2015

Gem needs to access project specific folder

I am planning to create a ruby gem project and finally add that library gem in to another project. gem does some extra steps which needs to be completed before project feature file execution. Now i need to access the project feature folder from this gem. I tried this code

feature_folder= File.join("**", "features")
Dir.glob(feature_folder)

but didnt work. Is there anyway i can access the project features folder from my gem? I couldn't find how exactly cucumber access the project folders ( features , step_definitions) from cucumber gem.

Why in ruby, 0.692 * 3 = 2.0759999999999996?

$ ruby -v
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]

$ irb
irb(main):001:0> 0.692 * 3
=> 2.0759999999999996

I just ran into these numbers by chance. Is this related to Why Are Floating Point Numbers Inaccurate?.

How is it that you can do `Integer('1')` in Ruby?

How does the standard library allow you to coerce a String into an Integer with Integer('1') #=> 1? Is there a language feature that allows one to create this kind of conversion syntax in a class?

Getting a file name instead of directory name in Ruby

PLEASE NOTE THAT I AM A NOOB

So I recently started learning Ruby and a buddy is motivating me to get into it so he helped me get on my way. We're currently building a program that allows you to download the HTML code from a page. My code isn't working. The code is:

require 'net/http'

print "Source: "
source = gets.chomp

print  "Path: "
filename = gets.chomp

page = Net::HTTP.get(source, path) 
path = File.join(File.dirname(__FILE__), filename)
file = File.open(path, 'r+')
file.write(page)
file.close()

and the error message I get is

webdownload.rb:11:in `initialize': Is a directory - ./ (Errno::EISDIR)
from webdownload.rb:11:in `open'
from webdownload.rb:11:in `<main>'

My buddy said it's because I'm declaring a directory name instead of a file name (which I can deduce from the error message). I however don't know how to change it into a file name.

Fixture finder methods

I have two models: User, Group

I have fixtures for both of them.

I have UserTest class which is unitTest and I can use fixture finder users(:someuserfixture). However, if I try to use groups(:somegroupfixture) it says that groups isn't recognized.

How can I access fixtures of another model?

ruby - split an array into sub arrays when a value changes and ignore/delete that value

I want to split the following array into sub arrays so that the sub-arrays start and finish when the 1's start and finish...

a=[1,1,0,0,1,0,1,1,1]

so I end up with this as a new array...

=> [[1,1],[1],[1,1,1]]

anyone got any ideas...?

thanks in advance Matt

Chef, Vagrant and private git cloning

i have acces to git repo on host, and i have a Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu14.04"
  config.vm.box_url = "http://ift.tt/1cwfxmd    vagrant-disk1.box"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = 1024
    vb.cpus = 2
  end
  config.ssh.forward_agent = true
  config.vm.provision :chef_solo do |chef|
  # chef.log_level = :debug
    chef.cookbooks_path = "./cookbooks"
    chef.add_recipe "git_sync"
  end
end

if i run vagrant and ssh into it, i could also git clone my private repo, (recipe "install_pkgs" is to install git on vm) but the pecipe "git_sync" gets an error like:

[2015-05-08T18:40:26+00:00] ERROR: Running exception handlers
[2015-05-08T18:40:26+00:00] ERROR: Exception handlers complete
[2015-05-08T18:40:26+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[2015-05-08T18:40:26+00:00] ERROR: git[/home/vagrant/geomongo] (git_sync::default line 1) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '128'
---- Begin output of git ls-remote "git@bitbucket.org:galiaf95/test.git" HEAD ----
STDOUT: 
STDERR: Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
---- End output of git ls-remote "git@bitbucket.org:galiaf95/test.git" HEAD ----
Ran git ls-remote "git@bitbucket.org:galiaf95/test.git" HEAD returned 128

================================================================================
Error executing action `sync` on resource 'git[/home/vagrant/geomongo]'
================================================================================


Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '128'
---- Begin output of git ls-remote "git@bitbucket.org:galiaf95/test.git" HEAD ----
STDOUT: 
STDERR: Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
---- End output of git ls-remote "git@bitbucket.org:galiaf95/test.git" HEAD ----
Ran git ls-remote "git@bitbucket.org:galiaf95/test.git" HEAD returned 128


Resource Declaration:
---------------------
# In /tmp/vagrant-chef-1/chef-solo-1/cookbooks/git_sync/recipes/default.rb

  1: git "/home/vagrant/geomongo" do
  2:   # repository "git@bitbucket.org:osll/geomongo.git"  
  3:   # repository "http://ift.tt/1cwfxCz"
  4:   repository "git@bitbucket.org:galiaf95/test.git"
  5:   action :sync
  6: end


Compiled Resource:
------------------
# Declared in /tmp/vagrant-chef-1/chef-solo-1/cookbooks/git_sync/recipes/default.rb:1:in `from_file'

git("/home/vagrant/geomongo") do
  provider Chef::Provider::Git
  action [:sync]
  retries 0
  retry_delay 2
  destination "/home/vagrant/geomongo"
  revision "HEAD"
  remote "origin"
  cookbook_name :git_sync
  recipe_name "default"
  repository "git@bitbucket.org:galiaf95/test.git"
end



[2015-05-08T18:38:31+00:00] INFO: Forking chef instance to converge...
[2015-05-08T18:40:26+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.

Here is my git_sync.rb recipe

git "/home/vagrant/geomongo" do"
  repository "git@bitbucket.org:galiaf95/test.git"
  action :sync
end

I'm new to chef and vagrant and it would be great to have some very comprehensive examples.