mardi 4 août 2015

Does ActiveSupport's Object#try method work with Thor::CoreExt::HashWithIndifferentAccess?

I'm working on a Middleman (Ruby) site. There are data objects which are instances of Thor::CoreExt::HashWithIndifferentAccess. However, it seems whenever I call try that I get nil.

I see that Thor::CoreExt::HashWithIndifferentAccess takes advantage of method_missing. Does this break how try works? Is anyone able to replicate this?

UPDATE: Here's a simple example. I'm using Middleman which provides access to data files.

# /data/people.yml
jane:
  name: Jane Doe
  age: 25
john:
  name: John Doe
  age: 25

Then in a template you have access to the data.people method which returns a Thor::CoreExt::HashWithIndifferentAccess instance:

data.people.inspect
 #=> {"jane"=>{"name"=>"Jane Doe", "age"=>25},"john"=>{"name"=>"John Doe", "age"=>25}}

data.people.class
 #=> Thor::CoreExt::HashWithIndifferentAccess

Now when I call try on anything I get nil:

data.people.jane
 #=> {"name"=>"Jane Doe", "age"=>25}

data.people.try('jane')
 #=> nil

data.people.try(:jane)
 #=> nil

data.people.try(:something_that_does_not_exist)
 #=> nil

data.people.jane.try(:name)
 #=> nil

"Watir::Exception::NoMatchingWindowFoundException: browser window was closed" error when using IE with Page-Object

I'm running tests in Ruby Mine 2.7.1 using Page-Object + Rspec.

Environment: Windows 7 x64, Ruby 1.9.3 p551, IE 11.

Gems: watir, rspec, bundler, page-object

When i run tests in Chrome or Firefox everything is ok.

But when i try to run them in IE, the IE window with correct page gets opened and after that i get an error specified in Subject: "Watir::Exception::NoMatchingWindowFoundException: browser window was closed"

The point is that the browser is actually open at that moment and shows a correct page. (See the screencast showing what happens: http://ift.tt/1hhIBzU)

Test gets finished with an error and the config.after do section doesn't get executed because the browser window remains opened.

That error happens when the following code is executed:

before(:each) do

visit <ClassName>

end

Seems like the IEDriverServer loses connection with IE right after the page is opened.

The most confusing this is that in very rare cases, everything is working good, and in even more rare cases everything is working with significant slowdowns with timeout error at the end.

sleep <n> delays don't help.

I tried different versions of selenium-webdriver gem (it is required by watir gem), different versions of IEDriverServer for both x86 and x64 platforms - result is the same.

What do i do to set IEDriverServer not to lose the IE browser?

How to implement sudo su in Ruby script?

I have sudo access to oracle user being non_root user (say guest_admin user). I need to login as oracle user to run some steps but my ruby script is failing at the very first step i.e., sudoing as oracle. Below is the piece of code.

require "java"
require "highline/import"
require "rubygems"
require 'rvm/capistrano'

curr =  File.expand_path(File.dirname(__FILE__))
$LOAD_PATH << curr  unless $LOAD_PATH.include? curr

system "sudo su - oracle"

I am getting the following error:

ruby test.rb

$rvm_path (/usr/local/rvm/scripts/rvm) does not exist.
/usr/local/rvm/scripts/rvm: line 171: __rvm_teardown: command not found
stty: standard input: Invalid argument
stty: standard input: Invalid argument

Can somebody help me fixing this? It's a show-stopper for me right now.

I tried to move require 'rvm/capistrano' at the end of the file but that dint help much.

Thanks.

Check how many parameters block being passed has, and then invoke yield with different parameters

I want to know if something like this is possible, as hash.each does two different things based on arguments passed to block

{ "a"=>3, "b"=>2}.each {|k| puts k}

{ "a"=>3, "b"=>2}.each {|k,v| puts k}

Both output different things, and not just what's below..

a
b
..
a
b

i get this

a
3
b
2
..
a
b

So i want to know if there is a way to get my function to do something custom depending on the number of arguments of block being passed to it.

like this

def my_method do
   if yield.args==2
     yield("hi","bro")
   else 
     yield("what's up")
   end
end

my_method {|a,b| puts a; puts b;} //"hi" "bro"

my_method {|a| puts a; } //"whats up"

Rails routes: Wrong singular for resources

I have the following line in my routes.rb (Rails 4.1.4):

resources :request_caches

However, when I run rake routes I get the following output:

request_caches    GET    /request_caches(.:format)            request_caches#index
                  POST   /request_caches(.:format)            request_caches#create
new_request_cach  GET    /request_caches/new(.:format)        request_caches#new
edit_request_cach GET    /request_caches/:id/edit(.:format)   request_caches#edit
request_cach      GET    /request_caches/:id(.:format)        request_caches#show
                  PATCH  /request_caches/:id(.:format)        request_caches#update
                  PUT    /request_caches/:id(.:format)        request_caches#update
                  DELETE /request_caches/:id(.:format)        request_caches#destroy

As you can see, Rails somehow maps request_caches plural to request_cach singular. But it should be request_cache. Is this some kind of special case, because of the word caches? I've also played around with

resources :request_caches, as: :request_cache

But this results in wrong routes like request_cache_index. And furthermore, I think this is a standard task and should be solved clearly using Rails intern route helpers.

So, what am I doing wrong?

Using def_delegate with a hash

I know how Forwardable#def_delegate works with methods on objects, but is there a similar way to forward methods names to hash keys. Like:

hash = { some_value: 42, other_value: 31415 }
def_delegate :hash, :some_value, :other_value

Calling object.some_value should return 42

PS: def and class eval is a way, but is there a nicer way?

why does the array elements change after block?

I am trying to solve this problem

Given a sentence containing multiple words, find the frequency of a given word in that sentence.

Construct a method named 'find_frequency' which accepts two arguments 'sentence' and 'word', both of which are String objects.

Example: The method, given 'Ruby is The best language in the World' and 'the', should return 2 (comparison should be case-insensitive).

Hint: You can use the method Array#count to count the frequency of any element in the given array.

Since the comparison should be case-insensitive. I use these code to help:

word = "the"
word_set = []
word.size.times do |i|
  word[i] = word[i].upcase
  word_set << word
  word[i] = word[i].downcase
end

Inside the block every time after upcase method the word does change and does add to the word_set, however when the block finish the word_set just contain the the the What is the problem?