About Me

My photo
जिंदगी की परीक्षा में कोई नम्बर नहीं मिलते है लोग आपको दिल से याद करे तो समझ लेना आप पास हो गए....

Thursday 4 February 2016

Rails 4 way notes 1

1. Rails 4, generating a new application will result in the creation of binstubs for Rails executables, located
in the bin folder. A binstub is a script containing an executable that runs in the context of the bundle. This
means one does not have to prefix bundle exec each time a Rails specific executable is invoked. Binstubs are
also first class citizens in Rails 4, and should be added into your version control system like any other source
code file.
By default, the following stubs are available on every new Rails 4 project:
• bin/bundle
• bin/rails
• bin/rake

To add a binstub of a commonly used executable in your bundle, invoke bundle binstubs some-gem-name .
To illustrate, consider the following example:
$ bundle binstubs guard
which creates a binstub for guard in the bin folder.

2.Secret Token
Certain types of hacking involve modifying the contents of cookies without the server knowing about it.
By digitally signing all cookies sent to the browser, Rails can detect whether they were tampered with. The
secret_token.rb initializer contains the secret key base, randomly generated along with your app, which is
used to sign cookies.

p15
ref: rails 4 way

Ruby Learning Notes 1

.On Unix systems, you can use the “shebang” notation as the first line of the program file:#!/usr/bin/ruby
puts "Hello, Ruby Programmer"
puts "It is now #{Time.now}"

If you make this source file executable (using, for instance, chmod +x myprog.rb ), Unix lets you run the file as a program:

$ ./myprog.rb
Hello, Ruby Programmer
It is now 2013-05-27 12:30:36 -0500


If your system supports it, you can avoid hard-coding the path to Ruby in the “shebang” line by using
#!/usr/bin/env ruby , which will search your path for ruby and then execute it.




2.The ri tool is a local, command-line viewer for ruby documentation. Most Ruby distributions now also install the resources used by the ri program.
To find the documentation for a class, type ri ClassName. For example, the following is the summary information for the GC class. (To get a list of classes with ri documentation, type
ri with no arguments.)
$ ri GC

If you installed Ruby using rvm, there’s one additional step to get ri documentation available. At a
prompt, enter rvm docs generate .

3.Class A class is a combination of state and methods that use that state.

Naming Convention:
Local variables, method parameters, and method names should all start with a lowercase letter or an underscore. Global variables are prefixed with a dollar
sign ($), and instance variables begin with an “at” sign (@). Class variables start with two “at” signs (@@). Finally, class names, module names, and constants must start with an uppercase letter.

Hash: a hash by default returns nil when indexed by a key it doesn’t
contain.specifying a default value(which is by default nil) when you create a new, empty hash.
histogram = Hash.new(0)   # This overrides default value from nil to zero
histogram['ruby'] # => 0

Symbol: Symbols are simply constant names that you don’t have to  predeclare and that are guaranteed to be unique. A symbol literal starts with a colon and is
normally followed by some kind of name

Ruby statement modifiers are a useful shortcut if the body of an if or while statement is just a single expression.

if radiation > 3000
  puts "Danger, Will Robinson"
end

Here it is again, rewritten using a statement modifier:
puts "Danger, Will Robinson" if radiation > 3000


Regular expression: create a regular expression by writing a pattern between
slash characters (/pattern/)

We can put all this together to produce some useful regular expressions:
/\d\d:\d\d:\d\d/   # a time such as 12:34:56
/Perl.*Python/  #Perl, zero or more other chars, then Python
/Perl Python/    #Perl, a space, and Python
/Perl *Python/  #Perl, zero or more spaces, and Python
/Perl +Python/  #Perl, one or more spaces, and Python
/Perl\s+Python/  #Perl, whitespace characters, then Python
/Ruby (Perl|Python)/   #Ruby, a space, and either Perl or Python

The match operator =~ can be used to match a string against a regular expression. If the pattern is found in the string,=~ returns its starting position; otherwise, it returns nil . This means you can use regular expressions as the condition in if and while statements.

code blocks: which are chunks of code you can associate with method invocations, almost as if they were parameters.Code blocks are just chunks of code between braces or between do and end . This is a code block:
{ puts "Hello" }

a Ruby standard and use braces for single-line blocks and do / end for multiline blocks.

ARGV :When you run a Ruby program from the command line, you can pass in arguments. the array ARGV contains each of the arguments passed to the running program. Create a file called cmd_line.rb that contains the following:
puts "You gave #{ARGV.size} arguments"
p ARGV

When we run it with arguments, we can see that they get passed in:
$ ruby cmd_line.rb ant bee cat dog
You gave 4 arguments
["ant", "bee", "cat", "dog"]


The p method prints out an internal representation of an object.
b1 = BookInStock.new("isbn1", 3)
p b1

produces:
#<BookInStock:0x007fac4910f3e0 @isbn="isbn1", @price=3.0>

To read the instance variable of object out side the world we wrote attribute accessor method , but in ruby we need not to write for each instance variable since ruby provides shortcut for that "attr_reader", as example

class BookInStock

  attr_reader :isbn,:price
 def initialize(isbn,price)
   @isbn = isbn
   @price = price
 end
end

book = BookInStock.new("isbn1", 12.34)
puts "ISBN = #{book.isbn}"
puts "Price = #{book.price}"





~                             
Ruby provides a shortcut for creating these simple attribute-setting methods. If you want a write-only accessor, you can use the form attr_writer , but that’s fairly rare. You’re far more likely to want both a reader and a writer for a given attribute, so you’ll use the handy-dandy attr_accessor method:

pg36?(floating point)


References:

Programming Ruby: The Pragmatic Programmer's Guide