Here's my irb session:
irb(main):001:0> class User
irb(main):002:1> include MongoMapper::Document
irb(main):003:1> key :name, String
irb(main):004:1> key :age, Integer
irb(main):005:1> many :hobbies
irb(main):006:1> end
NameError: uninitialized constant User::MongoMapper
from (irb):2
irb(main):007:0>
which is right off of http://mongomapper.com/
I'm in windows 7, ruby 1.8.7 patchlevel 249. My gem list includes mongo, mongo_mapper, bson, and bson_ext (among others). I tried 'require'ing 'mongo_mapper' and/or 'mongo', and just got error messages about those 'require's.
I'm sure it's something simple, but as a ruby newbie, I'm stumped.
TIA
You have to
require "rubygems"
first on 1.8.7.
Ruby 1.9.2 automatically does it for you.
On 1.8.7 you can set an environment variable called "RUBYOPT" to do this for you.
See here.
Then after you have RubyGems loaded, you can load MongoMapper and everything should work.
require "mongo_mapper"
Related
I have the following:
input = gets.chomp
basket = input.strip.split(',')
basket.delete_if(&:blank?)
which should allow you to do: cat, dog,,
from there we can split that up, strip it clean and split it on , and then check for empty elements and remove them.
but this code gives me an error: delete_if: undefined method 'blank?' for "cat":String (NoMethodError) which does not make any sense to me. I thought that the whole purpose of blank? or empty? was to say remove this element if this is true.
ActiveSupport that comes with Rails adds the blank? method to String and many other classes. Since this method is not part of Ruby core, you need to have Rails or the ActiveSupport gem installed. If that gem is installed than you can require ActiveSupport's core extensions like this:
> 'foo'.blank?
# => NoMethodError: undefined method `blank?' for "foo":String
> require 'active_support/core_ext'
# => true
> 'foo'.blank?
# => false
require 'active_support'
require 'active_support/core_ext'
import "active_support" first
blank? is a method augmented to the String class from rails. It's not part of the ruby String class by default.
I am running ruby 1.9.3 on a linux box. I would like to use SOCKSSocket, however, I continue to run into the following error:
uninitialized constant SOCKSSocket
simple test using IRB
irb(main):001:0> require 'resolv-replace'
=> true
irb(main):002:0> SOCKSSocket
NameError: uninitialized constant SOCKSSocket
from (irb):2
from /usr/local/bin/irb:12:in `<main>'
here is the source code directly from resolv-replace.rb
class SOCKSSocket < TCPSocket
# :stopdoc:
alias original_resolv_initialize initialize
# :startdoc:
def initialize(host, serv)
original_resolv_initialize(IPSocket.getaddress(host), port)
end
end if defined? SOCKSSocket
I can't help but think that I need to install some dependency needed to enable socks or something. Anything would be helpful.
Ok, it seems the configure script does not have --enable-socks as part of it's list of valid options and that is the reason for the WARNING: unrecognized options ...
I did not track down how to add --enable-socks to the list of valid options, however, I did rig the script.
Edit: configure
find the section: Initialize some vars... and add enable_option_checking=no
# Initialize some variables set by options.
enable_option_checking=no
Now, run:
./configure --prefix=/usr/local --enable-socks
make
sudo make install
>ruby --version => 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]
then, try it out in irb
irb(main):001:0> require 'socket'
=> true
irb(main):002:0> require 'resolv-replace'
=> true
irb(main):003:0> SOCKSSocket
=> SOCKSSocket
irb(main):004:0>
I haven't done anything using SOCKSSocket yet, however, at least now it looks like I have it accessible to my code. Also, I assume there is some ENV var to disable option checking or a better way around it. I just did not track that down.
Thanks for your help!!
SOCKSSocket appears to be an optional component of ruby. That's why
resolv-replace only monkey-patches the class if it already exists.
As an illustration, 'net/ftp' defines the following method:
def open_socket(host, port)
if defined? SOCKSSocket and ENV["SOCKS_SERVER"]
#passive = true
return SOCKSSocket.open(host, port)
else
return TCPSocket.open(host, port)
end
end
Perhaps you could do something similar (i.e. create a SOCKS socket if
you have SOCKS enabled, otherwise create a boring old TCP socket).
And if you really need the proxy behaviour, a quick google search
revealed the following gem: http://socksify.rubyforge.org/ which
might be useful.
I'm trying to write a Ruby plasmoid for KDE. I need to use barely one rubygem. Whenever I write require 'dbus', it throw me and an error:
code/main.rb:6:in 'require': no such file to load -- dbus (LoadError)
code/main.rb:6:in '<module:TestApp>'
code/main.rb:5:in '<top (required)>'
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:177:in 'load'
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:177:in 'init'
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:201:in 'constraintsEvent': undefined method 'constraintsEvent' for nil:NilClass (NoMethodError)
/usr/share/apps/plasma_scriptengine_ruby/applet.rb:201:in 'constraintsEvent': undefined method 'constraintsEvent' for nil:NilClass (NoMethodError)
Actually, normal "ruby main.rb" works well (regarding on "require" part), but testing plasmoid with "plasmoidviewer" fails. Note, that regular gems from standart Ruby installation works well, i.e. require 'Qt4' or require 'yaml' loads perfectly. I'm using Ruby 1.9.2p180 under Linux.
09:40 PM - UPDATE: Richard Dale, one of the QtRuby developers, just fixed this issue a few minutes ago. Next release of KDE will have patched version of QtRuby.
require 'find'
require 'findUtils'
Find.find(PATH_WHERE_GEM_IS_INSTALLED) do |path|
if FileTest.directory?(path)
$: << File.expand_path(path)
if File.basename(path)[0] == ?. and File.basename(path) != '.'
Find.prune
else
next
end
else
end
end
and after that you can do
require 'dbus'
Have you tried this:
require 'rubygems'
?
I want to use gcd function of the Integer class. Using the example from Ruby Doc as a test it fails:
irb(main):001:0> 72.gcd 168
NoMethodError: undefined method `gcd' for 72:Fixnum
from (irb):1
I have the windows one click installer ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]. On other PCs with the same version of ruby this works correctly.
Any ideas?
Try
require 'rubygems'
72.gcd 168
require 'rational'
I am working with ruby 1.8.6 (2007-03-13 patchlevel 0) [x86_64-linux] and I get
undefined method `bytes' for #<String:0x2a95ec2268> (NoMethodError)
even though my code works on ruby 1.8.7. patchlevel 249
I saw somewhere that you need to add require "jcode" for a similar method not defined error with each_byte. I tried adding that but it still does not work. Any suggestions are very appreciated.
In Ruby 1.8.6, you can use my backports gem:
require 'backports/1.8.7/string/bytes'
Ta-da, you now have access to String#bytes.
You also have all the many other changes introduced in 1.8.7. And most of 1.9.1, and all of the upcoming 1.9.2, etc...
Ruby 1.8.6 doesn't have String#bytes. That's a 1.9 addition that was backported to 1.8.7.
You can roughly implement it yourself like this:
class String
require 'enumerator'
def bytes(&block)
return to_enum(:each_byte) unless block_given?
each_byte &block
end
end unless ''.respond_to?(:bytes)
[Note: I haven't checked whether this actually fulfills the contract of String#bytes 100%, but it is close enough for my use.]