I got to know
Ruby relatively recently, thanks to the wonderful
Ruby on Rails framework . I do not consider myself an expert in Ruby, but I read PickAxe, I wrote several Ruby utilities for personal use, and I also develop on Rails in my free time.
Ruby is currently my primary language. I started with
TurboPascal at school, studied
C and
C ++ at the university, wrote the dissertation in
Java , and I also learned
PHP from scratch because I wanted to learn how to make websites quickly and easily. I think that writing code for me is a kind of entertainment, and then everything else. In the beginning, I had thoughts about which language to learn,
Python or Ruby. I chose the second one because I didn’t want the wrong indents to cause problems with my code.
There are a whole bunch of introductions, guides, articles and essays written by gurus on how to get the most out of Ruby. This article is nothing like this.
This is a more modest, incomplete list of reasons that may (or may not) encourage you to use Ruby, or at least play with it.
Caution: if you use another programming language for work or for other purposes - do not complain to me if you no longer want to use it. This is exactly what happened to me, but for me it is not so important. Ruby is a wonderful and elegant language and like so many similar things, it can capture your mind and break your soul.
')
In short, you are warned.
Why learn Ruby?
1.You get all the amenities (pleasure) without tricks.
Ruby borrowed the best from other programming languages, from
smalltalk to
Java , from
Perl to
Python . Here are the basic features that you could basically see in other languages:
- Exceptions. Believe it or not, exceptions are one of the most important things when developing programs of any kind. PHP4 programmers do not know anything about them and will tell you that you can just type (errors) onto the screen or use their own “superfood” class to handle errors. Fortunately for all of us, Ruby comes with try / catch (or even better begin / rescue ) blocks and a set of predefined, extensible Exceptions to handle errors correctly.
- Namespaces: Ruby modules make using namespaces easy, this should appeal to C ++ and Java enthusiasts.
- Embedded Regular Concepts: For all Perl connoisseurs, you can enclose something in // and it becomes a regular expression, ready for comparison (for this we use the = = operator).
- Operator Overloading: Ruby allows you to define operators such as +, -,> , etc. for any of your class.
- Packages: called " gems " (pebbles), they really justify their name, in addition - they work. Packages support dependencies, and can also be cross-platform as well as platform-dependent.
- Interactive console: can be used to test code interactively, like a python console
2. You love the little things.
Ruby is exquisite. Why? Because he does not try to make the code so concise that it would interfere with readability and understanding. There are some tips that can help you:
- , if unless . if , unless . , , , , :
order.new unless order.exists
- . , «?» , true false, , «!» , , , ..
- (alias) . exist exists, .
- attr_reader, attr_writer attr_accessor getter/setteer .
- : , , , «$», «@», «@@», ..
- , .
write File.open("/home/h3rald/test.txt")
File.open "/home/h3rald/test.txt"
, , .
3. .
? . «
Enter» .
Ruby Python, , . ,
C++, . , ?
4. , .
Java, , .
— «, 14 374346.678 — ?»
— «, , !»
Ruby , , . . , :
"YOU SHOULDN'T ALWAYS USE CAPITALS".lowcase # "you shouldn't always use capitals"
, :
# PHP Code
strtolower("YOU SHOULDN'T ALWAYS USE CAPITALS")
, .
5. .
«
return ». , : Ruby , , «
return» . .
6. .
, . : , - , . :
- , . — , .
- . — . - . , .
- . , , — .
Ruby , . .
, ?
Numeric, :
class Numeric
def feet
self*3.2808399
end
end
, , , , :
5.feet # 16.4041995
Ruby , . !
7. , , .
,
C++, , - — . , , «» «» «». ,
Ruby . - . , . ,
Ruby, , , .
8. XML — .
XML — , , , . , , () , — . :
regexp: !ruby/regexp /a-zA-Z/
number: 4.
string: a string
XML, ?
YAML, ( )
Ruby, , , .
Ruby
XML,
YAML ,
XML, (
Rails). , , :
{"string" => "a string", "number" => 4.7, "regexp" => /a-zA-Z/}.to_yaml
to_yaml,
Object, ,
Ruby.
YAML , . , ?
9. — , .
Ruby Lisp Perl Proc.
Proc — « , . , .». :
def gen_times(factor)
return Proc.new {|n| n*factor }
end
times3 = gen_times(3)
times5 = gen_times(5)
times3.call(12) #=> 36
times5.call(5) #=> 25
times3.call(times5.call(4)) #=> 60
lambda Proc.new, . ,
Perl Python (
Lisp).
PHP, .
Ruby «
Proc», , ,
each,
Array:
[1, 2, 4, 6, 8].each {|c| puts c*2} #=> , 2
,
do… end, .
Ruby .
10. Rails.
, …
Rails -. ,
PHP,
Rails Ruby, , .
.
Ruby, , . , , , , — .
— , , (, ) . .