Movatterモバイル変換


[0]ホーム

URL:


Ruby

A Programmer's Best Friend

HomeDownloadsDocumentationLibrariesCommunityNewsSecurityAbout RubyMenu

Ruby From Other Languages

When you first look at some Ruby code, it will likely remind you ofother programming languages you’ve used. This is on purpose. Much of thesyntax is familiar to users of Perl, Python, and Java (among otherlanguages), so if you’ve used those, learning Ruby will be a piece ofcake.

This document contains two major sections. The first attempts to be arapid-fire summary of what you can expect to see when going fromlanguageX to Ruby. The second section tackles the major languagefeatures and how they might compare to what you’re already familiarwith.

What to Expect:Language X to Ruby

Important Language Features And Some Gotchas

Here are some pointers and hints on major Ruby features you’ll see whilelearning Ruby.

Iteration

Two Ruby features that are a bit unlike what you may have seen before,and which take some getting used to, are “blocks” and iterators. Insteadof looping over an index (like with C, C++, or pre-1.5 Java), or loopingover a list (like Perl’sfor (@a) {...}, or Python’sfor i in aList: ...), with Ruby you’ll very often instead see

some_list.eachdo|this_item|# We're inside the block.# deal with this_item.end

For more info oneach (and its friendscollect,find,inject,sort, etc.), seeri Enumerable (and thenri Enumerable#some_method).

Everything has a value

There’s no difference between an expression and a statement. Everythinghas a value, even if that value isnil. This is possible:

x=10y=11z=ifx<ytrueelsefalseendz# => true

Symbols are not lightweight Strings

Many Ruby newbies struggle with understanding what Symbols are, and whatthey can be used for.

Symbols can best be described as identities. A symbol is all aboutwho it is, notwhat it is. Fire upirb and see the difference:

irb(main):001:0>:george.object_id==:george.object_id=>trueirb(main):002:0>"george".object_id=="george".object_id=>falseirb(main):003:0>

Theobject_id methods returns the identity of an Object. If twoobjects have the sameobject_id, they are the same (point to the sameObject in memory).

As you can see, once you have used a Symbol once, any Symbol with thesame characters references the same Object in memory. For any given twoSymbols that represent the same characters, theobject_ids match.

Now take a look at the String (“george”). Theobject_ids don’t match.That means they’re referencing two different objects in memory. Wheneveryou use a new String, Ruby allocates memory for it.

If you’re in doubt whether to use a Symbol or a String, consider what’smore important: the identity of an object (i.e. a Hash key), or thecontents (in the example above, “george”).

Everything is an Object

“Everything is an object” isn’t just hyperbole. Even classes andintegers are objects, and you can do the same things with them as withany other object:

# This is the same as# class MyClass#   attr_accessor :instance_var# endMyClass=Class.newdoattr_accessor:instance_varend

Variable Constants

Constants are not really constant. If you modify an already initializedconstant, it will trigger a warning, but not halt your program. Thatisn’t to say youshould redefine constants, though.

Naming conventions

Ruby enforces some naming conventions. If an identifier starts with acapital letter, it is a constant. If it starts with a dollar sign ($),it is a global variable. If it starts with@, it is an instancevariable. If it starts with@@, it is a class variable.

Method names, however, are allowed to start with capital letters. Thiscan lead to confusion, as the example below shows:

Constant=10defConstant11end

NowConstant is 10, butConstant() is 11.

Keyword arguments

Like in Python, since Ruby 2.0 methods can be definedusing keyword arguments:

defdeliver(from:"A",to:nil,via:"mail")"Sending from#{from} to#{to} via#{via}."enddeliver(to:"B")# => "Sending from A to B via mail."deliver(via:"Pony Express",from:"B",to:"A")# => "Sending from B to A via Pony Express."

The universal truth

In Ruby, everything exceptnil andfalse is considered true. InC, Python and many other languages, 0 and possibly other values, such asempty lists, are considered false. Take a look at the following Pythoncode (the example applies to other languages, too):

# in Pythonif0:print("0 is true")else:print("0 is false")

This will print “0 is false”. The equivalent Ruby:

# in Rubyif0puts"0 is true"elseputs"0 is false"end

Prints “0 is true”.

Access modifiers apply until the end of scope

In the following Ruby code,

classMyClassprivatedefa_method;true;enddefanother_method;false;endend

You might expectanother_method to be public. Not so. Theprivateaccess modifier continues until the end of the scope, or until anotheraccess modifier pops up, whichever comes first. By default, methods arepublic:

classMyClass# Now a_method is publicdefa_method;true;endprivate# another_method is privatedefanother_method;false;endend

public,private andprotected are really methods, so they can takeparameters. If you pass a Symbol to one of them, that method’s visibility isaltered.

Method access

In Java,public means a method is accessible by anyone.protectedmeans the class’s instances, instances of descendant classes, andinstances of classes in the same package can access it, but not anyoneelse, andprivate means nobody besides the class’s instances canaccess the method.

Ruby differs slightly.public is, naturally, public.private meansthe method(s) are accessible only when they can be called without anexplicit receiver. Onlyself is allowed to be the receiver of aprivate method call.

protected is the one to be on the lookout for. A protected method can becalled from a class or descendant class instances, but also with anotherinstance as its receiver.Here is an example (adapted fromThe Ruby Language FAQ):

classTest# public by defaultdefidentifier99enddef==(other)identifier==other.identifierendendt1=Test.new# => #<Test:0x34ab50>t2=Test.new# => #<Test:0x342784>t1==t2# => true# now make `identifier' protected; it still works# because protected allows `other' as receiverclassTestprotected:identifierendt1==t2# => true# now make `identifier' privateclassTestprivate:identifierendt1==t2# NoMethodError: private method `identifier' called for #<Test:0x342784>

Classes are open

Ruby classes are open. You can open them up, add to them, and change them atany time. Even core classes, likeInteger or evenObject, the parent of allobjects. Ruby on Rails defines a bunch of methods for dealing with time onInteger. Watch:

classIntegerdefhoursself*3600# number of seconds in an hourendaliashourhoursend# 14 hours from 00:00 January 1st# (aka when you finally wake up ;)Time.mktime(2006,01,01)+14.hours# => Sun Jan 01 14:00:00

Funny method names

In Ruby, methods are allowed to end with question marks or exclamation marks.By convention, methods that answer questions end in question marks(e.g.Array#empty?, which returnstrue if the receiver is empty).Potentially “dangerous” methods by convention end with exclamation marks(e.g. methods that modifyself or the arguments,exit!, etc.).Not all methods that change their arguments end with exclamation marks, though.Array#replace replaces the contents of an array with the contentsof another array. It doesn’t make much sense to have a method like thatthatdoesn’t modify self.

Singleton methods

Singleton methods are per-object methods. They are only available on theObject you defined it on.

classCardefinspect"Cheap car"endendporsche=Car.newporsche.inspect# => Cheap cardefporsche.inspect"Expensive car"endporsche.inspect# => Expensive car# Other objects are not affectedother_car=Car.newother_car.inspect# => Cheap car

Missing methods

Ruby doesn’t give up if it can’t find a method that responds to aparticular message. It calls themethod_missing method with the nameof the method it couldn’t find and the arguments. By default,method_missing raises a NameError exception, but you can redefine it tobetter fit your application, and many libraries do. Here is an example:

# id is the name of the method called, the * syntax collects# all the arguments in an array named 'arguments'defmethod_missing(id,*arguments)puts"Method#{id} was called, but not found. It has "+"these arguments:#{arguments.join(", ")}"end__:a,:b,10# => Method __ was called, but not found. It has these# arguments: a, b, 10

The code above just prints the details of the call, but you are free tohandle the message in any way that is appropriate.

Message passing, not function calls

A method call is really amessage to another object:

# This1+2# Is the same as this ...1.+(2)# Which is the same as this:1.send"+",2

Blocks are Objects, they just don’t know it yet

Blocks (closures, really) are heavily used by the standard library. Tocall a block, you can either useyield, or make it aProc byappending a special argument to the argument list, like so:

defblock(&the_block)# Inside here, the_block is the block passed to the methodthe_block# return the blockendadder=block{|a,b|a+b}# adder is now a Proc objectadder.class# => Proc

You can create blocks outside of method calls, too, by callingProc.newwith a block or calling thelambda method.

Similarly, methods are also Objects in the making:

method(:puts).call"puts is an object!"# => puts is an object!

Operators are syntactic sugar

Most operators in Ruby are just syntactic sugar (with some precedencerules) for method calls. You can, for example, override Integer’s+method:

classInteger# You can, but please don't do thisdef+(other)self-otherendend

You don’t need C++’soperator+, etc.

You can even have array-style access if you define the[] and[]= methods.To define the unary + and - (think +1 and -2), you must define the+@ and-@ methods, respectively. The operators below arenot syntactic sugar,though. They are not methods, and cannot be redefined:

=,..,...,not,&&,and,||,or,::

In addition,+=,*= etc. are just abbreviations forvar = var + other_var,var = var * other_var, etc. and therefore cannot be redefined.

Finding Out More

When you are ready for more Ruby knowledge, see ourDocumentation section.


Participate in a friendly and growing community.

Syndicate

Recent News (RSS)


HomeDownloadsDocumentationLibrariesCommunityNewsSecurityAbout Ruby

This site in other languages:Български,Deutsch,English,Español,Français,Bahasa Indonesia,Italiano,日本語,한국어,polski,Português,Русский,Türkçe,Tiếng Việt,简体中文,繁體中文.

This website is proudly maintained by members of the Ruby community.


[8]ページ先頭

©2009-2025 Movatter.jp