Skip to content

Instantly share code, notes, and snippets.

Florian Dutey fdutey

Block or report user

Report or block fdutey

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:69495
#Je reprends l'exemple du post précédent
posts = Post.all
posts.collect{ |post| post.published? } #=> [true, true, false]
posts.collect(&:published?) #=> [true, true, false]
View gist:66911
h = { "a" => 1, "b" => 2, "c" => 3 }
#La syntaxe de Ruby permet ceci
a, b = ["a", "b"]
a #=> "a"
b #=> "b"
#N'avons nous pas dit qu'un Hash était un tableau de tableaux?
h.each{ |obj| puts obj.class } #=> Array \n Array
View gist:66904
h = { "a" => 1, "b" => 2, "c" => 3 }
h.collect #=> [["a", 1], ["b", 2], ["c", 3]]
View gist:66897
posts = Post.all #=> 3 posts, 2 publiés, un non
#indique si TOUS les éléments d'un enumerable obéissent à une condition passée sous forme de bloc
posts.all?{ |post| post.published? } #=> false
#indique si au moins 1 élement de la collection obéit à une condition passée sous forme de bloc
posts.any?{ |post| post.published? } #=> true
#renvoie un tableau contenant la valeur renvoyée par un bloc pour chaque élément de la collection
posts.collect{ |post| post.published? } #=> [true, true, false]
View gist:59056
#Questions mark (points d'interrogation)
Foo.all(:conditions => ['foo = ? AND bar = ?', foo, bar])
#Hash and keys (méthode alternative, un hash et des clefs)
Foo.all(:conditions => ['foo = :foo AND bar = :bar', params])
#params => { :foo => 'bar', :bar => 'foo', ... }
View gist:51785
module ActionView
module Helpers
class InstanceTag
def to_label_tag(text = nil, options = {})
options = options.stringify_keys
name_and_id = options.dup
add_default_name_and_id(name_and_id)
options.delete("index")
options["for"] ||= name_and_id["id"]
if text.blank?
View gist:50787
class Bookmark < ActiveRecord::Base
#Validations
validates_presence_of :label
validates_presence_of :url
def clean_url
url =~ /^http:\/\//i ? url : "http://#{url}"
end
end
You can’t perform that action at this time.