So I am running Ruby 1.9.3 and I am trying to use the Prime class.
I have added require mathn at the top of my .rb file
Other than the method name, this code came from the documentation page:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/prime/rdoc/Prime.html
def prime_number(n)
Prime.each(n) do |prime|
p prime
end
end
prime_number(100)
Any ideas why this doesn't work? The error I get says
undefined method each for Prime:Class
I tried it in Ruby 1.9.3 and it worked. Tried it again in Ruby 1.8.7 and got your error message. A newer version of Ruby will probably solve your problem.
Your problem is that you did require mathn and you must have require 'mathn' at the top of your file.
Either that, or you have neglected the quotes when you described your code above.
Related
Hey guys i have a problem i am facing with the twitter gem. I have a file (twitter.rb) with this content
require "rubygems"
require "twitter"
puts Twitter.user_timeline("roykasa").first.text
puts Twitter.user("roykasa").location
search = Twitter::Search.new
search.containing("hate").to("StewieJokess").
result_type("recent").each do |r| puts r.text end
When i run the file i get this error :
uninitialized constant Twitter (NameError)
I read somewhere on SO where a user had a similar problem and he solved it by installing a new version of ruby and rubygems but the problem i am having is am running suse 12.1 and am running the latest versions of both ruby and ruby gems. No rpms can be found from 3rd parties anywhere. Atleast i have searched. Does anyone know another way round this?
If you are running Ruby 1.8.x you should be able to solve your problem by renaming your own script to anything different than twitter.rb.
This is because the main file in the twitter gem is named exactly like this and your file probably overrides it in combined virtual file system that the $LOAD_PATH order creates. Before Ruby 1.9.x, require did not only load from library directories, but preferred to load files relative to the current working directory of your process, in this case, the directory where your script lies in.
Do not name your file as twitter.rb, also make sure there is no other file in the same directory with the name twitter.rb
I am trying to use the script blogger.rb and I just can't get it work. It keeps giving me the error :
blogger.rb:294:in text2html': undefined methodcapture2' for Open3:Module (NoMethodError)
The script does a require Open3 in the beginning. I don't understand where is the problem ! I have no knowledge of Ruby. However, I can intelligently read and edit codes in general.
I'd guess that you're using Ruby 1.8 but the script requires 1.9. The Open3 class in 1.8.7 has a popen3 class method and nothing else. The Ruby 1.9 Open3 has the capture2 and capture2e class methods that you're looking for. So you need to upgrade your Ruby to 1.9 or find another script.
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.
I'm trying to get 'jcode' for ruby, but I type "gem install jcode" and it says nothing exists?
Does anyone know why? I'm trying to manipulate UTF-8 encoded strings.
Ruby >= 1.9 doesn't require jcode, a module to handle japanese (EUC/SJIS) strings, as it supports unicode natively.
This should fix the problem:
require 'jcode' if RUBY_VERSION < '1.9'
but must say I did not test it deeply...
Have you tried simply:
require 'jcode'
?
As it happens, jcode is part of the Ruby Standard Library.
Try not to include it at all. It should be included automatically in later versions of Ruby.
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.)