Quantcast
Channel: Notes posted to Ruby
Browsing all 107 articles
Browse latest View live

Destructive to the Original String. (String#insert)

Just as an FYI this function is destructive to the original String object. name = 'draper' #=> "draper" name.insert( 0, 'don ' ) #=> 'don draper' name #=> 'don draper'

View Article



Use Join to Turn Array Items into a String. (Array#to_s)

If you’re looking to take an array like [ 'don', 'draper' ] And get 'don draper' Then use join instead: [ 'don', 'draper' ].join( ' ' ) #=> 'don draper'

View Article

ferragamo (Dir#mkdir)

Salvatore <a href=“http://www.ferragamochaussures.com”>ferragamo pas cher, 30 ~ 70% de rabais! Salvatore ferragamo chaussures!Bienvenue à notre Salvatore ferragamo en ligne, la spécialité de...

View Article

BasicObject.new (BasicObject#new)

Instantiates a new blank object (devoid of methods). The only class method of class BasicObject. see “ri BasicObject” eg, > o=BasicObject.new (Object doesn’t support #inspect) > > o.methods...

View Article

Dir (Dir#exists?)

The documentation is using File.directory?(“/path/to/directory”), but the method being referred to is Dir.exists?(). Dir.exists?(".") => true The source code is the same as File.directory?().

View Article


Starts with a Capital Letter (String#start_with?)

(or any regular expression you’d like) 'Abracadabra'[0..0] =~ /[A-Z]/ # => true

View Article

Undefined Method `mktmpdir' for Dir:Class (Dir#mktmpdir)

Be sure to require 'tmpdir' before using it. Read more at http://mikbe.tk/2011/03/07/temporary-directory.

View Article

railz (Math#gamma)

heyyyy

View Article


Starts with capital letter alternative (String#start_with?)

Just adding an anchor to the regular expression seems simpler (and was faster in my benchmarks, not that that matters much): 'Abracadabra' =~ /^[A-Z]/

View Article


rindex with identically array elements (Array#rindex)

Code Example a = [1,1,1] a.rindex( a.min ) #=> 2

View Article

more_than? instance method (Array)

Over the weekend I kept running into instances where I was writing code like this: Code example arr = ['hello', 'world'] if arr.length > 2 # do stuff else # do something else end So I ended up...

View Article

Test if an array is included in another (Array#include?)

Array class Array def included_in? array array.to_set.superset?(self.to_set) end end [1,2,4].included_in?([1,10,2,34,4]) #=> true

View Article

see also – similar methods (DateTime#strftime)

See also Time#strftime and Date#strftime . (They work similarly, but have different APIdock notes.)

View Article


see also – similar methods (Date#strftime)

See also Time#strftime and DateTime#strftime . (They work similarly, but have different APIdock notes.)

View Article

see also – similar methods (Time#strftime)

See also DateTime#strftime and Date#strftime . (They work similarly, but have different APIdock notes.)

View Article


Assignment using 'key: value' (Hash#[])

Another shorthand way of assigning key, value pairs: Hash[one: 1, two: 2] #=> {:one=>1, :two=>2}

View Article

Long-wanted functional extension (Enumerable#each_with_object)

This is pretty nice method allowing you to build stuff in a functional way. Lets say you want to build a hash from an array, keyed by array object, where each value is the number of same objects in...

View Article


What it do? (Hash#to_yaml)

For those favoring YAML outputs, this methods simply and recursively outputs the keys and values in YAML (into a String) for your pleasure.

View Article

__callee__ and __method__ both return symbol when originally defined, not...

There has been some indication that __callee__ is intended to return the symbol with which the method was actually invoked, whereas __method__ returns name with which the method was originally...

View Article

Reports originally defined method names, not invoked names in Ruby 1.9.x...

In Ruby 1.8.7, the reported method names were those of the methods actually invoked, so if #b was an alias for #a, and #b was called, it would be reported as “… in `b’”. In Ruby 1.9, the same...

View Article
Browsing all 107 articles
Browse latest View live




Latest Images