天道酬勤,学无止境

ruby

"cannot load such file -- chef/rest" when "require 'chef/rest'"

Trying to write first program to access chef server via API. Following example program on https://docs.chef.io/api_chef_server.html#examples there supposed to be two lines of code to include chef::rest library. require 'chef' require 'chef/rest' but the code failed with error Traceback (most recent call last): 2: from ./rest.rb:4:in `<main>' 1: from /opt/chef/embedded/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require' /opt/chef/embedded/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- chef/rest (LoadError) Chef

2021-10-27 04:07:52    分类:问答    ruby   rest   chef-infra

meta/dynamic/generic programming in ruby

I am trying to make a generic method: class Foo attr_reader def add(object) item = @item.find { |item| item.(#the method is calling object.class) == object if item.is_a?(object.class)} end end I want to create a generic method which compares the element of an array depending the parameter class. Example: item.method1 == method1 if item.is_a?(method1.class) where method1 is the name of class. Also, I need a complete tutorial about metaprogramming and dynamic/generic programming.

2021-10-27 04:06:41    分类:问答    ruby   dynamic   metaprogramming

Sinatra Gem Won't Install

I am running Ubuntu 10.04 on a linode trying to get sinatra, passenger, nginx, and rvm to all play nice with Ruby 1.9.2. I'm running into an issue with gems though: ~/.rvm/gems/ruby-1.9.2-p290/gems$ ls returns daemon_controller-0.2.6 fastthread-1.0.7 passenger-3.0.9 rack-1.3.2 rake-0.9.2 and gem list returns *** LOCAL GEMS *** rack (1.3.2) rake (0.9.2) sinatra (1.2.6) tilt (1.3.3) What is odd to me is that they aren't the same. And sinatra is missing from the .rvm gems. When I go to my website I get the following: Error message: no such file to load -- sinatra with /home/noahc/.rvm/rubies/ruby

2021-10-27 03:46:00    分类:问答    ruby   nginx   gem   sinatra   passenger

How to use acts-as-commentable-with-threading in Rails

I am developing my first rails site (yup, i am a rails idiot). I'm writing a blog, and i got to the comments part. I installed acts-as-commentable-with-threading ( GitHub ), i made and ran the migration like the install instructions said. I have added acts_as_commentable to my Posts model and i have a Comments controller When i add @comment = Comment.build_from(params[:id],1, params[:body] ) I get the error. undefined method `build_from' for # Clearly i am doing something terribly wrong, and i don't really get the example. What should i be feeding to build_from? Can somebody explain this

2021-10-27 03:43:18    分类:问答    ruby-on-rails   ruby   acts-as-commentable

What does it mean to use the name of a class for string interpolation?

The following code is an extract from rubykoans about_classes.rb: class Dog7 def initialize(initial_name) @name = initial_name end def to_s @name end end I created an instance of Dog7: fido = Dog7.new("Fido") I understand the following: "My dog is " + fido.to_s # => "My dog is Fido" "My dog is #{fido.to_s}" # => "My dog is Fido" I do not understand why the following interpolation makes sense and gives the same result: "My dog is #{fido}" # => "My dog is Fido" fido is not a string.

2021-10-27 03:37:45    分类:问答    ruby   string-interpolation

How to validate header parameters with Sinatra?

I'm working on a simple API with Sinatra and I have a route like this one: get '/api/v1/invoice/:biller', :provides => [:json] do respond_with invoice( request ) end It works like a charm when I don't send any header params, but when I send: Accept Content-Type Then I got a 404 Not Found error and the classic Sinatra error 'Sinatra doesn't know this ditty'. How can I validate specific header params on Sinatra? Edit This is the actual header (Accept) with a curl example: curl -H "Accept: application/vnd.tpago.billpayment+json" -X GET "http://localhost:3540/api/v1/invoice/5947647" Thanks!

2021-10-27 03:37:05    分类:问答    ruby   http-headers   sinatra   http-status-code-404

Trying to raise ArgumentError rspec

I have an Rspec test that is not passing, and I don't understand why. All other tests are passing, except this one requiring an ArgumentError The test looks like this: describe "#evaluate" do it "raises error" do expect(rpn.evaluate('%')).to raise_error(ArgumentError) end end and my file is setup so it will raise an error (as an else statement) else raise ArgumentError.new end but rspec is telling me this Failure/Error: expect(rpn.evaluate('%')).to raise_error(ArgumentError) ArgumentError: ArgumentError

2021-10-27 03:36:19    分类:问答    ruby-on-rails   ruby   rspec

Rails Postgres - Jsonb 列查询(Rails Postgres - Jsonb Column Query)

问题 我有一个带有名为“available_quantity”的 jsonb 列的捆绑表。 这将有样本值 [{“面额”:“100”,“数量”:“20”},{“面额”:“1000”,“数量”:“19”}] 现在,我想查询数量小于 50 的所有记录。 我试过这个查询, Bundle.where("(available_quantity->>'quantity')::numeric < 50") 但是这个返回空的关系。 我怎样才能做到这一点? 架构 Table "offer_service.bundles" Column | Type | Collation | Nullable | Default -----------------------+-----------------------------+-----------+---- ------+-------------------------------------------------------- id | bigint | | not null | nextval('offer_service.bundles_id_seq'::regclass) project_id | bigint | | | item_type | character varying | | | item_id | bigint | | |

2021-10-27 03:07:54    分类:技术分享    ruby-on-rails   ruby   postgresql

attr_accessor 导致 Rspec 测试失败(attr_accessor causes Rspec tests to fail)

问题 我在我的 User 模型中添加了一个:username_or_email属性,如下所示: class User < ActiveRecord::Base #authlogic acts_as_authentic do |c| c.login_field = :username_or_email end #virtual field for allowing a user to login with their username or email attr_accessor :username_or_email attr_accessible :username, :email, :password, :password_confirmation, :username_or_email validates :username, :presence => true, :length => { :within => 3..20 }, :uniqueness => true, :format => { :with => /\A[a-z0-9][a-z0-9\-]+[a-z0-9]\z/ } validates :email, :presence => true, :format => { :with => /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i }

2021-10-27 03:06:20    分类:技术分享    ruby-on-rails   ruby   rspec   authlogic   attr-accessor

Creating a gem using Jruby and not Ruby

So I was successful in writing a jruby file and reading from a jar file. I also created a seperate Ruby gem, installed it and used it successfully. I would like to write a gem using Jruby (and not ruby). When I did try to do so and after installing the gem, it gave me the following error: `require': cannot load such file -- java (LoadError) Is there a specific way to write a gem using jruby that is different from the way to write a gem usig ruby? If so, please direct me to a useful tutorial that helps me create a gem using jruby thank you. EDIT: I added the following to the jruby_gem.gemspec

2021-10-27 02:54:11    分类:问答    java   ruby   gem   rubygems   jruby