Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I use the webdriver-user-agent to resize an automated browser easily, and it has user agents and window sizes configurable in a YAML file. I want to add to the YAML file, and I'm wondering what the best way to do this is. I may have additions that only I'll want to use, so I'm ruling out a pull request on Github, and I don't want to add it under the gem's lib directory, because then I will lose my work when I upgrade the gem.
What's the best way to add additional code to a gem?
Unfortunately, you're talking about adding configuration to the gem, which is less easy than adding code, but still doable.
You're going to monkeypatch whatever it is that the gem uses to load its YAML configuration to instead load the configuration of your choosing. Looking at the source you can see that the YAML is being loaded by #devices and the filename comes from #devices_file. Fortunately for us (though not so much for performance), the loaded YAML isn't cached so just replacing #devices_file will do what you want:
require 'webdriver-user-agent'
module Webdriver
module UserAgent
module Devices
def devices_file
File.expand_path("../my-devices.yml", __FILE__)
end
end
end
end
This will look for a file called my-devices.yml in the same directory as the file the above code is in (like your spec_helper.rb or whatnot) - adjust appropriately.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is monkey patching possible in Go as in Ruby?
If so, I have a third party library build, which contains one function called encrypt. Now i want to overwrite the behaviour of the function. Is that possible?
Golang is a compiled language, you'll have to edit the code, recompile and deploy all over again. "monkey patching" isn't possible as if you're talking about changing the software after it is already deployed like ruby. (which is an interpreted language).
But if your concerns are beyond an hotfix and involve adding new code at runtime from external source, you might want to have a look at go plugins.
here's a nice medium post about it.
I'm not big on ruby so to make sure we are on the same page: my understanding is that monkey patching allows you to change the underlying behaviours (methods) attached to a given type at runtime.
In go this would be a big fat no no, probably should be in ruby too for the most part :)
Types are concrete in go, everything has to know what it is dealing with at compile time. It's difficult to propose a solution without a clearer understanding of the end goal, can you provide a more concrete example?
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have several Ruby scripts that have been refactored to use common methods. What folder structure should I use for these files?
For example: I have reports "Grower", "Fecal", "30Day", "30DayFecal", etc. that all use methods in files "date_of", "get_fecal_data", "get_fy", "chart_fecal", etc. I'm thinking that I should set up folders like;
App
-Grower
-Fecal
-30Day
-30DayFecal
-lib
-date_of.rb
-get_fecal_data.rb
-get_fy.rb
-chart_fecal.rb
Please advise.
Seems you're being partially influenced by the Rails folder layout.
app
models
controllers
views
lib
...
You can use that, since it is a common way to think, and will aid you getting advice from others. Just be sure to make it clear that you're not developing a Rails app, so you don't create other confusions.
Views
These can be any output-formatters. Rails app commonly export data in .csv format, for instance.
Controllers
These, along with services and related concepts, are things that manipulate data after a Model has retrieved it from a 'store' (e.g. a file on disk, database, or anything).
A common convention is that Controllers depend upon Models, whereas Lib[raries] is self-sufficient. Some would say that Services depend upon external API data.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am learning how to use the data structure in ruby.
I found this library on github : https://github.com/kanwei/algorithms/tree/master.
I have created a word_count.rb file locally.
I want to use the library I mentioned, locally in my ruby class file.
The link I mentioned has following project structure :
how can I use this project in class file ?
It is published as a gem (just the page you found does not document that fact): https://rubygems.org/gems/algorithms
So if you have a Gemfile in your project, add the line:
gem "algorithms", "~> 0.6.1"
Otherwise, run the following command:
gem install algorithms
and use it like you would any other gem, with
require 'algorithms'
in one of your source files.
require 'algorithms'
include Containers
http://kanwei.github.io/algorithms/
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've created a Ruby gem with the traditional 'lib' structure, but I also have another directory, say 'more' at the same level as 'lib'. However I cannot seem to access any of the methods in the 'more/*.rb' files. Can anyone tell me what needs to be done?
PS - I know from /Shopify/Liquid that they have a similar situation with 'lib' and 'performance' dirs, and there is a 'performance/shopify/liquid.rb' file and also a 'lib/performance' dir but for the life of me I cannot figure out how all that fits together. If I could resolve that then I think it would also be applicable to my solution.
From liquid's gemspec:
s.require_path = "lib"
It doesn't look like /performance is used in the gem, it's only for benchmarking and testing.
liquid/lib doesn't appear to have a performance directory. Looks like the word 'performance' is only used in the Rakefile really. Again for testing.
You can do this as well, no magic needed, it's just another directory.
If you want to include another directory as a library path, then alter the gemspec require_path value.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
EDIT: In short, what do I need to do to create a running application in Ruby, do I need to create a gem?
ORIGINAL QUESTION THAT SUCKED:
I've never taken any programming classes, or had assignments before so I'm asking this question in hopes this is clear to someone who has had many programming assignments before.
Basically the instructions state, "The Application must Run."
The instructions on the assignment uploader state that no executable files should be uploaded and the application should be put into a .zip.
For a second I thought I'd just create the app code locally, zip all the files, and upload them, but now I'm second guessing that. It seems like I should create a gem out of my application and then the readme that would be uploaded would instruct them to just install the gem?
Am I on the right track here? I suppose my question can be stated as, I need to create a ruby application that will run, is the best way to do that by creating a gem? Or am I completely over thinking this?
Thank you very much.
The most likely thing is that it means this:
The application must run via executing the command ruby myapp.rb
So it "must run" when you pass the filename of you code to the ruby executable.
Anything else wouldn't make much sense. If you are required to make a gem, that would likely have been explicitly clear. But you should clarify this with your instructor, as there isn't a lot to go on here.