How to implement Enums in Ruby?

Two ways. Symbols (:foo notation) or constants (FOO notation). Symbols are appropriate when you want to enhance readability without littering code with literal strings. postal_code[:minnesota] = “MN” postal_code[:new_york] = “NY” Constants are appropriate when you have an underlying value that is important. Just declare a module to hold your constants and then declare the constants … Read more

rbenv not changing ruby version

Check that PATH contains $HOME/.rbenv/shims and $HOME/.rbenv/bin $ env | grep PATH Also check that you have the following in your ~/.bash_profile if using bash or ~/.zshenv if using zsh export PATH=”$HOME/.rbenv/bin:$PATH” eval “$(rbenv init -)” NOTE: Make sure it’s the last setting in your ~/.bash_profile . I ran into an issue where I installed … Read more

Difference between “and” and && in Ruby?

and is the same as && but with lower precedence. They both use short-circuit evaluation. WARNING: and even has lower precedence than = so you’ll usually want to avoid and. An example when and should be used can be found in the Rails Guide under “Avoiding Double Render Errors”.

In Ruby, how do I skip a loop in a .each loop, similar to ‘continue’ [duplicate]

Use next: (1..10).each do |a| next if a.even? puts a end prints: 1 3 5 7 9 For additional coolness check out also redo and retry. Works also for friends like times, upto, downto, each_with_index, select, map and other iterators (and more generally blocks). For more info see http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UL.

Should I use alias or alias_method?

alias_method can be redefined if need be. (it’s defined in the Module class.) alias‘s behavior changes depending on its scope and can be quite unpredictable at times. Verdict: Use alias_method – it gives you a ton more flexibility. Usage: def foo “foo” end alias_method :baz, :foo

How to find where gem files are installed

Use gem environment to find out about your gem environment: RubyGems Environment: – RUBYGEMS VERSION: 2.1.5 – RUBY VERSION: 2.0.0 (2013-06-27 patchlevel 247) [x86_64-darwin12.4.0] – INSTALLATION DIRECTORY: /Users/ttm/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0 – RUBY EXECUTABLE: /Users/ttm/.rbenv/versions/2.0.0-p247/bin/ruby – EXECUTABLE DIRECTORY: /Users/ttm/.rbenv/versions/2.0.0-p247/bin – SPEC CACHE DIRECTORY: /Users/ttm/.gem/specs – RUBYGEMS PLATFORMS: – ruby – x86_64-darwin-12 – GEM PATHS: – /Users/ttm/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0 – /Users/ttm/.gem/ruby/2.0.0 … Read more

Ruby: Calling class method from instance

Rather than referring to the literal name of the class, inside an instance method you can just call self.class.whatever. class Foo def self.some_class_method puts self end def some_instance_method self.class.some_class_method end end print “Class method: ” Foo.some_class_method print “Instance method: ” Foo.new.some_instance_method Outputs: Class method: Foo Instance method: Foo

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)