background preloader

Ruby

Facebook Twitter

Ruby: instance_eval and class_eval method definitions. In yesterday's entry on Cleaning up Controller Tests you can find the following code.

Ruby: instance_eval and class_eval method definitions

Define_method, instance_eval and class_eval - Plough => Ruby. Right, so this is a tad confusing and the reason I am explaining is to ensure that I understand it properly. instance_eval - It creates singleton methods on the object it is invoked on.

define_method, instance_eval and class_eval - Plough => Ruby

If it is invoked on a Class object then these methods become singleton methods of the class. Some people would call them “class methods” but these are simply methods in an object’s singleton class. Here’s the code: Ruby - How to understand the difference between class_eval() and instance_eval()? Ruby Dynamic Methods - Blog @ RohitRox. We define methods using def keywords which is fine for most of the cases.

Ruby Dynamic Methods - Blog @ RohitRox

But consider a situation where you have to create a series of methods all of which have the same basic structure and logic then it seems repititive and not dry. Ruby, being a dynamic language, you can create methods on the fly. So, what does that mean? Metaprogramming - Ruby: define_method vs. def. Advanced Rails MetaProgramming with InstanceEval. Ruby's instance_eval InstanceEval is what you use to create a DSL.

Advanced Rails MetaProgramming with InstanceEval

It's the engine behind RSpec, Sinatra and Rails, and everything simple you take for granted. Turning Ruby block callbacks inside out. A Techscursion. Matt Sears wrote a great article about creating callbacks with Ruby blocks The crux of his method was the building of an anonymous class that would respond correctly to his callbacks For some reason this post captured my imagination and I started to deconstruct his method, as I couldn’t quite grasp why it worked.

Turning Ruby block callbacks inside out. A Techscursion.

Chef Style Guide — Chef Docs. Ruby Basics This section covers the basics of Ruby.

Chef Style Guide — Chef Docs

Verify Syntax Many people who are new to Ruby often find that it doesn’t take very long to get up to speed with the basics. For example, it’s useful to know how to check the syntax of a Ruby file, such as the contents of a cookbook named my_cookbook.rb: Programming Language Synchronicity: metaprogramming. There are many small parts of Ruby, tips, tricks and strange things.

Programming Language Synchronicity: metaprogramming

I thought that I would write about some of the more interesting of these, since some of them are common idioms in the Ruby community. The basis for the information is as always from the Pick-axe, but how these things are used in real life comes from various places. The splat operator The asterisk is sometimes called the splat operator when not used for multiplication.

It is used in two different places for opposite cases. A,b,c = *[1,3,2] Second, it's used at the left hand side to collect more than one right hand value into an arra *a = 1,3,2. Understanding instance exec in ruby. Difference between block and &block in Ruby. Introduction to ruby eval. Class and Instance Methods in Ruby. The other day I was explaining the difference between class and instance methods to a friend and I realized that I should probably write up a post.

Class and Instance Methods in Ruby

I figured since I’m on a plane headed back home, now was as good of time as ever. If you want a little history, you can read about the difference between class and instance variables. One Line Summary. Include vs Extend in Ruby. Now that we know the difference between an instance method and a class method, let’s cover the difference between include and extend in regards to modules.

Include vs Extend in Ruby

Include is for adding methods to an instance of a class and extend is for adding class methods. Let’s take a look at a small example. module Foo def foo puts 'heyyyyoooo! ' Po-ru.com: Destructuring assignment in Ruby. My post on underscores in Ruby attracted quite a lot of interest, particularly on the topic of destructuring assignment, so I thought I’d go into a bit more detail.

po-ru.com: Destructuring assignment in Ruby

As mentioned previously, Ruby supports destructuring assignment. You can assign an array to multiple variables both in direct assignment: a, b, c = [1, 2, 3] Ruby Tutorial with Code Samples. Reflection - How do I get a message sender in SmallTalk (Pharo) Meta programming. Ruby Tutorial with Code Samples. Three implicit contexts in Ruby - 世界線航跡蔵. Yehuda Katz wrote an article about self and metaclass. Ruby If Examples: Elsif, Else and Unless. Keyword Array Hash String 2D Case Class Console Convert DateTime Exception File Format If Iterator Math Method Nil Number Regexp Replace Set Sort Split Substring While In a program, statements are sequentially executed.

One comes after another. With an if-statement, we introduce a branch. We can avoid certain statements and jump to other ones. We direct the flow of control. Keywords: if elsif else unless and or end Example The if-statement uses the if-keyword. However:If the expression evaluates to false, the inner block of an if-statement is not reached. Ruby Programming/Syntax/Control Structures. Control Structures[edit] Conditional Branches[edit] Ruby can control the execution of code using Conditional branches.

A conditional Branch takes the result of a test expression and executes a block of code depending whether the test expression is true or false. If the test expression evaluates to the constant false or nil, the test is false; otherwise, it is true. Note that the number zero is considered true, whereas many other programming languages consider it false. In many popular programming languages, conditional branches are statements.