These notes restored in Academic Information Education Mersin from Notes of Mr. Sıtkı Bagdat .
Ruby is a programming languages
Ruby, readable high level a programming languages.
Started to developt at 1993 and published at 1995.
In 2013 for Ruby was selected brightest year,the reason is that in this year, two version was developt Ruby-2.0 and Ruby-2.1.
Ruby, entegration with Rails in 2004.Then we start to use Ruby on Rails in web development.
Ruby developt by Japon Yukihirato Matsumoto (Matz).
Ruby is completely object-oriented. (With a few exceptions)
Unlike another programming languages, ruby is interpreter languages. This means that, when we developt programm with ruby if can find a bug, Ruby running until bug’s line.
Included dynamic typing. This means that, we goes easily from a type to another type, not necessary to pre-definition.
Acoording to above irb, opened ruby terminal; exit exit to ruby terminal. In addition irb —simple -prompt this command is provide to seeming regular to terminal.Strikes the eye another notifications is that
_
this sign provide to take pre-functions and then make process with this functions.
1
system('clear')
with this command provide to clear your terminal.
12
irb(main):008:0> self=> main
this command show the writing on main class .
Also
1234567
irb(main):010:0> a="Ruby"=> "Ruby"irb(main):011:0> a.reverse
=> "ybuR"irb(main):012:0> "I loveirb(main):013:0" ruby"=> "I love\nruby
Seeming to above code .reverse method which entered code, provide to writing reverse.Also if when opened quotes then not closed, > instead “ can be this expression.
Logical expressions
False or nil, acceptable false.
Except false ve nil, everything acceptable true, like 0, [], …
Like another languages, when we make comparisons <,>, ==,! using this expressions.
We mention to everthing an object and derivation from a class in Ruby, strike to eye above, True, false , nil derivation a class.
Also we mention about nil, nil means that nothing like another languages null, undefined…etc.
In ruby, Not exist relevant to logical expression like Boolean or Bool classes. Also While we are using to logical expression, using like &&, ||, ! (and, or, not) expression.
Meeting to Methods
Defining to method start def, and ending with end.
123
defmethod_name...end
If method get a parameter like :
123
defmethod_name(param1)...end
123
defmethod_name(param1,param2)...end
Also Not required to using parenthesis like :
123
defmethod_nameparam1,param2,......end
1234
defcollectionnumber1,number2number1+number2endputs"Result of collection: ",collection(3,5)
The above codes writing in .rb file and then save it, then open your terminal , writing path of files in terminal
1
ruby project_name
write above command in terminal.
In Above code, defining collect method for collection two number and make collection process then show in terminal.
Method name, ending with ?, ! or = ,this characters have special meaning.
1
questioning?#return true or false
1
changed_value=...#changed value of anything value, assignment operator.
local variables starting with lover letter or ‘_’ , like :
12
local_variable="like this."plate=20
Global variables starting with ‘$’, like :
1
$global_variables
instance variables starting with ‘@’ and class variables starting with ‘@@’. like :
1
@instance_variables
1
@@class_variables
Also you can writing ruby, without opening irb in terminal. like:
12
ruby-e"puts 'Hello'"=>Hello
But the above method is not a preferred method commonly.
12345
defchanged(value)variables=valueendputs"Value of variables: #{changed(20)}"=>Valueofvariables:20
included another file in your file.
external_file.rb can be ruby file like this.Also can be we are currently working on calıstıgımız_dosya.rb.
If we want to embeded in external_file.rb files on currently_working_file.rb files, we should use load comand. like :
1
external_file.rb
12
currently_working_file.rb
load 'external_file.rb'
In Rails, we can generally use autoload command. autoload is loading automatically file.
Instead of load command, you can use require command but,when you use load, you can use many times in file, when you use require, require load file only once and generally require more preferred than it.
In addition, If we use ruby library, we should use, like
1
require'library_name'
You can see to use all path and method with ENV
Writing command about Gem :
1234567
gem -v #seeing the version of gemgem update --system #provide to update your systemgem list #you can see all gem listgem list -r #will see load to gemgem install pry #provide to writing colorful in terminal.gem install pry --no-ri # thanks to this we can install without documentation.gem uninstall package_name #use for uninstall gem
If you use package ,before need to install the package, using this command
1
geminstallpackage_name
when we install this gem, then we embeded in currently_working_file.rb like
1
require'package_name'
If I use many gem, Do I install gem one by one?
Of course No!, instead of this we use Gemfile, we write in file
12
source('http://ruby-gems.org')gem'package_name'
we write above command in Gemfile, we install automatically all gems.
Also If I create a ruby file, go to wanted directory and write below code, These code provided create ruby file in Desktop.
123
gem install bundler
cd Desktop #for desktop pathbundle gem package_name
Opened file, after we make to do, push to package to RubyGems then we see in this site.
Note :
-Also Thor is a package(gem) for ruby.
–ARGV is constant which hidden to arguments.
-In documentation section writing ri command, generally we use to search for method.
1
riClass#method
Input – Output processing
For output command write like
1234
puts# output with \n.printp#when we use '.inspect', instead of *.inspect* we use *p*.printf
For input command write like
1
gets#get input from user.
above instead of p using .inspect
12
puts"ruby".inspectputs3.inspect
.inspect method provide to take hid state of method. This means that if I using .inspect instead of this using p.
chomp provide if the last character can exist “\n” symbol, cleaning the last characters.
[A-Za-z0-9_]=\w#word character[Â-Za-z0-9_]=\W#not a word character
123
exp1=/.*\.$/#this means ending with '.'exp2=Regexp.new('^[a-z]+$')#this means that starting with a ending with z.exp3=%r{^[a-z]+$}#starting with a but not ending with z.