Last active
October 13, 2015 13:07
-
-
Save nickdenardis/4200060 to your computer and use it in GitHub Desktop.
Useful Ruby Commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
books.values.each { |rate| ratings[rate] += 1 } | |
class BlogEntry | |
def initialize( title, mood, fulltext ) | |
@time = Time.now | |
@title, @mood, @fulltext = title, mood, fulltext | |
end | |
end | |
--------------- | |
http://stackoverflow.com/questions/1611237/how-to-manipulate-dom-with-ruby-on-rails | |
If what you're trying to do is manipulate HTML documents inside a rails application, you should take a look at Nokogiri. | |
It uses XPath to search through the document. With the following, you would find any h1 with the "blue" css class inside a document. | |
require 'nokogiri' | |
require 'open-uri' | |
doc = Nokogiri::HTML(open('http://www.stackoverflow.com')) | |
doc.xpath('//h1/a[@class="blue"]').each do |link| | |
puts link.content | |
end | |
After, if what you were trying to do was indeed parse the current page dom, you should take a look at JavaScript and JQuery. Rails can't do that. | |
--------------- | |
require 'uri/http' | |
uri = URI.parse("http://toolbar.google.com") | |
domain = PublicSuffixList.parse(uri.host) | |
# => "toolbar.google.com" | |
domain.domain | |
# => "google.com" | |
uri = URI.parse("http://www.google.co.uk") | |
domain = PublicSuffixList.parse(uri.host) | |
# => "www.google.co.uk" | |
domain.domain | |
# => "google.com" | |
----------------- | |
http://immediatenet.com/t/fs?Size=1024x768&URL= | |
----------------- | |
require 'net/http' | |
Net::HTTP.start("somedomain.net/") do |http| | |
resp = http.get("/flv/sample/sample.flv") | |
open("sample.flv", "wb") do |file| | |
file.write(resp.body) | |
end | |
end | |
puts "Done." | |
----------------- | |
Edit2: The solution which saves part of a file while downloading: | |
# instead of http.get | |
f = open('sample.flv') | |
begin | |
http.request_get('/sample.flv') do |resp| | |
resp.read_body do |segment| | |
f.write(segment) | |
end | |
end | |
ensure | |
f.close() | |
end | |
----------------- | |
http://fog.io/0.8.1/storage/ | |
----------------- | |
Directories | |
http://rubyist-journal.com/2010/05/27/the-dilemma-of-rails-root-vs-rails_root-complex/ | |
----------------- | |
-bash> echo "config/app_environment_variables.rb" >> .gitignore | |
config/app_environment_variables.rb | |
ENV['HTTP_USER'] = 'devuser' | |
ENV['HTTP_PASS'] = 'devpass' | |
As well, add the following lines to config/environment.rb, between the require line, and the Application.initialize line: | |
# Load the app's custom environment variables here, so that they are loaded before environments/*.rb | |
app_environment_variables = File.join(Rails.root, 'config', 'app_environment_variables.rb') | |
load(app_environment_variables) if File.exists?(app_environment_variables) | |
As the comment above says, by doing this you will be loading your environment variables before environments/*.rb, which means that you will be able to refer to your variables inside those files (e.g. environments/production.rb). This is a great advantage over putting your environment variables file inside config/initializers/. | |
Inside app_environment_variables.rb there's no need to distinguish environments as far as development or production because you will never check this file into your source code management system, hence it is for the development context by default. But if you need to set something special for the test environment (or for occasions when you test production mode locally), just add a conditional block below all the other variables: | |
if Rails.env.test? | |
ENV['HTTP_USER'] = "testuser" | |
ENV['HTTP_PASS'] = "testpass" | |
end | |
if Rails.env.production? | |
ENV['HTTP_USER'] = "produser" | |
ENV['HTTP_PASS'] = "prodpass" | |
end | |
------------------ | |
gem install rubyzip | |
Zip::ZipFile::open("all.zip", true) { | |
|zf| | |
Dir['{test,lib}/**/*'].each { |f| zf.add(f, f) } | |
} | |
To unzip it to the file system again you'd do something like: | |
OUTDIR="out" | |
Zip::ZipFile::open("all.zip") { | |
|zf| | |
zf.each { |e| | |
fpath = File.join(OUTDIR, e.name) | |
FileUtils.mkdir_p(File.dirname(fpath)) | |
zf.extract(e, fpath) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment