236 shaares
110 liens privés
110 liens privés
35 résultats
taggé
ruby
example : where is #render ?
def index
p method(:render).source_location
render params[:id]
end
then curl http://localhost:3000/users/xxxx
(or wherever your index is) and check your server output :
Processing by UsersController#show as */*
Parameters: {"id"=>"xxxx"}
["/Users/aaron/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb", 40]
Completed 500 Internal Server Error in 35ms (ActiveRecord: 0.0ms)
from : https://tenderlovemaking.com/2016/02/05/i-am-a-puts-debuggerer.html
# in model team
has_and_belongs_to_many :users
accepts_nested_attributes_for :users, allow_destroy: true, reject_if: :all_blank
accepts_nested_attributes_for :user, allow_destroy: true, reject_if: :all_blank
# Controller
params.require(:team).permit(:name, :users_attributes => [ :user_attributes => [:email] ] )
# In a helper, to manage non persistent manipulations on team.users
self.association(:users).add_to_target(user)
Modify your object, then return the object.
def update_params(params)
params.tap {|p| p[:foo] = 'bar' }
end
# => returns the params object
Array.new(8) { 'O' }
#=> ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
# starts_with_consonant?(s)
# takes a string and returns true if it starts with a consonant and false otherwise.
# (For our purposes, a consonant is any letter other than A, E, I, O, U.)
# NOTE: be sure it works for both upper and lower case and for nonletters!
def starts_with_consonant?(s)
raise "#{__method__} : argument must be a String.\n #{s} is #{s.class}." unless s.is_a?(String)
!((s =~ /\A[A-Z&&[^AEIOU]]/i) == nil)
end
describe "Testing sum():" do
it "returns the sum of all the elements" do
array = [1, 3, 6, 1, 2]
sum(array).should eq(13)
end
it "returns 0 for an empty array" do
array = []
sum(array).should eq(0)
end
end
describe "Testing max_2_sum():" do
it "returns the sum of the two highest elements" do
array = [1, 3, 6, 1, 2]
max_2_sum(array).should eq(9)
end
it "returns the sum of the two highest elements when they are equal" do
array = [1, 6, 6, 1, 2]
max_2_sum(array).should eq(12)
end
it "returns 0 for an empty array" do
array = []
max_2_sum(array).should eq(0)
end
it "returns the first element if the array contains one element" do
array = [42]
max_2_sum(array).should eq(42)
end
end
describe "Testing sum_to_n?():" do
it "returns true for ([1, 3, 6, 1, 2], 4)" do
array = [1, 3, 6, 1, 2]
sum_to_n?(array, 4).should eq(true)
end
it "returns false for ([12, 35, 64, 12, 25], 11)" do
array = [12, 35, 64, 12, 25]
sum_to_n?(array, 11).should eq(false)
end
it "returns false when the sum is greater that the 2 largest elements" do
array = [12, 35, 64, 12, 25]
nb = max_2_sum(array) + 1
sum_to_n?(array, nb).should eq(false)
end
it "returns true for ([1,2,5,5], 10)" do
array = [1,2,5,5]
sum_to_n?(array, 10).should eq(true)
end
end
=begin
n a game we called that "hoop", n player numbered from 1 to n are playing. The first player says 1, the second player says 2 and so on. After everyone says a number, it's the first player's turn again. There's an additional rule that make the game more interesting: if the current number the person should say is divisible by 3 or 7, the person should say 0 instead.
You're given the number of players n, the number of turns m the game was played, and what was said in each turn. Your task is to find the players who made mistakes.
the players should be returned in the order they made mistakes;
if the player made several mistakes, all of them should be returned.
Example
For n = 3, m = 8 and turns = [1, 2, 3, 4, 4, 0, 0, 0],
the output should be
hoop(n, m, turns) = [3, 2, 2].
During the first round, players 1 and 2 did everything right, but the third player forgot to say 0 (3 is divisible by 3, so 0 should be said instead).
During the second round, player 2 said 4 instead of 5, which was a mistake.
Finally, during the last round the second player said 0 instead of 8, which was a mistake.
Input/Output
[time limit] 4000ms (rb)
[input] integer n
The number of players.
Constraints:
1 ≤ n ≤ 10.
[input] integer m
The number of turns.
Constraints:
1 ≤ m ≤ 50.
[input] array.integer turns
Array of non-negative numbers, where the ith number is the number said in the ith turn.
Constraints:
turns.length = m.
[output] array.integer
Array of players who made mistakes in the order of turns the mistakes were made in.
=end
# returns the expected output
def hoop(n, m, turns)
out=[]
gamer= 1
turns.each_with_index do |answer, index |
out << gamer unless answer == good_answer(index+1)
(gamer == n) ? gamer = 1 : gamer += 1
end
out
end
# returns the expected answer on the "number" turn
# @param [integer] number
def good_answer (number)
(number%3 == 0 || number%7 == 0) ? 0 : number
end
class String
def to_proc
eval "Proc.new { |*args| args.first#{self} }"
end
end
You can, but should you ?
require 'tty'
require 'tty-prompt'
prompt = TTY::Prompt.new
answer = prompt.select("my question") do |item|
item.choice 'One', 'Pika'
item.choice "Two", 'Boo'
end
puts "My answer was : #{answer}"
(with gem TTY)
ItemMenu = Struct.new(:lvl, :description, :command_type, :data) do
def action
puts "data = " + data.inspect
end
def save_to filepath
File.open( filepath, 'w+' ) do |f|
Marshal.dump(self,f)
end
end
def load_from filepath
File.open(filepath, 'r') do |f|
tmp = Marshal.load(f)
puts "tmp : " + tmp.inspect
self.lvl = tmp.lvl
self.description = tmp.description
self.command_type = tmp.command_type
self.data = tmp.data
end
end
end
l_1 = ItemMenu.new( '0.1', 'First Item','shell', 'ls' )
puts ' == CREATION == '
puts l_1.lvl
puts l_1.description
puts l_1.command_type
puts l_1.data
puts l_1.action
puts ' == SAVING == '
l_1.save_to '/home/username/work/menu/data/l_1.txt'
puts '== LOADING == '
l_2 = ItemMenu.new
l_2.load_from '/home/username/work/menu/data/l_1.txt'
puts l_2.inspect
"1".next # => '2'
"1a".next # => '1b'
"1z".next # => '2a'
"1aa".next # => '1ab'
Besoin de faire un client/serveur rapidement sous ruby.
DRb à la rescousse
Comme son nom l'indique, ce schéma explique comment choisir la manière dont on veut lancer un process sous ruby.
Team has and belongs to many users
Users has and belongs to many teams.
Users.all.where('id not in (?)', @team.users.map(&:id)
Returns the list of all users not related to the @team
unless user.is_a?(User)
raise argumentError.new("Only users are allowed")
end