how do i transform
www.bestbuy.com/site/Electronics\Audio\abcat0200000.c=3fid=3dabcat0200000
into its original format ?
www.bestbuy.com/site/Electronics/Audio/abcat0200000.c?id=abcat0200000
urldecode ?
Hi,
I have an array of hashes like following
[
{ :foo => 'foo', :bar => 2 },
{ :foo => 'foo', :bar => 3 },
{ :foo => 'foo', :bar => 5 },
]
I am trying to sort above array in descending order according to the value of :bar in each hash.
I am using sort_by like following to sort above array.
a.sort_by { |h| h[:bar] }
However above sorts the array in ascending order. How do I make it sort in descending order?
One solution was to do following:
a.sort_by { |h| -h[:bar] }
But that negative sign does not seem appropriate. Any views?
I'm trying to write a DSL that allows me to do
Policy.name do
author "Foo"
reviewed_by "Bar"
end
The following code can almost process it:
class Policy
include Singleton
def self.method_missing(name,&block)
puts name
puts "#{yield}"
end
def self.author(name)
puts name
end
def self.reviewed_by(name)
puts name
end
end
Defining my method as class methods (self.method_name) i can access it using the following syntax:
Policy.name do
Policy.author "Foo"
Policy.reviewed_by "Bar"
end
If i remove the "self" from the method names, and try to use my desired syntax, then i receive an error "Method not Found" in the Main so it could not find my function until the module Kernel. Its ok, i understand the error. But how can i fix it? How can i fix my class to make it work with my desired syntax that?
Given a class like this:
class B
class << self
attr_accessor :var
end
end
Suppose I can't modify the original source code of class B. How might I go about removing the setter on the class variable var? I've tried using something like B.send("unset_method", "var="), but that doesn't work (nor does remove_method, or overwriting that method with a var= method that doesn't do anything). Any ideas?
From a table element, I would like to select all rows that have the class even or the class odd.
I tried the jQuery syntax:
report.css("table.data tr[class~=odd even]").each{|line| parse_line_item(line)}
but it threw an error, any help is appreciated, thanks.
Hello
I have a form in witch users can add their working hours view them and edit them (All in one page). When adding working hours the user must select a project from a dropdown list. In case the action is adding a new hour record the dropdown field should remain empty (not selected) in case the action is edit the dropdown field should be selected with the appropriate value.
In order to overcome this challenge I wrote the following code
<% if params[:id].blank?%>
<select name="hour[project_id]" id="hour_project_id">
<option value="nil">Select Project</option>
<% @projects.each do|project|%>
<option value="<%=project.id %>"><%=project.name%></option>
<% end%>
</select>
<% else %>
<%= select('hour','project_id', @projects.collect{|project|[project.name,project.id]},{:prompt => 'Select Project'})%>
<% end %>
So in case of save action I did the dropdown list only with html, and in case of edit action I did it with the collect method. It works fine until I tried to code the errors. The problem is that when I use the error method: validates_presence_of :project_id it didn't recognize it in the html form of the dropdown list and don’t display the error message (its working only for the dropdown with the collect method).
I will deeply appreciate your instructions and help in this matter
i discovered this in a dark place one day...what the hell is it supposed to do ??
def spliceElement(newelement,dickwad)
dox = Nokogiri::HTML(newelement)
fuck = dox.xpath("//text()").to_a
fuck.each do |shit|
if shit.text.include? ": "
dickwad << shit.text.split(': ')[1].strip + "|"
else
if shit.text =~ /\s{1,}/ or shit.text =~ /\n{1,}/
puts "fuck"
else
dickwad << shit.text.squeeze(" ").strip + "|"
end
end
end
dickwad << "\n"
end
def extract(newdoc, newarray)
doc = Nokogiri::HTML(newdoc)
collection = Array.new
newarray.each do |dong|
newb = doc.xpath(dong).to_a
#puts doc.xpath(dong).text
collection << newb
end
dickwad = "";
if collection.length > 1
(0...collection.first.length).each do |i|
(0...collection.length).each do |j|
somefield = collection[j][i].to_s.gsub(/\s{2,}/,' ')
spliceElement(somefield, dickwad)
end
newrow = dickwad.chop + "\n"
return newrow.to_s
end
else
collection.first.each do |shit|
somefield = shit.to_s.gsub(/\s{2,}/,' ')
spliceElement(somefield, dickwad)
puts somefield + "\n\n"
#newrow = dickwad.chop + "\n"
#puts newrow
#return newrow.to_s
sleep 1
end
end
I want to add elements to my Hash lists, which can have more than one value. Here is my code. I don't know how I can solve it!
class dictionary
def initialize(publisher)
@publisher=publisher
@list=Hash.new()
end
def []=(key,value)
@list << key unless @list.has_key?(key)
@list[key] = value
end
end
dic = Dictionary.new
dic["tall"] = ["long", "word-2", "word-3"]
p dic
Many thanks in advance.
regards,
koko
Is it possible to get the parameter names of a method ?
Example with:
def method_called(arg1, arg2)
puts my_method.inspect
end
I would like to know what method (my_method) should I call to get the following output:
["arg1", "arg2"]
i am wrking on tag clouds with wp columns ( java script) but it s not wrking .It contains files like tagcloud.swf and swfobject.js . I have added this file in public folder and added html.erb file in the view but its not generating the code and showing any thing on the page
the code is
<%= javascript_include_tag 'swfobject.js' %>
<style type="text/css">
body { background-color: #eee; padding: 20px; }
</style>
<% tags = (current_user.all_tags) %
<% all_tags = tags.flatten.uniq%
<script type="text/javascript">
var so = new SWFObject("/tagcloud.swf", "tagcloud", "600", "400", "7", "#ffffff");
// uncomment next line to enable transparency
//so.addParam("wmode", "transparent");
so.addVariable("tcolor", "0x333333");
so.addVariable("mode", "tags");
so.addVariable("distr", "true");
so.addVariable("tspeed", "100");
so.addVariable("tagcloud", "<tags>
<% for t in all_tags %>
<a href='#' style='22' color='0xff0000' hicolor='0x00cc00'><%=t.to_s%></a>
<%#= link_to t.to_s ,tag_index_path(t) %>
<% end %></tags>");
so.write("flashcontent");
</script></body>
here is my controller:
class AdminController < ApplicationController
before_filter :require_user
authorize_resource :class => false
def index
end
def users_list
end
end
here is my Ability class:
class Ability
include CanCan::Ability
def initialize(user)
if user.admin?
can :manage, :all
else
can :read, :all
end
end
end
when trying to access "/admin/users_list" (with an admin user or without) i get the following error:
uninitialized constant CanCan::Rule::Mongoid
any thoughts?
I'm trying to give a file as input, have it changed within the program, and save the result to a file that is output. But the output file is the same as the input file. :/ Total n00b question, but what am I doing wrong?:
puts "Reading Celsius temperature value from data file..."
num = File.read("temperature.dat")
celsius = num.to_i
farenheit = (celsius * 9/5) + 32
puts "Saving result to output file 'faren_temp.out'"
fh = File.new("faren_temp.out", "w")
fh.puts farenheit
fh.close
The weird thing is that my app was working perfectly on Sat, and when I check it out on Monday (after doing nothing to it) I kept getting this problem:
This behaviour is only happening on my production server. When I try to login or create a new user or do something that interacts with a form I am getting an unknown action error. A simple retrieval of rows does not throw this error however.
I don't have all CRUD operations in most of my controllers because it's not necessary - but Rails always looks for the one that doesn't exist - it seams so anyway.
If I make a mistake in the form that would normally throw a validation message to the user it will throw this error too, does that mean it has something to do with the model too (I'm not too Rails experienced and didn't know if that would be the case or not)?
This is a general error I am getting - I have super_exception_notifier gem installed, so that's what all the extra params are.
Processing SessionsController#new (for OMITTED at 2010-04-12 09:11:12) [GET]
Rendering template within layouts/application
Rendering sessions/new
Completed in 3ms (View: 2, DB: 0) | 200 OK [http://OMITTED.com/session/new]
Processing SessionsController#show (for OMITTED at 2010-04-12 09:11:14) [GET]
ActionController::UnknownAction (No action responded to show. Actions: create, destroy, error_class_status_codes, error_class_status_codes=, error_layout, error_layout=, exception_notifiable_notification_level, exception_notifiable_notification_level=, exception_notifiable_silent_exceptions, exception_notifiable_silent_exceptions=, exception_notifiable_verbose, exception_notifiable_verbose=, http_status_codes, http_status_codes=, and new):
dragonfly (0.5.3) lib/dragonfly/middleware.rb:13:in `call'
passenger (2.2.9) lib/phusion_passenger/rack/request_handler.rb:92:in `process_request'
passenger (2.2.9) lib/phusion_passenger/abstract_request_handler.rb:207:in `main_loop'
passenger (2.2.9) lib/phusion_passenger/railz/application_spawner.rb:400:in `start_request_handler'
passenger (2.2.9) lib/phusion_passenger/railz/application_spawner.rb:351:in `handle_spawn_application'
passenger (2.2.9) lib/phusion_passenger/utils.rb:184:in `safe_fork'
passenger (2.2.9) lib/phusion_passenger/railz/application_spawner.rb:349:in `handle_spawn_application'
passenger (2.2.9) lib/phusion_passenger/abstract_server.rb:352:in `__send__'
passenger (2.2.9) lib/phusion_passenger/abstract_server.rb:352:in `main_loop'
passenger (2.2.9) lib/phusion_passenger/abstract_server.rb:196:in `start_synchronously'
passenger (2.2.9) lib/phusion_passenger/abstract_server.rb:163:in `start'
passenger (2.2.9) lib/phusion_passenger/railz/application_spawner.rb:209:in `start'
passenger (2.2.9) lib/phusion_passenger/spawn_manager.rb:262:in `spawn_rails_application'
passenger (2.2.9) lib/phusion_passenger/abstract_server_collection.rb:126:in `lookup_or_add'
passenger (2.2.9) lib/phusion_passenger/spawn_manager.rb:256:in `spawn_rails_application'
passenger (2.2.9) lib/phusion_passenger/abstract_server_collection.rb:80:in `synchronize'
passenger (2.2.9) lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
passenger (2.2.9) lib/phusion_passenger/spawn_manager.rb:255:in `spawn_rails_application'
passenger (2.2.9) lib/phusion_passenger/spawn_manager.rb:154:in `spawn_application'
passenger (2.2.9) lib/phusion_passenger/spawn_manager.rb:287:in `handle_spawn_application'
passenger (2.2.9) lib/phusion_passenger/abstract_server.rb:352:in `__send__'
passenger (2.2.9) lib/phusion_passenger/abstract_server.rb:352:in `main_loop'
passenger (2.2.9) lib/phusion_passenger/abstract_server.rb:196:in `start_synchronously'
This is what one of my forms looks like (nothing special)
<% form_tag session_path do -%>
<p><%= label_tag 'Username' %><br />
<%= text_field_tag 'login', @login %></p>
<p><%= label_tag 'password' %><br/>
<%= password_field_tag 'password', nil %></p>
<p><%= label_tag 'remember_me', 'Remember me' %>
<%= check_box_tag 'remember_me', '1', @remember_me %></p>
<p><%= submit_tag 'Log in' %></p>
<% end -%>
It looks like dragonfly is the culprit doesn't it, here's the section from the gem files it says is being naughty:
module Dragonfly
class Middleware
def initialize(app, dragonfly_app_name)
@app = app
@dragonfly_app_name = dragonfly_app_name
end
def call(env)
response = endpoint.call(env)
if response[0] == 404
13 -->> @app.call(env)
else
response
end
end
I don't know what goes on behind the scenes here so I probably haven't been looking in the right place to fix this issue. Like I said it only throws this in a production environment, which guess is what the 'env' variable is referencing.
Thank you for your time! I've spent nearly my whole day trying to figure this out! :(
So, I'd like to be able to make a call
x = MyClass.new('good morning', 'good afternoon', 'good evening', 'good night',
['hello', 'goodbye'])
that would add methods to the class whose values are the values of the arguments. So now:
p x.methods #> [m_greeting, a_greeting, e_greeting, n_greeting,
r_greeting, ...]
And
p x.m_greeting #> "good morning"
p x.r_greeting #> ['hello', 'goodbye']
I realize that this is sort of what instance variables are to do (and that if I wanted them immutable I could make them frozen constants) but, for reasons beyond my control, I need to make methods instead.
Thanks!
I'm trying this:
{:id => 5, :foos => [1,2,3]}.each {|k,v| v.to_s}
But that's returning this:
{:id=>5, :foos=>[1, 2, 3]}
I'd like to see this:
{:id=>"5", :foos=>"[1, 2, 3]"}
I've also tried variations of Hash#collect and Hash#map. Any ideas?
how does this work?
in irb:
>> class A
>> b = [1, 2,3]
>> end
=> [1, 2, 3]
Is b an instance variable? class variable? how would I access b from
outside the class? Is it used for meta-programming?
If i had a list of balls each of which has a color property. how can i cleanly get the list of balls with the most frequent color.
[m1,m2,m3,m4]
say,
m1.color = blue
m2.color = blue
m3.color = red
m4.color = blue
[m1,m2,m4] is the list of balls with the most frequent color
My Approach is to do:
[m1,m2,m3,m4].group_by{|ball| ball.color}.each do |samecolor|
my_items = samecolor.count
end
where count is defined as
class Array
def count
k =Hash.new(0)
self.each{|x|k[x]+=1}
k
end
end
my_items will be a hash of frequencies foreach same color group. My implementation could be buggy and i feel there must be a better and more smarter way.
any ideas please?
So here's the output of inspect on a class:
<Recurly::BillingInfo::CreditCard:0x1036a8a98 @prefix_options={}, @attributes={"month"=>1, "last_four"=>"1", "type"=>"bogus", "year"=>2010}>
I'm trying to get the type attribute but seems that might be some sort of reserved word?
Here's the full rundown of what I'm trying to do
@charges = Recurly::BillingInfo.find('123')
@charges.credit_card.type
So, how can I get type from that?
module MyModule
def my_method; 'hello'; end
end
class MyClass
class << self
include MyModule
end
end
MyClass.my_method # => "hello
I'm unsure why "include MyModule" needs to be in the singleton class in order to be called using just MyClass.
Why can't I go:
X = MyClass.new
X.my_method
Hi all,
I am looking to execute a password change over Net-ssh and this code seems to hang:
Net::SSH.start(server_ip, "user", :verbose => :debug ) do |session|
session.process.popen3("ls") do |input, output, error|
["old_pass","test", "test"].each do |x|
input.puts x
end
end
end
I know the connection works because using a simple exec I can get the output from ls on the remote server, but this hangs.
Any ideas?
The last message from debug is that the public key succeeded.
With vim how do I to turn this:
t.string :crypted_password :null => false
t.string :password_salt, :null => false
into this:
t.string :crypted_password, :null => false
t.string :password_salt, :null => false
without manually adding the spaces to each line?
I have a 'validate_on_create' statement in one of my controllers that I would like all of my seed data to skip. What are some solutions so that the create statement in my seeds file skips this validation. My current solution is commenting out the validation each time I run rake db:seed. Anything a little more clever?