Avoid Ruby open-uri cache - ruby

I am trying to run a script from my Github Gist page but OpenURI or something else seems to cache it on they way and it never actually updates. I am using the code below on Ubuntu 16.04 and Ruby 2.3.1. I just want it to fetch the script from the url every time instead of using a cached version.
#!/usr/bin/ruby -w
require "open-uri"
url = "https://gist.githubusercontent.com/*/*/raw/*/*.rb"
code_from_url = open(url) {|f| f.read }
eval(code_from_url)

Nevermind, there was no cache. It was just an incorrect link to a revision specific gist. The correct way to get the latest version of the first file in a gist is https://gist.githubusercontent.com/{username}/{gist}/raw

Related

new window creation in watir

I am new to watir. I am trying to create new ie window with
browser = Watir::Browser.new
but it gives error message like
`user_is_bewildered': Error in the default values: :browser's value must be one of 'ie', 'firefox', or 'safari', and '' doesn't look right. (StandardError)
I donno how to set default browser. Can some one help me? There is a another thread here. But I am not able to understand what i need to do in ffi.
Thanks
If this is the original Watir gem, then the following is how I used to launch it:
require 'rubygems'
require 'watir'
Watir::Browser.default = "firefox"
browser = Watir::Browser.new
# Whatever you want to do in watir
IamChuckB's answer may be a more efficient way of doing this, but having not used it, I'm not sure.
You need to tell it which browser to open. Try this:
browser = Watir::Browser.new :ff
I haven't played around with watir since my last job so I had to look this up. As I last recall, WATIR was not entirely integrated with FireWATIR (the Firefox based variant). It's good to see that the two have apparently been reconciled in the meantime.
Taken from Watir in Five Minutes on Zeljko Filipin's github, BTW.
To install devkit,
create a folder in the ruby directory called devkit
get the devkit from here
unpack it into the devkit directory created in step 1
add c:\ruby193\devkit\bin;c:\ruby193\devkit\wming\bin to your path, of course adjusting for your ruby install directory
now open a command prompt to install the gem again
c:\> gem install watir
The issue is resolved. Thanx for the the inputs. The issue was with wrong nokorigi gem installation, initially i installed x86-mswin32-60, i uninstalled it and tried with x86-mingw32, it solved.

Using gem Curb (curl) to download file

I have understood that I could use the RubyGem Curb to fulfill my needs of curl'ing down a file to my computer. But I cannot figure out how to actually "touch" the file. This is what I have got so far
include 'rubygems'
include 'curb'
curl = Curl::Easy.new('http://wordpress.org/latest.zip')
curl.perform
I think I understand that perform actually downloads the file. But how can I interact with the file afterwards? Where is it downloaded to?
Regards,
Mattias
The file can be accessed with the body_str method.
puts curl.body_str
Check the docs at http://rdoc.info/gems/curb/0.7.15/.
After perform, the content is in e.g. curl.body_str.

Sinatra cannot find views on Ruby 1.9.2-p0

I'm quite new to Ruby language (up to now I developed in Groovy + Grails) but since I was curious about it I wanted to try Sinatra on Ruby 1.9.2-p0.
I have a trivial website that is contained in /mywebpage and has 2 files:
# blog.rb
get '/' do
'Hello World!'
end
get '/impossible' do
haml :index
end
and
#config.ru
path = File.expand_path "../", __FILE__
$LOAD_PATH << (File.expand_path ".") + "/views"
require 'haml'
require 'sinatra'
require "#{path}/blog"
run Sinatra::Application
then in the same folder I have a /views/ folder that contains index.haml.
I try to run the server with rackup -p8080 but when I try to get /impossible I receive the following error:
Errno::ENOENT at /impossible
No such file or directory - /home/jack/mywebpage/<internal:lib/rubygems/views/index.haml
By searching over internet it seems that this maybe caused by "." not being included in $LOAD_PATH so I tried to add it or add directly views ./views so that actually $LOAD_PATH.inspect gives me correct path: ..., "/home/jack/mywebpage/views"]
But still it doesn't seem to work. Being quite new to the framework and the language I was wondering if I'm doing something wrong. any clues?
Running Sinatra with Ruby 1.9.2 the template directory is no longer implicitly 'views', you need to set it yourself.
set :views, File.dirname(__FILE__) + "/views"
Note that currently Ruby also has Kernel#__dir__() method that is equivalent to File.dirname(__FILE__).
This, and other issues with 1.9, will be have been solved in Sinatra 1.1. You could use this fork: http://github.com/rkh/sinatra/tree/1.1
I ran into a similar problem, and solved it like this. I didn't dig into the problem, but this is what I found and it works. It'll supposedly be fixed in the next version of Sinatra (which they should really get out the door, just to fix these few 1.9.2 bugs).
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
enable :run
get '/' do
"Hello, world!"
end
Edit: It seems there are multiple bugs with Sinatra on 1.9.2. This one will fix Sinatra apps not starting on 1.9.2. I don't use a views directory (I like to keep my apps single-file), so I didn't run into your particular problem. This fix most likely won't help you at all. I probably should have read your problem more closely..
gem install sinatra --pre
I ran into that last week and didn't find a suitable fix on the Sinatra site short of tweaking the sinatra code. I'm using rvm for my development and switched to try sinatra on Ruby 1.8.7 and it works fine again, so that's where I left it.
Oh, since you're new to Ruby, you might not know about rvm, so here's the lowdown. RVM is Mac only and highly recommended for managing your Ruby version and gems. It makes it trivial to have multiple Ruby versions and alternate groups of gems for development and testing. Everything is stored in your ~/.rvm directory so it's easy to blow it all away if you need to.
http://rvm.beginrescueend.com/
I just looked at the Sinatra site again about the problem to see if there was anything new, but it appears they consider the following to be an acceptable fix:
http://github.com/sinatra/sinatra/issues/#issue/50
I'm a bit adverse to having to edit the source of Sinatra as recommended by issue #50, but it's not real hard to do. I'd like to see them put out an update so we'd have an official fix but I haven't seen anything yet:
gem env will tell you the "GEM PATHS". Sinatra's gem will be in one of those. The line mentioned in issue #50 goes into base.rb. On my machine it's something like ...gems/ruby-1.9.2-p0/gems/sinatra-1.0/lib/sinatra/base.rb.
Insert:
/<internal:/, # ruby 1.9.2-p0 hacks
at line 1020.
Save the file and you should be good to go.

Ruby "No Such File To Load - sqlite3" on OS X

I was trying to create a quick little script that would insert data into an SQLite db for me but I can't get past the first few steps.
I have done "sudo gem install sqlite3-ruby", my Ruby version is 1.8.7 (I used the Ruby one click installer from rubyforge.org).
My script is incredibly simple. It looks like this:
#!/usr/bin/ruby -w
require "csv.rb"
require "sqlite3"
begin
CSV.open('updateddata.sql', 'r') do |row|
p row
end
rescue => err
puts "Exception : #{err}"
err
end
It never makes it past the line require "sqlite3". It just errors and tells me that it can't find that file to load.
I don't understand how it won't work even after using the One click installer (which is supposed to have SQLite built into the install).
I'm not even sure where to go from here.
I am not a Ruby developer at all, I just wanted to use it as a learning experience and to quickly complete this task for myself.
Dunno if Ruby in OS X behaves differently, but normally you need to do require 'rubygems' before requiring any gems.
You can also add
export RUBYOPT=rubygems
to your profile.
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/43fc65132487f98e/?pli=1 using sqlite3-ruby gem solved my issue on this

The Most Basic Nokogiri Program Fails -- Documentation Problem or Bug?

I decided to give Nokogiri a try, and copied the following program straight from http://nokogiri.rubyforge.org/nokogiri/Nokogiri.html (adding only the require 'rubygems' and the I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 constant):
require 'rubygems'
I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 = 1
require 'nokogiri'
require 'open-uri'
# Get a Nokogiri::HTML:Document for the page we’re interested in...
doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove'))
# Do funky things with it using Nokogiri::XML::Node methods...
####
# Search for nodes by css
doc.css('h3.r a.l').each do |link|
puts link.content
end
It returned no results. But when I changed
doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove'))
to
doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove').read)
the program worked as expected. Notice that the only difference was the addition of the .read at the end of the line. I would never have figured this out by myself, because just about every bit of example code leaves off the .read. The one place that included it, ironically was a post by one of the Nokogiri developers (at http://tenderlovemaking.com/2008/11/18/underpant-free-excitement). Did something in the API change? What am I missing?
I'm using Nokogiri 1.3.2.
Thank you.
I copied and pasted your (original) code into a Ruby file and ran it on my system (ruby 1.8.6p369, Nokogiri 1.3.2) and it worked fine. Might there be something else in your environment that could be causing the problem? Nokogiri aside, what does open('http://www.google.com/search?q=tenderlove') return for you?
Not sure what your issue is, but the call to open is from open-uri not nokogiri. So do some experimenting taking nokogiri out of play.
$ irb
>> require 'open-uri'
=> true
>> f = open('http://www.google.com/search?q=tenderlove')
=> #<File:/var/folders/LA/LACsuKOVHtaEgmBzsJcGAE+++TI/-Tmp-/open-uri.7455.0>
>> f.read
=> "<!doctype html><head><title>tenderlove - Google Search</title>...
I upgraded to Nokogiri 1.3.3, and upgraded libxml2 to 2.7.3. I no longer need to use the ridiculous I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 = 1 statement to avoid error messages, and the program works without the extraneous .read.
It's always good to check your version of Nokogiri and libxml to make sure they're current.
As of today (9/22/09) this is current on MacOS:
nokogiri -v
---
nokogiri: 1.3.3
warnings: [ ]
libxml:
compiled: 2.7.4
loaded: 2.7.4
binding: extension
(I put a space inside the empty warnings array to keep it from looking like a box.)

Resources