$ cat /dev/random
ripley's miscellanous debris

rubycational Tag Archive

Rubycational Channel 1: Ruby e gli Oggetti

(2006-05-17)

Lo sapevate? In ruby tutto e’ un oggetto... sapevatelo su Rubycational Channel :-)

Questo significa che, ad esempio, per rovesciare una stringa faremo:

 puts "subbaqui".reverse
 puts "subbaqui".class
ed otterremo:
iuqabbus
String

Cosi’ facendo chiediamo a ruby di stampare il valore ritornato dai metodi class e reverse dell’oggetto “subbaqui” di tipo String.

Altri esempi?

  puts "subbaqui".upcase
  puts "subbaqui".size
SUBBAQUI
8

Non siete ancora convinti?

Beh allora sappiate che anche tutti gli altri tipi di dati seguono lo stesso principio, sia quelli implementati direttamente nell’interprete (e quindi scritti in C ) sia quelli defini in ruby nelle librerie.

Interi (FixedNum)

  puts 5.succ
  puts -1.abs
6
1

Array

  my_array = []
  my_array << 'primo'
  my_array.push 'secondo'
  puts my_array
primo
secondo

Successioni (Range)

  puts (1..5).min
  puts (1..5).max
  puts '[' + (1..5).to_a.join(',') + ']'
1
5
[1,2,3,4,5]

Espressioni Regolari (Regexp)

  prova = /bbaqui$/
  puts prova.source
  puts prova.match("subbaqui").pre_match
bbaqui$
su

Ora verificatelo di persona con irb:
ripley@ubik:~/projects/current/DevRandom/devrandom$ irb
irb(main):001:0> "unastringa".methods
=> ["%", "upto", "find_all", "[]=", "hex", "<<", "each", "sum", 
"object_id", "reverse!", "chop", "insert", "singleton_methods", 
"dump", "inject", "delete", "concat", "tr_s!", "equal?", "taint", 
"*", "succ", "sort_by", "frozen?", "instance_variable_get", "max", 
"strip!", "+", "kind_of?", "rjust", "respond_to?", "to_a", "index", 
"select", "slice", "oct", "type", "length", "chomp", 
"protected_methods", "squeeze", "upcase", "partition", "sub!", 
"delete!", "upcase!", "crypt", "to_sym", "grep", "eql?", 
"instance_variable_set", "lstrip!", "is_a?", "hash", "center", 
"send", "rindex", "between?", "reject", "to_s", "split", "class", 
"size", "strip", "tainted?", "private_methods", "succ!", "downcase", 
"gsub!", "count", "__send__", "downcase!", "intern", "member?", "squeeze!", 
"untaint", "find", "next", "each_with_index", "rstrip!", "each_line", "id", 
"sub", "slice!", "to_i", "<", "instance_eval", "method", "collect", "<=>", 
"inspect", "tr", "replace", "all?", "==", ">", "reverse", "===", "lstrip", 
"entries", "unpack", "clone", "public_methods", "chop!", "capitalize", 
"extend", "capitalize!", "scan", ">=", "freeze", "detect", "<=", "display", 
"each_byte", "zip", "__id__", "to_f", "casecmp", "gsub", "methods", "empty?", 
"to_str", "map", "=~", "tr_s", "any?", "tr!", "nil?", "dup", "match", "sort", 
"rstrip", "instance_variables", "next!", "swapcase", "min", "chomp!", 
"include?", "instance_of?", "swapcase!", "ljust", "[]"]
irb(main):002:0>


Full of CC licensed ripley's debris
HTML and CSS Design by Nicolas Fafchamps
Generated with Rote and Rog