Monthly Archives

November 2019

.dub metod, which duplicates an object

By Posts No Comments
 
def 
 change_string(str)
 str.replace("New string content!") 
end
 
s = "Original string content!"
change_string(s.dup)

puts s 

Or you can use .freeze method

s = "Original string content!"
s.freeze

change_string(s)

Note that there’s no corresponding unfreeze method. Freezing is forever.

Also, you can use clone. It’s like as dup , but if you clone a frozen object, the clone is also frozen—whereas if you dup a frozen object, the duplicate isn’t frozen.


Commonly used Ruby command-line

By Ruby, Ruby extensions and programming libraries No Comments
Commonly used Ruby command-line switches
SwitchDescriptionExample of usage
-cChecks the syntax of a program file without executing the programruby -c c2f.rb
-wGives warning messages during program executionruby -w c2f.rb
-eExecutes the code provided in quotation marks on the command lineruby -e ‘puts “Code demo!”‘
-lLine mode: prints a newline after every line of outputruby -le ‘print “+ newline!”‘
-rnameRequires the named featureruby –rprofile
-vShows Ruby version information and executes the program in verbose moderuby –v
–versionShows Ruby version informationruby –-version
-hShows information about all command-line switches for the interpreterruby –h

Difference between require and load

By Posts, Ruby, Ruby extensions and programming libraries No Comments
require

is more abstract than load. Strictly speaking, you don’t require a file; you require a feature. And typically, you do so without even specifying the extension on the filename. To see how this works, change this line in test.rb

 load "test.rb"

to this:

 require "./test"

When you run test.rb, you get the same result as before, even though you haven’t supplied the full name of the file you want loaded.