Someone told me that the reason why Ruby does not implement abstract classes (you have to implement it yourself using some intelligent tricks) is that it isn't strongly typed. Can anybody explain that to me?
mardi 4 août 2015
Error while installing Sass on Win8.1: "ERROR: While executing gem (Errno::EACCES) Permission denied @ rb_sysopen"
I installed Ruby via the official installer, installed the non x64 version just in case. Then I tried to install sass via the command line like on the http://ift.tt/1f0fI2t page, The following ensued:
sgarcia C:\Users\sgarcia
> gem install sass
Fetching: sass-3.4.16.gem (100%)
ERROR: While executing gem ... (Errno::EACCES)
Permission denied @ rb_sysopen - C:/Program Files (x86)/Ruby22/bin/sass.bat
Any ideas what could be causing this?
A better way to do conditional active record statements?
I'm trying to figure out a better way to have one query here. I want to be able to send something to last where statement a wildcard so I can select all vendors. Right now if i don't include that line it doesn't filter by vendor so I essentially get all the purchase requests.
Any thoughts of a cleaner way to do these sorts of queries?
if @vendor == "0" #checks for vendor
@purchase_requests = PurchaseRequest.includes(:purchase_order)
.where(:created_at => @date_start..@date_end)
.where(:total_cost => @cost_beginning..@cost_end)
else
@purchase_requests = PurchaseRequest.includes(:purchase_order)
.where(:created_at => @date_start..@date_end)
.where(:total_cost => @cost_beginning..@cost_end)
.where("purchaseorder.VendorRef_ListID = ?", @vendor)
end
Unexpected label while trying to iterate over a ruby hash
I am in the process of trying to iterate over a ruby hash. I'll have to admit that my knowledge of ruby is very poor and i am in the process of trying to correct that, and so please bear with me if this is a very elementary question.
I am wondering if the syntax of my hash is off. The reason why it is looking the way that it is is because it is part of a rakefile, and i need to incorporate multiple addresses in this. (which i've never done, i've always only had 1 address to worry about) I know the solution to this is to build the addresses in as a rakefile, and then loop over them.
clinic.build_address.each do | build_address |
{
(city: "Minneapolis"
state: "MN"
address1: "316 10th ave"
zip: "55414"),
(city: "Hudson"
state: "WI"
address1: "119 Belmont Street"
zip: "54016")
}
With what I have right now I am getting an unexpected label (it is not liking that I have 'city:minneapolis')
Would anybody be able to take a quick look at what I have with this?
How can I make files inaccessible to the user, but accessible to my program?
I am making a small game at the moment, and want it to be possible for users to save their characters, which will likely be stored in a text file. Is there any way to make this text file inaccessible to the user in the windows explorer or whathaveyou, so that save tampering is impossible (or at least harder), but also accessible to my program?
I don't know why I can't display an image when I create a product using CarrierWave? I also get an error now when I want to edit my product.
I'm using Rails -v 4.2.1 & ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]
controllers/products_controller.rb
class ProductsController < ApplicationController
before_filter :find_product, only: [:edit, :show, :update, :destroy]
def index
@products = Product.order("created_at DESC")
end
def show
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save!
flash[:success] = "Product created!"
redirect_to products_path
else
flash[:danger] = "Error creating product."
redirect_to new_product_path
end
end
def edit
end
def update
if @product.update_attributes(product_params)
flash[:success] = "Product Updated!"
redirect_to products_path
else
redirect_to "edit"
end
end
private
def product_params
params.require(:product).permit(:name, :description, :price, :images)
end
def find_product
@product = Product.find(params[:product_id])
end
end
models/product.rb
class Product < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def extension_white_list
%w(jpg jpeg gif png)
end
version :thumb do
process resize_to_fill: [200, 200]
end
version :small_thumb, from_version: :thumb do
process resize_to_fill: [20, 20]
end
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
Does ActiveSupport's Object#try method work with Thor::CoreExt::HashWithIndifferentAccess?
I'm working on a Middleman (Ruby) site. There are data objects which are instances of Thor::CoreExt::HashWithIndifferentAccess. However, it seems whenever I call try that I get nil.
I see that Thor::CoreExt::HashWithIndifferentAccess takes advantage of method_missing. Does this break how try works? Is anyone able to replicate this?
UPDATE: Here's a simple example. I'm using Middleman which provides access to data files.
# /data/people.yml
jane:
name: Jane Doe
age: 25
john:
name: John Doe
age: 25
Then in a template you have access to the data.people method which returns a Thor::CoreExt::HashWithIndifferentAccess instance:
data.people.inspect
#=> {"jane"=>{"name"=>"Jane Doe", "age"=>25},"john"=>{"name"=>"John Doe", "age"=>25}}
data.people.class
#=> Thor::CoreExt::HashWithIndifferentAccess
Now when I call try on anything I get nil:
data.people.jane
#=> {"name"=>"Jane Doe", "age"=>25}
data.people.try('jane')
#=> nil
data.people.try(:jane)
#=> nil
data.people.try(:something_that_does_not_exist)
#=> nil
data.people.jane.try(:name)
#=> nil