About Me

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

Thursday 4 February 2016

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

No comments:

Post a Comment