This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
h = { "a" => 1, "b" => 2, "c" => 3 } | |
h.collect #=> [["a", 1], ["b", 2], ["c", 3]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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', ... } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bookmark < ActiveRecord::Base | |
#Validations | |
validates_presence_of :label | |
validates_presence_of :url | |
def clean_url | |
url =~ /^http:\/\//i ? url : "http://#{url}" | |
end | |
end |
NewerOlder