-
Notifications
You must be signed in to change notification settings - Fork 2
Ruby
Voir aussi gemme, Ruby on Rails et rvm
Tutos :
- +++ https://blog.calendly.com/ruby-gotchas-javascript-developer/
- http://tryruby.org/
- http://www.caliban.org/ruby/rubyguide.shtml
- ...
Références :
Sources :
- http://ruby-doc.org/docs/beginner-fr/xhtml/ch04s02.html
- http://www.rubyist.net/~slagell/ruby/objinitialization.html
- ...
Installation :
...
http://rubyforge.org/frs/?group_id=167
Utiliser rvm:
brew update
brew doctor
brew install gnupg openssl
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable
rvm autolibs read-only # cf http://stackoverflow.com/a/19696719/587407
rvm install ruby-2.4 --with-openssl-dir=`brew --prefix openssl`
which ruby
which gem
gem install bundle
Très utile : irb = interactive ruby console
Ruby est souvent utilisé pour faire des DSL : http://jroller.com/rolsen/entry/building_a_dsl_in_ruby
http://stackoverflow.com/questions/3694153/catch-all-exceptions-in-a-rails-controller
begin
# do something dodgy
rescue ActiveRecord::RecordNotFound
# handle not found error
rescue ActiveRecord::ActiveRecordError
# handle other ActiveRecord errors
rescue # StandardError
# handle most other errors
rescue Exception => e # here we grab the exception
# handle everything else
puts e.message
puts e.backtrace.inspect
end
Voir la table http://rubylearning.com/satishtalim/ruby_exceptions.html, avec notamment :
- NotImplementedError
- ArgumentError
- RuntimeError
On peut lancer une exception sur mauvais paramètres de la manière suivante :
raise ArgumentError, "Name too big"
on peut faire des choses plus puissantes comme retirer nos méthodes internes de la stack trace affichée : http://phrogz.net/programmingruby/tut_exceptions.html
Ne pas hésiter à utiliser TypeError également, cf. http://redmine.ruby-lang.org/issues/show/2830
Action sur tous les éléments d'un tableau : http://www.benjaminbooth.com/tableorbooth/2006/12/ruby_arrays_sel.html
.map/.select, .collect
assignation massive : http://nathanhoad.net/ruby-multiple-assignment
a, b, c, d = [1, 2, 3, 4]
et aussi
return a, b, c, d (équivalent à retourner un tableau)
Docs :
- http://www.techotopia.com/index.php/Ruby_Object_Oriented_Programming
- http://strugglingwithruby.blogspot.com/2009/03/accessors.html
- ...
J'ai toujours des problèmes avec ça.
class Foo
CONSTANTE = 12
@@variable_de_classe = 33
def self.methode_de_classe
...
end
end
Mais comment y accède-t-on ?
- depuis l'extérieur :
Foo::@@variable_de_class non ! ne fonctionne pas !
Foo::methode_de_class
- depuis l'intérieur
CONSTANTE
@@variable_de_class
self.class.methode_de_class
.camelize, .pluralize, .singularize, .descendants...
define_method(zzz) do
...
end
- Rélectivité : http://www.ruby-forum.com/topic/142523
Fixnum.instance_methods
String.method_defined?(:length)
fail "message"
warn "message"
Surveiller une exception :
begin
...
rescue
debugger
end
http://blog.grayproductions.net/articles/ruby_19s_three_default_encodings
- http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html
- http://pragdave.blogs.pragprog.com/pragdave/2007/12/pipelines-using.html
- http://stackoverflow.com/questions/839997/better-way-to-fill-a-ruby-hash
- http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/
- http://www.skorks.com/2009/08/how-a-ruby-case-statement-works-and-what-you-can-do-with-it/
- http://www.thirdbit.net/articles/2007/08/01/10-things-you-should-know-about-method_missing/
- http://railstips.org/blog/archives/2009/08/07/patterns-are-not-scary-method-missing-proxy/
- http://blog.hasmanythrough.com/2006/8/13/how-dynamic-finders-work
Hard to use.
Gem : gem est inclus dans les dernières versions de Ruby.
Libs utiles :
gem install rails
gem install hpricot
gem install Mechanize
http://rubyforge.org/projects/mechanize/
http://rubygems.org/gems/hpricot
http://stackoverflow.com/questions/839997/better-way-to-fill-a-ruby-hash