diff options
author | Vijay Dev <vijaydev.cse@gmail.com> | 2012-06-02 21:50:02 +0530 |
---|---|---|
committer | Vijay Dev <vijaydev.cse@gmail.com> | 2012-06-02 21:50:02 +0530 |
commit | 40d5303b4f1f32b1f97259052bd200a7e0792c7e (patch) | |
tree | 6879c06ca0b513636a8d882159a2da07ea701529 /guides/source | |
parent | a769fe9db16a792164363b49e4c7f07f6e412eaa (diff) | |
parent | ab2bf8148c8000a5185b11b6c1b11d94ae33053d (diff) | |
download | rails-40d5303b4f1f32b1f97259052bd200a7e0792c7e.tar.gz rails-40d5303b4f1f32b1f97259052bd200a7e0792c7e.tar.bz2 rails-40d5303b4f1f32b1f97259052bd200a7e0792c7e.zip |
Merge branch 'master' of github.com:lifo/docrails
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/active_support_core_extensions.textile | 26 | ||||
-rw-r--r-- | guides/source/routing.textile | 6 |
2 files changed, 23 insertions, 9 deletions
diff --git a/guides/source/active_support_core_extensions.textile b/guides/source/active_support_core_extensions.textile index 2addc50d68..011a702901 100644 --- a/guides/source/active_support_core_extensions.textile +++ b/guides/source/active_support_core_extensions.textile @@ -156,7 +156,7 @@ NOTE: Defined in +active_support/core_ext/object/duplicable.rb+. h4. +deep_dup+ -The +deep_dup+ method returns deep copy of given object. Normally, when you +dup+ an object that contains other objects, ruby does not +dup+ them. If you have array with a string, for example, it will look like this: +The +deep_dup+ method returns deep copy of a given object. Normally, when you +dup+ an object that contains other objects, ruby does not +dup+ them. If you have an array with a string, for example, it will look like this: <ruby> array = ['string'] @@ -164,7 +164,7 @@ duplicate = array.dup duplicate.push 'another-string' -# object was duplicated, element added only to duplicate +# object was duplicated, so element was added only to duplicate array #=> ['string'] duplicate #=> ['string', 'another-string'] @@ -177,7 +177,7 @@ duplicate #=> ['foo', 'another-string'] As you can see, after duplicating +Array+ instance, we got another object, therefore we can modify it and the original object will stay unchanged. This is not true for array's elements, however. Since +dup+ does not make deep copy, the string inside array is still the same object. -If you need a deep copy of an object, you should use +deep_dup+ in such situation: +If you need a deep copy of an object, you should use +deep_dup+. Here is an example: <ruby> array = ['string'] @@ -189,7 +189,7 @@ array #=> ['string'] duplicate #=> ['foo'] </ruby> -If object is not duplicable +deep_dup+ will just return this object: +If object is not duplicable, +deep_dup+ will just return this object: <ruby> number = 1 @@ -201,9 +201,21 @@ NOTE: Defined in +active_support/core_ext/object/deep_dup.rb+. h4. +try+ -Sometimes you want to call a method provided the receiver object is not +nil+, which is something you usually check first. +try+ is like +Object#send+ except that it returns +nil+ if sent to +nil+. +When you want to call a method on an object only if it is not +nil+, the simplest way to achieve it is with conditional statements, adding unnecessary clutter. The alternative is to use +try+. +try+ is like +Object#send+ except that it returns +nil+ if sent to +nil+. -For instance, in this code from +ActiveRecord::ConnectionAdapters::AbstractAdapter+ +@logger+ could be +nil+, but you save the check and write in an optimistic style: +Here is an example: + +<ruby> +# without try +unless @number.nil? + @number.next +end + +# with try +@number.try(:next) +</ruby> + +Another example is this code from +ActiveRecord::ConnectionAdapters::AbstractAdapter+ where +@logger+ could be +nil+. You can see that the code uses +try+ and avoids an unnecessary check. <ruby> def log_info(sql, name, ms) @@ -245,7 +257,7 @@ NOTE: Defined in +active_support/core_ext/kernel/singleton_class.rb+. h4. +acts_like?(duck)+ -The method +acts_like+ provides a way to check whether some class acts like some other class based on a simple convention: a class that provides the same interface as +String+ defines +The method +acts_like?+ provides a way to check whether some class acts like some other class based on a simple convention: a class that provides the same interface as +String+ defines <ruby> def acts_like_string? diff --git a/guides/source/routing.textile b/guides/source/routing.textile index 0773a96c67..7941e655bb 100644 --- a/guides/source/routing.textile +++ b/guides/source/routing.textile @@ -859,9 +859,11 @@ h3. Inspecting and Testing Routes Rails offers facilities for inspecting and testing your routes. -h4. Seeing Existing Routes with +rake+ +h4. Seeing Existing Routes -If you want a complete list of all of the available routes in your application, run +rake routes+ command. This will print all of your routes, in the same order that they appear in +routes.rb+. For each route, you'll see: +To get a complete list of the available routes in your application, visit +http://localhost:3000/rails/info/routes+ in your browser while your server is running in the *development* environment. You can also execute the +rake routes+ command in your terminal to produce the same output. + +Both methods will list all of your routes, in the same order that they appear in +routes.rb+. For each route, you'll see: * The route name (if any) * The HTTP verb used (if the route doesn't respond to all verbs) |