background preloader

Tutos

Facebook Twitter

Fctions

Support. Ruby [Archives] Ruby Programming Language Tutorial. Ruby programming tutorial. See also: Part One and Part Two of this series As I mentioned back in part one of this series, double-quoted strings do more work than single-quoted strings.

Ruby programming tutorial

In particular, they have the ability to evaluate bits of themselves as though they were programming code. Here is an example from one of this month’s sample programs, 1strings.rb: myname = 'Fred' puts( 'Hello #{myname}' ) puts( "Hello #{myname}" ) A double-quoted string is also able to evaluate variables and attributes (‘properties’) such as ob.name, expressions such as 2*3 and bits of code such as the method-call ob.ten and ‘escape characters’ such as “\n” and “\t” representing a newline and a tab. ‘It\’s my party’ User-Defined String Delimiters If, for some reason, single and double-quotes aren’t convenient – if, for example, your strings contain lots of quote characters and you don’t want to have to keep putting backslashes in front of them – you have the option of delimiting strings in many other ways. %Q[Hello #{myname}] Backquotes. Ruby Basic Tutorial. Troubleshooters.Com, Code Corner and Ruby Revival Present Ruby Basic Tutorial Copyright (C) 2005 by Steve Litt Note: All materials in Ruby Revival are provided AS IS.

Ruby Basic Tutorial

By reading the materials in Ruby Revival you are agreeing to assume all risks involved in the use of the materials, and you are agreeing to absolve the authors, owners, and anyone else involved with Python Patrol of any responsibility for the outcome of any use of these materials, even in the case of errors and/or omissions in the materials. If you do not agree to this, you must not read these materials. To the 99.9% of you honest readers who take responsibility for your own actions, I'm truly sorry it is necessary to subject all readers to the above disclaimer. CONTENTS This is a Ruby tutorial for one not knowing Ruby. Ruby can be used as a fully object oriented language, in which case you'd create classes and objects to accomplish everything. This is the simplest possible Ruby program, hello.rb.

Let's count to 10... Retry. Ruby Constants. A Ruby constant is like a variable, except that its value is supposed to remain constant for the duration of the program.

Ruby Constants

The Ruby interpreter does not actually enforce the constancy of constants, but it does issue a warning if a program changes the value of a constant (as shown in this trivial example) - p054constwarn.rb # p054constwarn.rb A_CONST = 10 A_CONST = 20 Produces a warning: p054constwarn.rb:3: warning: already initialized constant A_CONST p054constwarn.rb:3: warning: already initialized constant A_CONST Lexically, the names of constants look like the names of local variables, except that they begin with a capital letter. Note that constants do not exist until a value is actually assigned to them. Although constants should not be changed, you can modify the internal states of the objects they reference, as seen in p055constalter.rb A_CONST = "Doshi" B_CONST = A_CONST A_CONST[0] = "J" puts A_CONST puts B_CONST Note: The program p056const.rb shows all of this: