How do I write a switch statement in Ruby?
Ruby uses the case
expression instead.
case x
when 1..5
"It's between 1 and 5"
when 6
"It's 6"
when "foo", "bar"
"It's either foo or bar"
when String
"You passed a string"
else
"You gave me #{x} -- I have no idea what to do with that."
end
Ruby compares the object in the when
clause with the object in the case
clause using the ===
operator. For example, 1..5 === x
, and not x === 1..5
.
This allows for sophisticated when
clauses as seen above. Ranges, classes and all sorts of things can be tested for rather than just equality.
Unlike switch
statements in many other languages, Ruby’s case
does not have fall-through, so there is no need to end each when
with a break
. You can also specify multiple matches in a single when
clause like when "foo", "bar"
.
case...when
behaves a bit unexpectedly when handling classes. This is due to the fact that it uses the ===
operator.
That operator works as expected with literals, but not with classes:
This means that if you want to do a case ... when
over an object’s class, this will not work:
obj = 'hello'
case obj.class
when String
print('It is a string')
when Fixnum
print('It is a number')
else
print('It is not a string or number')
end
Will print “It is not a string or number”.
Fortunately, this is easily solved. The ===
operator has been defined so that it returns true
if you use it with a class and supply an instance of that class as the second operand:
In short, the code above can be fixed by removing the .class
:
obj = 'hello'
case obj # was case obj.class
when String
print('It is a string')
when Fixnum
print('It is a number')
else
print('It is not a string or number')
end
I hit this problem today while looking for an answer, and this was the first appearing page, so I figured it would be useful to others in my same situation.
It is done by case in Ruby. Also see this article on Wikipedia.
Quoted:
case n
when 0
puts 'You typed zero'
when 1, 9
puts 'n is a perfect square'
when 2
puts 'n is a prime number'
puts 'n is an even number'
when 3, 5, 7
puts 'n is a prime number'
when 4, 6, 8
puts 'n is an even number'
else
puts 'Only single-digit numbers are allowed'
end
Another example:
score = 70
result = case score
when 0..40 then "Fail"
when 41..60 then "Pass"
when 61..70 then "Pass with Merit"
when 71..100 then "Pass with Distinction"
else "Invalid Score"
end
puts result
On around page 123 (I am using Kindle) of The Ruby Programming Lanugage (1st Edition, O’Reilly), it says the then
keyword following the when
clauses can be replaced with a newline or semicolon (just like in the if then else
syntax). (Ruby 1.8 also allows a colon in place of then
… But this syntax is no longer allowed in Ruby 1.9.)
I have a value 'Dog'
and an array ['Cat', 'Dog', 'Bird']
.
How do I check if it exists in the array without looping through it? Is there a simple way of checking if the value exists, nothing more?
You’re looking for include?
:
There is an in?
method in ActiveSupport
(part of Rails) since v3.1, as pointed out by @campaterson. So within Rails, or if you require 'active_support'
, you can write:
OTOH, there is no in
operator or #in?
method in Ruby itself, even though it has been proposed before, in particular by Yusuke Endoh a top notch member of ruby-core.
As pointed out by others, the reverse method include?
exists, for all Enumerable
s including Array
, Hash
, Set
, Range
:
Note that if you have many values in your array, they will all be checked one after the other (i.e. O(n)
), while that lookup for a hash will be constant time (i.e O(1)
). So if you array is constant, for example, it is a good idea to use a Set instead. E.g:
require 'set'
ALLOWED_METHODS = Set[:to_s, :to_i, :upcase, :downcase
# etc
]
def foo(what)
raise "Not allowed" unless ALLOWED_METHODS.include?(what.to_sym)
bar.send(what)
end
A quick test reveals that calling include?
on a 10 element Set
is about 3.5x faster than calling it on the equivalent Array
(if the element is not found).
A final closing note: be wary when using include?
on a Range
, there are subtleties, so refer to the doc and compare with cover?
…
Try
I have a string variable with content as follows:
varMessage =
"hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n"
"/my/name/is/balaji.so\n"
"call::myFunction(int const&)\n"
"void::secondFunction(char const&)\n"
.
.
.
"this/is/last/line/liobrary.so"
in above string i have to find a sub string i.e.
How can I find it? I just need to determine whether the substring is present or not.
You can use the include?
method:
If case is irrelevant, then a case-insensitive regular expression is a good solution:
This will also work for multi-line strings.
See Ruby’s Regexp class.
You can also do this…
my_string = "Hello world"
if my_string["Hello"]
puts 'It has "Hello"'
else
puts 'No "Hello" found'
end
# => 'It has "Hello"'
How do I take a string and convert it to lower or upper case in Ruby?
Ruby has a few methods for changing the case of strings. To convert to lowercase, use downcase
:
Similarly, upcase
capitalizes every letter and capitalize
capitalizes the first letter of the string but lowercases the rest:
"hello James!".upcase #=> "HELLO JAMES!"
"hello James!".capitalize #=> "Hello james!"
"hello James!".titleize #=> "Hello James!"
If you want to modify a string in place, you can add an exclamation point to any of those methods:
Refer to the documentation for String for more information.
You can find out all the methods available on a String by opening irb and running:
And for a list of the methods available for strings in particular:
I use this to find out new and interesting things about objects which I might not otherwise have known existed.
Like @endeR mentioned, if internationalization is a concern, the unicode_utils gem is more than adequate.
$ gem install unicode_utils
$ irb
> require 'unicode_utils'
=> true
> UnicodeUtils.downcase("FEN BİLİMLERİ", :tr)
=> "fen bilimleri"
String manipulations in Ruby 2.4 are now unicode-sensitive.
I need to read the data out of database and then save it in a text file.
How can I do that in Ruby? Is there any file management system in Ruby?
The Ruby File class will give you the ins and outs of ::new
and ::open
but its parent, the IO class, gets into the depth of #read
and #write
.
Are you looking for the following?
You can use the short version:
It returns the length written; see ::write for more details and options.
To append to the file, if it already exists, use:
I am looking for a more elegant way of concatenating strings in Ruby.
I have the following line:
Is there a nicer way of doing this?
And for that matter what is the difference between <<
and +
?
You can do that in several ways:
<<
but that is not the usual way
With string interpolation
source = "#{ROOT_DIR}/#{project}/App.config"
```</li>
<li><p>with `+`</p>
```ruby
source = "#{ROOT_DIR}/" + project + "/App.config"
```</li>
</ol>
The second method seems to be more efficient in term of memory/speed from what I've seen (not measured though). All three methods will throw an uninitialized constant error when ROOT_DIR is nil.
When dealing with pathnames, you may want to use `File.join` to avoid messing up with pathname separator.
In the end, it is a matter of taste.
#### Answer 2 (score 92)
The `+` operator is the normal concatenation choice, and is probably the fastest way to concatenate strings.
The difference between `+` and `<<` is that `<<` changes the object on its left hand side, and `+` doesn't.
```ruby
irb(main):001:0> s = 'a'
=> "a"
irb(main):002:0> s + 'b'
=> "ab"
irb(main):003:0> s
=> "a"
irb(main):004:0> s << 'b'
=> "ab"
irb(main):005:0> s
=> "ab"
If you are just concatenating paths you can use Ruby’s own File.join method.
I am in the process of porting a CLI library from Ruby over to Node.js. In my code I execute several third party binaries when necessary. I am not sure how best to accomplish this in Node.
Here’s an example in Ruby where I call PrinceXML to convert a file to a PDF:
What is the equivalent code in Node?
For even newer version of Node.js (v8.1.4), the events and calls are similar or identical to older versions, but it’s encouraged to use the standard newer language features. Examples:
For buffered, non-stream formatted output (you get it all at once), use child_process.exec
:
const { exec } = require('child_process');
exec('cat *.js bad_file | wc -l', (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
You can also use it with Promises:
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function ls() {
const { stdout, stderr } = await exec('ls');
console.log('stdout:', stdout);
console.log('stderr:', stderr);
}
ls();
If you wish to receive the data gradually in chunks (output as a stream), use child_process.spawn
:
const { spawn } = require('child_process');
const child = spawn('ls', ['-lh', '/usr']);
// use child.stdout.setEncoding('utf8'); if you want text chunks
child.stdout.on('data', (chunk) => {
// data from standard output is here as buffers
});
// since these are streams, you can pipe them elsewhere
child.stderr.pipe(dest);
child.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
Both of these functions have a synchronous counterpart. An example for child_process.execSync
:
const { execSync } = require('child_process');
// stderr is sent to stderr of parent process
// you can set options.stdio if you want it to go elsewhere
let stdout = execSync('ls');
As well as child_process.spawnSync
:
const { spawnSync} = require('child_process');
const child = spawnSync('ls', ['-lh', '/usr']);
console.log('error', child.error);
console.log('stdout ', child.stdout);
console.log('stderr ', child.stderr);
Note: The following code is still functional, but is primarily targeted at users of ES5 and before.
The module for spawning child processes with Node.js is well documented in the documentation (v5.0.0). To execute a command and fetch its complete output as a buffer, use child_process.exec
:
var exec = require('child_process').exec;
var cmd = 'prince -v builds/pdf/book.html -o builds/pdf/book.pdf';
exec(cmd, function(error, stdout, stderr) {
// command output is in stdout
});
If you need to use handle process I/O with streams, such as when you are expecting large amounts of output, use child_process.spawn
:
var spawn = require('child_process').spawn;
var child = spawn('prince', [
'-v', 'builds/pdf/book.html',
'-o', 'builds/pdf/book.pdf'
]);
child.stdout.on('data', function(chunk) {
// output will be here in chunks
});
// or if you want to send output elsewhere
child.stdout.pipe(dest);
If you are executing a file rather than a command, you might want to use child_process.execFile
, which parameters which are almost identical to spawn
, but has a fourth callback parameter like exec
for retrieving output buffers. That might look a bit like this:
var execFile = require('child_process').execFile;
execFile(file, args, options, function(error, stdout, stderr) {
// command output is in stdout
});
As of v0.11.12, Node now supports synchronous spawn
and exec
. All of the methods described above are asynchronous, and have a synchronous counterpart. Documentation for them can be found here. While they are useful for scripting, do note that unlike the methods used to spawn child processes asynchronously, the synchronous methods do not return an instance of ChildProcess
.
v12.9.1
, LTS v10.16.3
, and v8.16.1
— Aug 2019
Async method (Unix):
'use strict';
const { spawn } = require( 'child_process' );
const ls = spawn( 'ls', [ '-lh', '/usr' ] );
ls.stdout.on( 'data', data => {
console.log( `stdout: ${data}` );
} );
ls.stderr.on( 'data', data => {
console.log( `stderr: ${data}` );
} );
ls.on( 'close', code => {
console.log( `child process exited with code ${code}` );
} );
Async method (Windows):
'use strict';
const { spawn } = require( 'child_process' );
const dir = spawn( 'dir', [ '.' ] );
dir.stdout.on( 'data', data => console.log( `stdout: ${data}` ) );
dir.stderr.on( 'data', data => console.log( `stderr: ${data}` ) );
dir.on( 'close', code => console.log( `child process exited with code ${code}` ) );
Sync:
'use strict';
const { spawnSync } = require( 'child_process' );
const ls = spawnSync( 'ls', [ '-lh', '/usr' ] );
console.log( `stderr: ${ls.stderr.toString()}` );
console.log( `stdout: ${ls.stdout.toString()}` );
From Node.js v12.9.1 Documentation
The same goes for Node.js v10.16.3 Documentation and Node.js v8.16.1 Documentation
How do I generate a random number between 0
and n
?
Use rand(range)
From Ruby Random Numbers:
If you needed a random integer to simulate a roll of a six-sided die, you’d use:
Finally, if you just need a random float, just call1 + rand(6)
. A roll in craps could be simulated with2 + rand(6) + rand(6)
.rand
with no arguments.
As Marc-André Lafortune mentions in his answer below (go upvote it), Ruby 1.9.2 has its own Random
class (that Marc-André himself helped to debug, hence the 1.9.2 target for that feature).
For instance, in this game where you need to guess 10 numbers, you can initialize them with:
Note:
Using Random.new.rand(20..30)
(using Random.new
) generally would not be a good idea, as explained in detail (again) by Marc-André Lafortune, in his answer (again).
But if you don’t use Random.new
, then the class method rand
only takes a max
value, not a Range
, as banister (energetically) points out in the comment (and as documented in the docs for Random
). Only the instance method can take a Range
, as illustrated by generate a random number with 7 digits.
This is why the equivalent of Random.new.rand(20..30)
would be 20 + Random.rand(11)
, since Random.rand(int)
returns “a random integer greater than or equal to zero and less than the argument.” 20..30
includes 30, I need to come up with a random number between 0 and 11, excluding 11.
While you can use rand(42-10) + 10
to get a random number between 10
and 42
(where 10 is inclusive and 42 exclusive), there’s a better way since Ruby 1.9.3, where you are able to call:
Available for all versions of Ruby by requiring my backports
gem.
Ruby 1.9.2 also introduced the Random
class so you can create your own random number generator objects and has a nice API:
The Random
class itself acts as a random generator, so you call directly:
Notes on Random.new
In most cases, the simplest is to use rand
or Random.rand
. Creating a new random generator each time you want a random number is a really bad idea. If you do this, you will get the random properties of the initial seeding algorithm which are atrocious compared to the properties of the random generator itself.
If you use Random.new
, you should thus call it as rarely as possible, for example once as MyApp::Random = Random.new
and use it everywhere else.
The cases where Random.new
is helpful are the following:
rand
/Random.rand
that the main programs might be relying on
Random
objects can marshalled)
If you’re not only seeking for a number but also hex or uuid it’s worth mentioning that the SecureRandom
module found its way from ActiveSupport
to the ruby core in 1.9.2+. So without the need for a full blown framework:
require 'securerandom'
p SecureRandom.random_number(100) #=> 15
p SecureRandom.random_number(100) #=> 88
p SecureRandom.random_number #=> 0.596506046187744
p SecureRandom.random_number #=> 0.350621695741409
p SecureRandom.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"
It’s documented here: Ruby 1.9.3 - Module: SecureRandom (lib/securerandom.rb)
How do I call shell commands from inside of a Ruby program? How do I then get output from these commands back into Ruby?
This explanation is based on a commented Ruby script from a friend of mine. If you want to improve the script, feel free to update it at the link.
First, note that when Ruby calls out to a shell, it typically calls /bin/sh
, not Bash. Some Bash syntax is not supported by /bin/sh
on all systems.
Here are ways to execute a shell script:
`Kernel#, commonly called backticks –
cmd``
This is like many other languages, including Bash, PHP, and Perl.
Returns the result (i.e. standard output) of the shell command.
Docs: http://ruby-doc.org/core/Kernel.html#method-i-60
value = `echo 'hi'`
value = `#{cmd}`
```</li>
<li><p>Built-in syntax, `%x( cmd )`</p>
<p>Following the `x` character is a delimiter, which can be any character.
If the delimiter is one of the characters `(`, `[`, `{`, or `<`,
the literal consists of the characters up to the matching closing delimiter,
taking account of nested delimiter pairs. For all other delimiters, the
literal comprises the characters up to the next occurrence of the
delimiter character. String interpolation `#{ ... }` is allowed.</p>
Returns the result (i.e. standard output) of the shell command, just like the backticks.
Docs: <a href="http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html" rel="noreferrer">http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html</a>
```ruby
value = %x( echo 'hi' )
value = %x[ #{cmd} ]
```</li>
<li><p>`Kernel#system`</p>
Executes the given command in a subshell.
Returns `true` if the command was found and run successfully, `false` otherwise.
Docs: <a href="http://ruby-doc.org/core/Kernel.html#method-i-system" rel="noreferrer">http://ruby-doc.org/core/Kernel.html#method-i-system</a>
```ruby
wasGood = system( "echo 'hi'" )
wasGood = system( cmd )
```</li>
<li><p>`Kernel#exec`</p>
Replaces the current process by running the given external command.
Returns none, the current process is replaced and never continues.
Docs: <a href="http://ruby-doc.org/core/Kernel.html#method-i-exec" rel="noreferrer">http://ruby-doc.org/core/Kernel.html#method-i-exec</a>
```ruby
exec( "echo 'hi'" )
exec( cmd ) # Note: this will never be reached because of the line above
```</li>
</ol>
<p>Here's some extra advice:
`$?`, which is the same as `$CHILD_STATUS`, accesses the status of the last system executed command if you use the backticks, `system()` or `%x{}`.
You can then access the `exitstatus` and `pid` properties:</p>
```ruby
$?.exitstatus
For more reading see:
Here’s a flowchart based on this answer. See also, using script
to emulate a terminal.
The way I like to do this is using the %x
literal, which makes it easy (and readable!) to use quotes in a command, like so:
Which, in this case, will populate file list with all test files under the current directory, which you can process as expected:
What is the Ruby function to remove all white space? Kind of like php’s trim()
?
If you want to remove only leading and trailing whitespace (like PHP’s trim) you can use .strip
, but if you want to remove all whitespace, you can use .gsub(/\s+/, "")
instead .
And to emulate PHP’s trim()
function:
PHP, for all its warts, is pretty good on this count. There’s no difference between an array and a hash (maybe I’m naive, but this seems obviously right to me), and to iterate through either you just do
In Ruby there are a bunch of ways to do this sort of thing:
Hashes make more sense, since I just always use
Why can’t I do this for arrays? If I want to remember just one method, I guess I can use each_index
(since it makes both the index and value available), but it’s annoying to have to do array[index]
instead of just value
.
Oh right, I forgot about array.each_with_index
. However, this one sucks because it goes |value, key|
and hash.each
goes |key, value|
! Is this not insane?
This will iterate through all the elements:
Prints:
This will iterate through all the elements giving you the value and the index:
Prints:
I’m not quite sure from your question which one you are looking for.
I think there is no one right way. There are a lot of different ways to iterate, and each has its own niche.
each
is sufficient for many usages, since I don’t often care about the indexes.
each_ with _index
acts like Hash#each - you get the value and the index.
each_index
- just the indexes. I don’t use this one often. Equivalent to “length.times”.
map
is another way to iterate, useful when you want to transform one array into another.
select
is the iterator to use when you want to choose a subset.
inject
is useful for generating sums or products, or collecting a single result.
It may seem like a lot to remember, but don’t worry, you can get by without knowing all of them. But as you start to learn and use the different methods, your code will become cleaner and clearer, and you’ll be on your way to Ruby mastery.
I’m not saying that Array
-> |value,index|
and Hash
-> |key,value|
is not insane (see Horace Loeb’s comment), but I am saying that there is a sane way to expect this arrangement.
When I am dealing with arrays, I am focused on the elements in the array (not the index because the index is transitory). The method is each with index, i.e. each+index, or |each,index|, or |value,index|
. This is also consistent with the index being viewed as an optional argument, e.g. |value| is equivalent to |value,index=nil| which is consistent with |value,index|.
When I am dealing with hashes, I am often more focused on the keys than the values, and I am usually dealing with keys and values in that order, either key => value
or hash[key] = value
.
If you want duck-typing, then either explicitly use a defined method as Brent Longborough showed, or an implicit method as maxhawkins showed.
Ruby is all about accommodating the language to suit the programmer, not about the programmer accommodating to suit the language. This is why there are so many ways. There are so many ways to think about something. In Ruby, you choose the closest and the rest of the code usually falls out extremely neatly and concisely.
As for the original question, “What is the “right” way to iterate through an array in Ruby?", well, I think the core way (i.e. without powerful syntactic sugar or object oriented power) is to do:
But Ruby is all about powerful syntactic sugar and object oriented power, but anyway here is the equivalent for hashes, and the keys can be ordered or not:
So, my answer is, “The “right” way to iterate through an array in Ruby depends on you (i.e. the programmer or the programming team) and the project.". The better Ruby programmer makes the better choice (of which syntactic power and/or which object oriented approach). The better Ruby programmer continues to look for more ways.
Now, I want to ask another question, “What is the “right” way to iterate through a Range in Ruby backwards?"! (This question is how I came to this page.)
It is nice to do (for the forwards):
but I don’t like to do (for the backwards):
Well, I don’t actually mind doing that too much, but when I am teaching going backwards, I want to show my students a nice symmetry (i.e. with minimal difference, e.g. only adding a reverse, or a step -1, but without modifying anything else). You can do (for symmetry):
and
which I don’t like much, but you can’t do
(*1..10).each{|i| puts "i=#{i}" }
(*1..10).reverse.each{|i| puts "i=#{i}" }
#
(1..10).step(1){|i| puts "i=#{i}" }
(1..10).step(-1){|i| puts "i=#{i}" }
#
(1..10).each{|i| puts "i=#{i}" }
(10..1).each{|i| puts "i=#{i}" } # I don't want this though. It's dangerous
You could ultimately do
but I want to teach pure Ruby rather than object oriented approaches (just yet). I would like to iterate backwards:
I believe this is impossible without defining a pred
method, which means modifying the Range class to use it. If you can do this please let me know, otherwise confirmation of impossibility would be appreciated though it would be disappointing. Perhaps Ruby 1.9 addresses this.
(Thanks for your time in reading this.)
How can I get the current absolute URL in my Ruby on Rails view?
The request.request_uri
only returns the relative URL.
You should use request.original_url
to get the current URL.
This method is documented at original_url method, but if you’re curious, the implementation is:
You can write "#{request.protocol}#{request.host_with_port}#{request.fullpath}"
, since request.url
is now deprecated.
You can write request.url
instead of request.request_uri
. This combines the protocol (usually http://) with the host, and request_uri to give you the full address.
I think that the Ruby on Rails 3.0 method is now request.fullpath
.
You could use url_for(only_path: false)
I’m trying to install a gem using gem install mygem
or update RubyGems using gem update --system
, and it fails with this error:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
Does anyone have an idea how to solve this?
You don’t have write permissions into the /Library/Ruby/Gems/1.8 directory.
means exactly that, you don’t have permission to write there.
That is the version of Ruby installed by Apple, for their own use. While it’s OK to make minor modifications to that if you know what you’re doing, because you are not sure about the permissions problem, I’d say it’s not a good idea to continue along that track.
Instead, I’ll strongly suggest you look into using either rbenv or RVM to manage a separate Ruby, installed into a sandbox in your home directory, that you can modify/fold/spindle/change without worrying about messing up the system Ruby.
Between the two, I use rbenv, though I used RVM a lot in the past. rbenv takes a more “hands-off” approach to managing your Ruby installation. RVM has a lot of features and is very powerful, but, as a result is more intrusive. In either case, READ the installation documentation for them a couple times before starting to install whichever you pick.
Try adding --user-install
instead of using sudo
:
You really should be using a Ruby version manager.
Using one properly would prevent and can resolve your permission problem when executing a gem update
command.
I recommend rbenv.
However, even when you use a Ruby version manager, you may still get that same error message.
If you do, and you are using rbenv, just verify that the ~/.rbenv/shims
directory is before the path for the system Ruby.
$ echo $PATH
will show you the order of your load path.
If you find that your shims directory comes after your system Ruby bin directory, then edit your ~/.bashrc
file and put this as your last export PATH command: export PATH=$HOME/.rbenv/shims:$PATH
$ ruby -v
shows you what version of Ruby you are using
This shows that I’m currently using the system version of Ruby (usually not good)
$ rbenv global 1.9.3-p448
switches me to a newer, pre-installed version (see references below).
This shows that I’m using a newer version of Ruby (that likely won’t cause the Gem::FilePermissionError)
You typically should not need to preface a gem command with sudo. If you feel the need to do so, something is probably misconfigured.
For details about rbenv see the following:
I am having a hard time understanding attr_accessor
in Ruby. Can someone explain this to me?
Let’s say you have a class Person
.
Obviously we never defined method name
. Let’s do that.
class Person
def name
@name # simply returning an instance variable @name
end
end
person = Person.new
person.name # => nil
person.name = "Dennis" # => no method error
Aha, we can read the name, but that doesn’t mean we can assign the name. Those are two different methods. The former is called reader and latter is called writer. We didn’t create the writer yet so let’s do that.
class Person
def name
@name
end
def name=(str)
@name = str
end
end
person = Person.new
person.name = 'Dennis'
person.name # => "Dennis"
Awesome. Now we can write and read instance variable @name
using reader and writer methods. Except, this is done so frequently, why waste time writing these methods every time? We can do it easier.
Even this can get repetitive. When you want both reader and writer just use accessor!
class Person
attr_accessor :name
end
person = Person.new
person.name = "Dennis"
person.name # => "Dennis"
Works the same way! And guess what: the instance variable @name
in our person object will be set just like when we did it manually, so you can use it in other methods.
class Person
attr_accessor :name
def greeting
"Hello #{@name}"
end
end
person = Person.new
person.name = "Dennis"
person.greeting # => "Hello Dennis"
That’s it. In order to understand how attr_reader
, attr_writer
, and attr_accessor
methods actually generate methods for you, read other answers, books, ruby docs.
attr_accessor is just a method. (The link should provide more insight with how it works - look at the pairs of methods generated, and a tutorial should show you how to use it.)
The trick is that class
is not a definition in Ruby (it is “just a definition” in languages like C++ and Java), but it is an expression that evaluates. It is during this evaluation when the attr_accessor
method is invoked which in turn modifies the current class - remember the implicit receiver: self.attr_accessor
, where self
is the “open” class object at this point.
The need for attr_accessor
and friends, is, well:
Ruby, like Smalltalk, does not allow instance variables to be accessed outside of methods1 for that object. That is, instance variables cannot be accessed in the x.y
form as is common in say, Java or even Python. In Ruby y
is always taken as a message to send (or “method to call”). Thus the attr_*
methods create wrappers which proxy the instance @variable
access through dynamically created methods.
Boilerplate sucks
Hope this clarifies some of the little details. Happy coding.
1 This isn’t strictly true and there are some “techniques” around this, but there is no syntax support for “public instance variable” access.
attr_accessor
is (as @pst stated) just a method. What it does is create more methods for you.
So this code here:
is equivalent to this code:
You can write this sort of method yourself in Ruby:
class Module
def var( method_name )
inst_variable_name = "@#{method_name}".to_sym
define_method method_name do
instance_variable_get inst_variable_name
end
define_method "#{method_name}=" do |new_value|
instance_variable_set inst_variable_name, new_value
end
end
end
class Foo
var :bar
end
f = Foo.new
p f.bar #=> nil
f.bar = 42
p f.bar #=> 42
I have a string that I want to parse in Ruby:
string = '{"desc":{"someKey":"someValue","anotherKey":"value"},"main_item":{"stats":{"a":8,"b":12,"c":10}}}'
Is there an easy way to extract the data?
This looks like JavaScript Object Notation (JSON). You can parse JSON that resides in some variable, e.g. json_string
, like so:
If you’re using an older Ruby, you may need to install the json gem.
There are also other implementations of JSON for Ruby that may fit some use-cases better:
Just to extend the answers a bit with what to do with the parsed object:
# JSON Parsing example
require "rubygems"
require "json"
string = '{"desc":{"someKey":"someValue","anotherKey":"value"},"main_item":{"stats":{"a":8,"b":12,"c":10}}}'
parsed = JSON.parse(string) # returns a hash
p parsed["desc"]["someKey"]
p parsed["main_item"]["stats"]["a"]
# Read JSON from a file, iterate over objects
file = open("shops.json")
json = file.read
parsed = JSON.parse(json)
parsed["shop"].each do |shop|
p shop["id"]
end
As of Ruby v1.9.3 you don’t need to install any Gems in order to parse JSON, simply use require 'json'
:
See JSON at Ruby-Doc.
I’ve been using Eclipse with RDT (not RadRails) a lot lately, and I’m quite happy with it, but I’m wondering if you guys know any decent alternatives. I know NetBeans also supports Ruby these days, but I’m not sure what it has to offer over Eclipse.
Please, list any features you think are brilliant or useful when suggesting an IDE, makes it easier to compare.
Also, I said Ruby, not Rails. While Rails support is a plus, I prefer things to be none Rails-centric. It should also be available on Linux and optionally Solaris.
Have you tried Aptana? It’s based on Eclipse and they have a sweet Rails plugin.
RubyMine from JetBrains. (Also available as a plugin to IntelliJ IDEA)
Redcar has been getting some attention lately, as well. Still early in its life, but it shows promise.
I am using the following code to check if a variable is not nil and not zero
Is there a better way to do this?
unless discount.nil? || discount == 0 # ... end
class Object
def nil_zero?
self.nil? || self == 0
end
end
# which lets you do
nil.nil_zero? # returns true
0.nil_zero? # returns true
1.nil_zero? # returns false
"a".nil_zero? # returns false
unless discount.nil_zero?
# do stuff...
end
Beware of the usual disclaimers… great power/responsibility, monkey patching leading to the dark side etc.
unless [nil, 0].include?(discount) # ... end
I have an array of integers.
For example:
Is there any nice way to get the sum of them?
I know, that
would work.
Try this:
See Ruby’s Enumerable Documentation
(note: the 0
base case is needed so that 0
will be returned on an empty array instead of nil
)
Or try the Ruby 1.9 way:
Note: the 0
base case is needed otherwise nil
will be returned on empty arrays:
While equivalent to array.inject(0, :+)
, the term reduce is entering a more common vernacular with the rise of MapReduce programming models.
inject, reduce, fold, accumulate, and compress are all synonymous as a class of folding functions. I find consistency across your code base most important, but since various communities tend to prefer one word over another, it’s nonetheless useful to know the alternatives.
To emphasize the map-reduce verbiage, here’s a version that is a little bit more forgiving on what ends up in that array.
Some additional relevant reading:
I’ll use python as an example of what I’m looking for (you can think of it as pseudocode if you don’t know Python):
I know in ruby I can do :
But is this the proper way to determine the type of the object?
The proper way to determine the “type” of an object, which is a wobbly term in the Ruby world, is to call object.class
.
Since classes can inherit from other classes, if you want to determine if an object is “of a particular type” you might call object.is_a?(ClassName)
to see if object
is of type ClassName
or derived from it.
Normally type checking is not done in Ruby, but instead objects are assessed based on their ability to respond to particular methods, commonly called “Duck typing”. In other words, if it responds to the methods you want, there’s no reason to be particular about the type.
For example, object.is_a?(String)
is too rigid since another class might implement methods that convert it into a string, or make it behave identically to how String behaves. object.respond_to?(:to_s)
would be a better way to test that the object in question does what you want.
you could also try: instance_of?
Oftentimes in Ruby, you don’t actually care what the object’s class is, per se, you just care that it responds to a certain method. This is known as Duck Typing and you’ll see it in all sorts of Ruby codebases.
So in many (if not most) cases, its best to use Duck Typing using #respond_to?(method)
:
I’m currently generating an 8-character pseudo-random uppercase string for “A” .. “Z”:
but it doesn’t look clean, and it can’t be passed as an argument since it isn’t a single statement. To get a mixed-case string “a” .. “z” plus “A” .. “Z”, I changed it to:
but it looks like trash.
Does anyone have a better method?
I spend too much time golfing.
And a last one that’s even more confusing, but more flexible and wastes fewer cycles:
Why not use SecureRandom?
require 'securerandom'
random_string = SecureRandom.hex
# outputs: 5b5cd0da3121fc53b4bc84d0c8af2e81 (i.e. 32 chars of 0..9, a..f)
SecureRandom also has methods for:
see: http://ruby-doc.org/stdlib-1.9.2/libdoc/securerandom/rdoc/SecureRandom.html
I use this for generating random URL friendly strings with a guaranteed maximum length:
It generates random strings of lowercase a-z and 0-9. It’s not very customizable but it’s short and clean.
I’ve recently started programming in Ruby, and I am looking at exception handling.
I was wondering if ensure
was the Ruby equivalent of finally
in C#? Should I have:
file = File.open("myFile.txt", "w")
begin
file << "#{content} \n"
rescue
#handle the error here
ensure
file.close unless file.nil?
end
or should I do this?
#store the file
file = File.open("myFile.txt", "w")
begin
file << "#{content} \n"
file.close
rescue
#handle the error here
ensure
file.close unless file.nil?
end
Does ensure
get called no matter what, even if an exception isn’t raised?
Yes, ensure
ensures that the code is always evaluated. That’s why it’s called ensure
. So, it is equivalent to Java’s and C#’s finally
.
The general flow of begin
/rescue
/else
/ensure
/end
looks like this:
begin
# something which might raise an exception
rescue SomeExceptionClass => some_variable
# code that deals with some exception
rescue SomeOtherException => some_other_variable
# code that deals with some other exception
else
# code that runs only if *no* exception was raised
ensure
# ensure that this code always runs, no matter what
# does not change the final value of the block
end
You can leave out rescue
, ensure
or else
. You can also leave out the variables in which case you won’t be able to inspect the exception in your exception handling code. (Well, you can always use the global exception variable to access the last exception that was raised, but that’s a little bit hacky.) And you can leave out the exception class, in which case all exceptions that inherit from StandardError
will be caught. (Please note that this does not mean that all exceptions are caught, because there are exceptions which are instances of Exception
but not StandardError
. Mostly very severe exceptions that compromise the integrity of the program such as SystemStackError
, NoMemoryError
, SecurityError
, NotImplementedError
, LoadError
, SyntaxError
, ScriptError
, Interrupt
, SignalException
or SystemExit
.)
Some blocks form implicit exception blocks. For example, method definitions are implicitly also exception blocks, so instead of writing
you write just
or
The same applies to class
definitions and module
definitions.
However, in the specific case you are asking about, there is actually a much better idiom. In general, when you work with some resource which you need to clean up at the end, you do that by passing a block to a method which does all the cleanup for you. It’s similar to a using
block in C#, except that Ruby is actually powerful enough that you don’t have to wait for the high priests of Microsoft to come down from the mountain and graciously change their compiler for you. In Ruby, you can just implement it yourself:
# This is what you want to do:
File.open('myFile.txt', 'w') do |file|
file.puts content
end
# And this is how you might implement it:
def File.open(filename, mode='r', perm=nil, opt=nil)
yield filehandle = new(filename, mode, perm, opt)
ensure
filehandle&.close
end
And what do you know: this is already available in the core library as File.open
. But it is a general pattern that you can use in your own code as well, for implementing any kind of resource cleanup (à la using
in C#) or transactions or whatever else you might think of.
The only case where this doesn’t work, if acquiring and releasing the resource are distributed over different parts of the program. But if it is localized, as in your example, then you can easily use these resource blocks.
BTW: in modern C#, using
is actually superfluous, because you can implement Ruby-style resource blocks yourself:
FYI, even if an exception is re-raised in the rescue
section, the ensure
block will be executed before the code execution continues to the next exception handler. For instance:
If you want to ensure a file is closed you should use the block form of File.open
:
File.open("myFile.txt", "w") do |file|
begin
file << "#{content} \n"
rescue
#handle the error here
end
end
How can I comment multiple lines in Ruby?
#!/usr/bin/env ruby
=begin
Every body mentioned this way
to have multiline comments.
The =begin and =end must be at the beginning of the line or
it will be a syntax error.
=end
puts "Hello world!"
<<-DOC
Also, you could create a docstring.
which...
DOC
puts "Hello world!"
"..is kinda ugly and creates
a String instance, but I know one guy
with a Smalltalk background, who
does this."
puts "Hello world!"
##
# most
# people
# do
# this
__END__
But all forgot there is another option.
Only at the end of a file, of course.
Despite the existence of =begin
and =end
, the normal and a more correct way to comment is to use #
’s on each line. If you read the source of any ruby library, you will see that this is the way multi-line comments are done in almost all cases.
Using the command-line gem
tool, how can I install a specific version of a gem?
Use the -v
flag:
Use the --version
parameter (shortcut -v
):
You can also use version comparators like >=
or ~>
Or with newer versions of gem even:
For installing gem install gemname -v versionnumber
For uninstall gem uninstall gemname -v versionnumber
I want to check whether the “user” key is present or not in the session hash. How can I do this?
Note that I don’t want to check whether the key’s value is nil or not. I just want to check whether the “user” key is present.
Hash
’s key?
method tells you whether a given key is present or not.
While Hash#has_key?
gets the job done, as Matz notes here, it has been deprecated in favour of Hash#key?
.
In latest Ruby versions Hash instance has a key?
method:
Be sure to use the symbol key or a string key depending on what you have in your hash:
To add a new pair to Hash I do:
Is there a similar way to delete a key from Hash ?
This works:
but I would expect to have something like:
It is important that the returning value will be the remaining hash, so I could do things like:
in one line.
Rails has an except/except! method that returns the hash with those keys removed. If you’re already using Rails, there’s no sense in creating your own version of this.
class Hash
# Returns a hash that includes everything but the given keys.
# hash = { a: true, b: false, c: nil}
# hash.except(:c) # => { a: true, b: false}
# hash # => { a: true, b: false, c: nil}
#
# This is useful for limiting a set of parameters to everything but a few known toggles:
# @person.update(params[:person].except(:admin))
def except(*keys)
dup.except!(*keys)
end
# Replaces the hash without the given keys.
# hash = { a: true, b: false, c: nil}
# hash.except!(:c) # => { a: true, b: false}
# hash # => { a: true, b: false }
def except!(*keys)
keys.each { |key| delete(key) }
self
end
end
Oneliner plain ruby, it works only with ruby > 1.9.x:
1.9.3p0 :002 > h = {:a => 1, :b => 2}
=> {:a=>1, :b=>2}
1.9.3p0 :003 > h.tap { |hs| hs.delete(:a) }
=> {:b=>2}
Tap method always return the object on which is invoked…
Otherwise if you have required active_support/core_ext/hash
(which is automatically required in every Rails application) you can use one of the following methods depending on your needs:
➜ ~ irb
1.9.3p125 :001 > require 'active_support/core_ext/hash' => true
1.9.3p125 :002 > h = {:a => 1, :b => 2, :c => 3}
=> {:a=>1, :b=>2, :c=>3}
1.9.3p125 :003 > h.except(:a)
=> {:b=>2, :c=>3}
1.9.3p125 :004 > h.slice(:a)
=> {:a=>1}
except uses a blacklist approach, so it removes all the keys listed as args, while slice uses a whitelist approach, so it removes all keys that aren’t listed as arguments. There also exist the bang version of those method (except!
and slice!
) which modify the given hash but their return value is different both of them return an hash. It represents the removed keys for slice!
and the keys that are kept for the except!
:
Why not just use:
somearray = ["some", "thing"]
anotherarray = ["another", "thing"]
somearray.push(anotherarray.flatten!)
I expected
You’ve got a workable idea, but the #flatten!
is in the wrong place – it flattens its receiver, so you could use it to turn [1, 2, ['foo', 'bar']]
into [1,2,'foo','bar']
.
I’m doubtless forgetting some approaches, but you can concatenate:
or prepend/append:
or splice:
or append and flatten:
You can just use the +
operator!
irb(main):001:0> a = [1,2]
=> [1, 2]
irb(main):002:0> b = [3,4]
=> [3, 4]
irb(main):003:0> a + b
=> [1, 2, 3, 4]
You can read all about the array class here: http://ruby-doc.org/core/classes/Array.html
What’s the syntax for dropping a database table column through a Rails migration?
For instance:
would remove the hobby Column from the users table.
For older versions of Rails
For Rails 3 and up
Rails 4 has been updated, so the change method can be used in the migration to drop a column and the migration will successfully rollback. Please read the following warning for Rails 3 applications:
Rails 3 Warning
Please note that when you use this command:
The generated migration will look something like this:
def up
remove_column :table_name, :field_name
end
def down
add_column :table_name, :field_name, :datatype
end
Make sure to not use the change method when removing columns from a database table (example of what you don’t want in the migration file in Rails 3 apps):
The change method in Rails 3 is not smart when it comes to remove_column, so you will not be able to rollback this migration.
I need to update my ruby version from 2.0.0 to the latest version, I can not use some gems because my version is not updated. I had used Homebrew to install Ruby some time ago, How can i update my Ruby version?
Open your terminal and run
curl -sSL https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer | bash -s stable
When this is complete, you need to restart your terminal for the rvm
command to work.
Now, run rvm list known
This shows the list of versions of the ruby.
Now, run rvm install ruby-2.4.2
If you type ruby -v
in the terminal, you should see ruby 2.4.2
.
If it still shows you ruby 2.0.
, run rvm use ruby-2.4.2 --default
.
Update:
From the comments (kudos to Maksim Luzik), I haven’t tested but seems like a more elegant solution:
After installing ruby through brew, run following command to update the links to the latest ruby installation: brew link --overwrite ruby
Original answer:
Late to the party, but using brew is enough. It’s not necessary to install rvm and for me it just complicated things.
By brew install ruby
you’re actually installing the latest (currently v2.4.0). However, your path finds 2.0.0 first. To avoid this just change precedence (source). I did this by changing ~/.profile
and setting:
export PATH=/usr/local/bin:$PATH
After this I found that bundler gem was still using version 2.0.0, just install it again: gem install bundler
I recommend rbenv* https://github.com/rbenv/rbenv
rbenv does…
- Provide support for specifying application-specific Ruby versions.
- Let you change the global Ruby version on a per-user basis.
- Allow you to override the Ruby version with an environment variable.
In contrast with RVM, rbenv does not…
- Need to be loaded into your shell. Instead, rbenv’s shim approach works by adding a directory to your
$PATH
.- Override shell commands like
cd
or require prompt hacks. That’s dangerous and error-prone.- Have a configuration file. There’s nothing to configure except which version of Ruby you want to use.
- Install Ruby. You can build and install Ruby yourself, or use ruby-build to automate the process.
- Manage gemsets. Bundler is a better way to manage application dependencies. If you have projects that are not yet using Bundler you can install the rbenv-gemset plugin.
- Require changes to Ruby libraries for compatibility. The simplicity of rbenv means as long as it’s in your
$PATH
, nothing else needs to know about it.
INSTALLATION
Install Homebrew http://brew.sh
Then:
$ brew update$ brew install rbenv$ brew install rbenv ruby-build # Add rbenv to bash so that it loads every time you open a terminal echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile source ~/.bash_profile
UPDATE
There’s one additional step afterbrew install rbenv
Runrbenv init
and add one line to.bash_profile
as it states. After that reopen your terminal window […] SGI Sep 30 at 12:01 —https://stackoverflow.com/users/119770
$ rbenv install --list Available versions: 1.8.5-p113 1.8.5-p114 […] 2.3.1 2.4.0-dev jruby-1.5.6 […] $ rbenv install 2.3.1 […]
Set the global version:
$ rbenv global 2.3.1 $ ruby -v ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
Set the local version of your repo by adding .ruby-version
to your repo’s root dir:
$ cd ~/whatevs/projects/new_repo $ echo "2.3.1" > .ruby-version
For MacOS visit this link
What are all the common ways to read a file in Ruby?
For instance, here is one method:
I know Ruby is extremely flexible. What are the benefits/drawbacks of each approach?
File.open("my/file/path", "r") do |f|
f.each_line do |line|
puts line
end
end
# File is closed automatically at end of block
It is also possible to explicitly close file after as above (pass a block to open
closes it for you):
The easiest way if the file isn’t too long is:
Indeed, IO.read
or File.read
automatically close the file, so there is no need to use File.open
with a block.
Be wary of “slurping” files. That’s when you read the entire file into memory at once.
The problem is that it doesn’t scale well. You could be developing code with a reasonably sized file, then put it into production and suddenly find you’re trying to read files measuring in gigabytes, and your host is freezing up as it tries to read and allocate memory.
Line-by-line I/O is very fast, and almost always as effective as slurping. It’s surprisingly fast actually.
I like to use:
or
File inherits from IO, and foreach
is in IO, so you can use either.
I have some benchmarks showing the impact of trying to read big files via read
vs. line-by-line I/O at “Why is”slurping" a file not a good practice?".
The difference between rake db:migrate
and rake db:reset
is pretty clear in my head. The thing which I don’t understand is how rake db:schema:load
different from the former two.
Just to be sure that I am on the same page:
rake db:migrate
- Runs the migrations which haven’t been run yet.
rake db:reset
- Clears the database (presumably does a rake db:drop
+ rake db:create
+ rake db:migrate
) and runs migration on a fresh database.
Please help to clarify, if my understanding has gone wrong.
db:schema:load creates tables and columns within the (existing) database following schema.rb
db:setup does db:create, db:schema:load, db:seed
Typically, you would use db:migrate after having made changes to the schema via new migration files (this makes sense only if there is already data in the database). db:schema:load is used when you setup a new instance of your app.
I hope that helps.
UPDATE for rails 3.2.12:
I just checked the source and the dependencies are like this now:
db:schema:dump dumps the current env’s schema (and seems to create the db as well)
db:setup runs db:schema:load, db:seed
For further information please have a look at https://github.com/rails/rails/blob/v3.2.12/activerecord/lib/active_record/railties/databases.rake (for Rails 3.2.x) and https://github.com/rails/rails/blob/v4.0.5/activerecord/lib/active_record/railties/databases.rake (for Rails 4.0.x)
Use
rake db:migrate
If you wanna make changes to the schema
rake db:reset
If you wanna drop the database, reload the schema from schema.rb
, and reseed the database
rake db:schema:load
If you wanna reset database to schema as provided in schema.rb
(This will delete all data)
rake db:schema:load
will set up the schema as provided in schema.rb
file. This is useful for a fresh install of app as it doesn’t take as much time as db:migrate
Important note, db:schema:load
will delete data on server.
rake db:migrate
makes changes to the existing schema. Its like creating versions of schema. db:migrate
will look in db/migrate/
for any ruby files and execute the migrations that aren’t run yet starting with the oldest. Rails knows which file is the oldest by looking at the timestamp at the beginning of the migration filename. db:migrate
comes with a benefit that data can also be put in the database. This is actually not a good practice. Its better to use rake db:seed
to add data.
rake db:migrate
provides tasks up, down etc which enables commands like rake db:rollback
and makes it the most useful command.
rake db:reset
does a db:drop
and db:setup
It drops the database, create it again, loads the schema, and initializes with the seed data
namespace :schema do
desc 'Creates a db/schema.rb file that is portable against any DB supported by Active Record'
task :dump => [:environment, :load_config] do
require 'active_record/schema_dumper'
filename = ENV['SCHEMA'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema.rb')
File.open(filename, "w:utf-8") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
db_namespace['schema:dump'].reenable
end
desc 'Loads a schema.rb file into the database'
task :load => [:environment, :load_config, :check_protected_environments] do
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV['SCHEMA'])
end
# desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
task :reset => [ 'db:drop', 'db:setup' ]
namespace :migrate do
# desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
task :redo => [:environment, :load_config] do
if ENV['VERSION']
db_namespace['migrate:down'].invoke
db_namespace['migrate:up'].invoke
else
db_namespace['rollback'].invoke
db_namespace['migrate'].invoke
end
end
As far as I understand, it is going to drop your database and re-create it based on your db/schema.rb
file. That is why you need to make sure that your schema.rb
file is always up to date and under version control.
How is the conditional operator (? :
) used in Ruby?
For example, is this correct?
It is the ternary operator, and it works like in C (the parenthesis are not required). It’s an expression that works like:
However, in Ruby, if
is also an expression so: if a then b else c end
=== a ? b : c
, except for precedence issues. Both are expressions.
Examples:
Note that in the first case parenthesis are required (otherwise Ruby is confused because it thinks it is puts if 1
with some extra junk after it), but they are not required in the last case as said issue does not arise.
You can use the “long-if” form for readability on multiple lines:
Your use of ERB suggests that you are in Rails. If so, then consider truncate
, a built-in helper which will do the job for you:
I’m looking at the documentation for FileUtils. I’m confused by the following line:
What does the %w
mean? Can you point me to the documentation?
%w(foo bar)
is a shortcut for ["foo", "bar"]
. Meaning it’s a notation to write an array of strings separated by spaces instead of commas and without quotes around them. You can find a list of ways of writing literals in zenspider’s quickref.
I think of %w()
as a “word array” - the elements are delimited by spaces and it returns an array of strings.
There are other % literals:
%r()
is another way to write a regular expression.
%q()
is another way to write a single-quoted string (and can be multi-line, which is useful)
%Q()
gives a double-quoted string
%x()
is a shell command
%i()
gives an array of symbols (Ruby >= 2.0.0)
%s()
turns foo
into a symbol (:foo
)
I don’t know any others, but there may be some lurking around in there…
There is also %s
that allows you to create any symbols, for example:
%s|some words| #Same as :'some words'
%s[other words] #Same as :'other words'
%s_last example_ #Same as :'last example'
Since Ruby 2.0.0 you also have:
%i( a b c ) # => [ :a, :b, :c ]
%i[ a b c ] # => [ :a, :b, :c ]
%i_ a b c _ # => [ :a, :b, :c ]
# etc...
How can I check whether a variable is defined in Ruby? Is there an isset
-type method available?
Use the defined?
keyword (documentation). It will return a String with the kind of the item, or nil
if it doesn’t exist.
>> a = 1
=> 1
>> defined? a
=> "local-variable"
>> defined? b
=> nil
>> defined? nil
=> "nil"
>> defined? String
=> "constant"
>> defined? 1
=> "expression"
As skalee commented: “It is worth noting that variable which is set to nil is initialized.”
This is useful if you want to do nothing if it does exist but create it if it doesn’t exist.
This only creates the new instance once. After that it just keeps returning the var.
The correct syntax for the above statement is:
if (defined?(var)).nil? # will now return true or false
print "var is not defined\n".color(:red)
else
print "var is defined\n".color(:green)
end
substituting (var
) with your variable. This syntax will return a true/false value for evaluation in the if statement.
I have a rake task that needs to insert a value into multiple databases.
I’d like to pass this value into the rake task from the command line, or from another rake task.
How can I do this?
options and dependencies need to be inside arrays:
namespace :thing do
desc "it does a thing"
task :work, [:option, :foo, :bar] do |task, args|
puts "work", args
end
task :another, [:option, :foo, :bar] do |task, args|
puts "another #{args}"
Rake::Task["thing:work"].invoke(args[:option], args[:foo], args[:bar])
# or splat the args
# Rake::Task["thing:work"].invoke(*args)
end
end
Then
rake thing:work[1,2,3]
=> work: {:option=>"1", :foo=>"2", :bar=>"3"}
rake thing:another[1,2,3]
=> another {:option=>"1", :foo=>"2", :bar=>"3"}
=> work: {:option=>"1", :foo=>"2", :bar=>"3"}
NOTE: variable task
is the the task object, not very helpful unless you know/care about Rake internals.
RAILS NOTE:
If running the task from rails, its best to preload the environment by adding => [:environment]
which is a way to setup dependent tasks.
You can specify formal arguments in rake by adding symbol arguments to the task call. For example:
require 'rake'
task :my_task, [:arg1, :arg2] do |t, args|
puts "Args were: #{args}"
end
task :invoke_my_task do
Rake.application.invoke_task("my_task[1, 2]")
end
# or if you prefer this syntax...
task :invoke_my_task_2 do
Rake::Task[:my_task].invoke(3, 4)
end
# a task with prerequisites passes its
# arguments to it prerequisites
task :with_prerequisite, [:arg1, :arg2] => :my_task #<- name of prerequisite task
# to specify default values,
# we take advantage of args being a Rake::TaskArguments object
task :with_defaults, :arg1, :arg2 do |t, args|
args.with_defaults(:arg1 => :default_1, :arg2 => :default_2)
puts "Args with defaults were: #{args}"
end
Then, from the command line:
> rake my_task[1,2] Args were: {:arg1=>"1", :arg2=>"2"} > rake "my_task[1, 2]" Args were: {:arg1=>"1", :arg2=>"2"} > rake invoke_my_task Args were: {:arg1=>"1", :arg2=>"2"} > rake invoke_my_task_2 Args were: {:arg1=>3, :arg2=>4} > rake with_prerequisite[5,6] Args were: {:arg1=>"5", :arg2=>"6"} > rake with_defaults Args with defaults were: {:arg1=>:default_1, :arg2=>:default_2} > rake with_defaults['x','y'] Args with defaults were: {:arg1=>"x", :arg2=>"y"}
As demonstrated in the second example, if you want to use spaces, the quotes around the target name are necessary to keep the shell from splitting up the arguments at the space.
Looking at the code in rake.rb, it appears that rake does not parse task strings to extract arguments for prerequisites, so you can’t do task :t1 => "dep[1,2]"
. The only way to specify different arguments for a prerequisite would be to invoke it explicitly within the dependent task action, as in :invoke_my_task
and :invoke_my_task_2
.
Note that some shells (like zsh) require you to escape the brackets: rake my_task\['arg1'\]
In addition to answer by kch (I didn’t find how to leave a comment to that, sorry):
You don’t have to specify variables as ENV
variables before the rake
command. You can just set them as usual command line parameters like that:
and access those from your rake file as ENV variables like such:
I have a string in Ruby:
How can I replace any one word in this sentence easily without using complex code or a loop?
You can try using this way :
Then the sentence will become :
Won’t cause an exception if the replaced word isn’t in the sentence (the []=
variant will).
The above replaces only the first instance of “Robert”.
To replace all instances use gsub
/gsub!
(ie. “global substitution”):
The above will replace all instances of Robert with Joe.
If you’re dealing with natural language text and need to replace a word, not just part of a string, you have to add a pinch of regular expressions to your gsub as a plain text substitution can lead to disastrous results:
Regular expressions have word boundaries, such as \b
which matches start or end of a word. Thus,
In Ruby, unlike some other languages like Javascript, word boundaries are UTF-8-compatible, so you can use it for languages with non-Latin or extended Latin alphabets:
I have an array of elements in Ruby
I need to remove elements with value 3
for example
How do I do that?
I think I’ve figured it out:
Borrowing from Travis in the comments, this is a better answer:
I personally like[1, 2, 7, 4, 5] - [7]
which results in=> [1, 2, 4, 5]
fromirb
I modified his answer seeing that 3 was the third element in his example array. This could lead to some confusion for those who don’t realize that 3 is in position 2 in the array.
Another option:
which results in
Is there a way to make this look a little better?
conn.exec 'select attr1, attr2, attr3, attr4, attr5, attr6, attr7 ' +
'from table1, table2, table3, etc, etc, etc, etc, etc, ' +
'where etc etc etc etc etc etc etc etc etc etc etc etc etc'
Like, is there a way to imply concatenation?
There are pieces to this answer that helped me get what I needed (easy multi-line concatenation WITHOUT extra whitespace), but since none of the actual answers had it, I’m compiling them here:
str = 'this is a multi-line string'\
' using implicit concatenation'\
' to prevent spare \n\'s'
=> "this is a multi-line string using implicit concatenation to eliminate spare
\\n's"
As a bonus, here’s a version using funny HEREDOC syntax (via this link):
p <<END_SQL.gsub(/\s+/, " ").strip
SELECT * FROM users
ORDER BY users.id DESC
END_SQL
# >> "SELECT * FROM users ORDER BY users.id DESC"
The latter would mostly be for situations that required more flexibility in the processing. I personally don’t like it, it puts the processing in a weird place w.r.t. the string (i.e., in front of it, but using instance methods that usually come afterward), but it’s there. Note that if you are indenting the last END_SQL
identifier (which is common, since this is probably inside a function or module), you will need to use the hyphenated syntax (that is, p <<-END_SQL
instead of p <<END_SQL
). Otherwise, the indenting whitespace causes the identifier to be interpreted as a continuation of the string.
This doesn’t save much typing, but it looks nicer than using + signs, to me.
EDIT: Adding one more:
Yes, if you don’t mind the extra newlines being inserted:
conn.exec 'select attr1, attr2, attr3, attr4, attr5, attr6, attr7
from table1, table2, table3, etc, etc, etc, etc, etc,
where etc etc etc etc etc etc etc etc etc etc etc etc etc'
Alternatively you can use a heredoc:
In ruby 2.0 you can now just use %
For example:
I’ve set everything up that I need on my Mac (Ruby, Rails, Homebrew, Git, etc), and I’ve even written a small program. Now, how do I execute it in Terminal? I wrote the program in Redcar and saved it as a .rb, but I don’t know how to execute it through Terminal. I want to run the program and see if it actually works. How do I do this?
Just call: ruby your_program.rb
or
#!/usr/bin/env ruby
,
chmod +x your_program.rb
./your_program.rb some_param
Open your terminal and open folder where file is saved.
Ex /home/User1/program/test.rb
cd /home/User1/program
ruby test.rb
format or test.rb
output
If I download a .gem file to a folder in my computer, can I install it later using gem install
?
Yup, when you do gem install
, it will search the current directory first, so if your .gem file is there, it will pick it up. I found it on the gem reference, which you may find handy as well:
gem install will install the named gem. It will attempt a local installation (i.e. a .gem file in the current directory), and if that fails, it will attempt to download and install the most recent version of the gem you want.
Also, you can use gem install --local path_to_gem/filename.gem
This will skip the usual gem repository scan that happens when you leave off --local
.
You can find other magic with gem install --help
.
you can also use the full filename to your gem file:
this works as well – it’s probably the easiest way
I’m new to programming. Can someone explain what .map
would do in:
The map
method takes an enumerable object and a block, and runs the block for each element, outputting each returned value from the block (the original object is unchanged unless you use map!)
:
Array
and Range
are enumerable types. map
with a block returns an Array. map!
mutates the original array.
Where is this helpful, and what is the difference between map!
and each
? Here is an example:
names = ['danil', 'edmund']
# here we map one array to another, convert each element by some rule
names.map! {|name| name.capitalize } # now names contains ['Danil', 'Edmund']
names.each { |name| puts name + ' is a programmer' } # here we just do something with each element
The output:
map
, along with select
and each
is one of Ruby’s workhorses in my code.
It allows you to run an operation on each of your array’s objects and return them all in the same place. An example would be to increment an array of numbers by one:
If you can run a single method on your array’s elements you can do it in a shorthand-style like so:
To do this with the above example you’d have to do something like this
class Numeric
def plusone
self + 1
end
end
[1,2,3].map(&:plusone)
#=> [2,3,4]
```</li>
<li><p>To more simply use the ampersand shortcut technique, let's use a different example:</p>
```ruby
["vanessa", "david", "thomas"].map(&:upcase)
#=> ["VANESSA", "DAVID", "THOMAS"]
```</li>
</ol>
Transforming data in Ruby often involves a cascade of `map` operations. Study `map` & `select`, they are some of the most useful Ruby methods in the primary library. They're just as important as `each`.
(`map` is also an alias for `collect`. Use whatever works best for you conceptually.)
<strong>More helpful information:</strong>
If the <a href="http://ruby-doc.org/core-2.2.3/Enumerable.html" rel="noreferrer">Enumerable</a> object you're running `each` or `map` on contains a set of Enumerable elements (hashes, arrays), you can declare each of those elements inside your block pipes like so:
```ruby
[["audi", "black", 2008], ["bmw", "red", 2014]].each do |make, color, year|
puts "make: #{make}, color: #{color}, year: #{year}"
end
# Output:
# make: audi, color: black, year: 2008
# make: bmw, color: red, year: 2014
In the case of a Hash (also an Enumerable
object, a Hash is simply an array of tuples with special instructions for the interpreter). The first “pipe parameter” is the key, the second is the value.
{:make => "audi", :color => "black", :year => 2008}.each do |k,v|
puts "#{k} is #{v}"
end
#make is audi
#color is black
#year is 2008
To answer the actual question:
Assuming that params
is a hash, this would be the best way to map through it: Use two block parameters instead of one to capture the key & value pair for each interpreted tuple in the hash.
Using ruby 2.4 you can do the same thing using transform_values
, this feature extracted from rails to ruby.
I have a string
"1,2,3,4"
and I’d like to convert it into an array:
How?
Or for integers:
Or for later versions of ruby (>= 1.9 - as pointed out by Alex):
"1,2,3,4".split(",")
as strings
"1,2,3,4".split(",").map { |s| s.to_i }
as integers
String Integer
without space as String
String Integer
with space as String
String Integer
without space as Integer
Explanation:
arr
-> string which you’re going to perform any action.
split()
-> is an method, which split the input and store it as array.
''
or ' '
or ','
-> is an value, which is needed to be removed from given string.
Here is Bar#do_things
:
class Bar
def do_things
Foo.some_method(x) do |x|
y = x.do_something
return y_is_bad if y.bad? # how do i tell it to stop and return do_things?
y.do_something_else
end
keep_doing_more_things
end
end
And here is Foo#some_method
:
class Foo
def self.some_method(targets, &block)
targets.each do |target|
begin
r = yield(target)
rescue
failed << target
end
end
end
end
I thought about using raise, but I am trying to make it generic, so I don’t want to put anything any specific in Foo
.
Use the keyword next
. If you do not want to continue to the next item, use break
.
When next
is used within a block, it causes the block to exit immediately, returning control to the iterator method, which may then begin a new iteration by invoking the block again:
f.each do |line| # Iterate over the lines in file f
next if line[0,1] == "#" # If this line is a comment, go to the next
puts eval(line)
end
When used in a block, break
transfers control out of the block, out of the iterator that invoked the block, and to the first expression following the invocation of the iterator:
f.each do |line| # Iterate over the lines in file f
break if line == "quit\n" # If this break statement is executed...
puts eval(line)
end
puts "Good bye" # ...then control is transferred here
And finally, the usage of return
in a block:
return
always causes the enclosing method to return, regardless of how deeply nested within blocks it is (except in the case of lambdas):
I wanted to just be able to break out of a block - sort of like a forward goto, not really related to a loop. In fact, I want to break of of a block that is in a loop without terminating the loop. To do that, I made the block a one-iteration loop:
for b in 1..2 do
puts b
begin
puts 'want this to run'
break
puts 'but not this'
end while false
puts 'also want this to run'
end
Hope this helps the next googler that lands here based on the subject line.
If you want your block to return a useful value (e.g. when using #map
, #inject
, etc.), next
and break
also accept an argument.
Consider the following:
def contrived_example(numbers)
numbers.inject(0) do |count, x|
if x % 3 == 0
count + 2
elsif x.odd?
count + 1
else
count
end
end
end
The equivalent using next
:
def contrived_example(numbers)
numbers.inject(0) do |count, x|
next count if x.even?
next (count + 2) if x % 3 == 0
count + 1
end
end
Of course, you could always extract the logic needed into a method and call that from inside your block:
def contrived_example(numbers)
numbers.inject(0) { |count, x| count + extracted_logic(x) }
end
def extracted_logic(x)
return 0 if x.even?
return 2 if x % 3 == 0
1
end
I find myself repeatedly looking for a clear definition of the differences of nil?
, blank?
, and empty?
in Ruby on Rails. Here’s the closest I’ve come:
blank?
objects are false, empty, or a whitespace string. For example, ""
, " "
, nil
, []
, and {}
are blank.
nil?
objects are instances of NilClass.
empty?
objects are class-specific, and the definition varies from class to class. A string is empty if it has no characters, and an array is empty if it contains no items.
Is there anything missing, or a tighter comparison that can be made?
.nil?
can be used on any object and is true if the object is nil.
.empty?
can be used on strings, arrays and hashes and returns true if:
Running .empty?
on something that is nil will throw a NoMethodError
.
That is where .blank?
comes in. It is implemented by Rails and will operate on any object as well as work like .empty?
on strings, arrays and hashes.
nil.blank? == true
false.blank? == true
[].blank? == true
{}.blank? == true
"".blank? == true
5.blank? == false
0.blank? == false
.blank?
also evaluates true on strings which are non-empty but contain only whitespace:
Rails also provides .present?
, which returns the negation of .blank?
.
Array gotcha: blank?
will return false
even if all elements of an array are blank. To determine blankness in this case, use all?
with blank?
, for example:
I made this useful table with all the cases:
blank?
, present?
are provided by Rails.
Just extend Julian’s table:
Ruby has this handy and convenient way to share instance variables by using keys like
Why would I choose attr_reader
or attr_writer
if I could simply use attr_accessor
? Is there something like performance (which I doubt)? I guess there is a reason, otherwise they wouldn’t have made such keys.
You may use the different accessors to communicate your intent to someone reading your code, and make it easier to write classes which will work correctly no matter how their public API is called.
Here, I can see that I may both read and write the age.
Here, I can see that I may only read the age. Imagine that it is set by the constructor of this class and after that remains constant. If there were a mutator (writer) for age and the class were written assuming that age, once set, does not change, then a bug could result from code calling that mutator.
But what is happening behind the scenes?
If you write:
That gets translated into:
If you write:
That gets translated into:
If you write:
That gets translated into:
Knowing that, here’s another way to think about it: If you did not have the attr_… helpers, and had to write the accessors yourself, would you write any more accessors than your class needed? For example, if age only needed to be read, would you also write a method allowing it to be written?
All of the answers above are correct; attr_reader
and attr_writer
are more convenient to write than manually typing the methods they are shorthands for. Apart from that they offer much better performance than writing the method definition yourself. For more info see slide 152 onwards from this talk (PDF) by Aaron Patterson.
Not all attributes of an object are meant to be directly set from outside the class. Having writers for all your instance variables is generally a sign of weak encapsulation and a warning that you’re introducing too much coupling between your classes.
As a practical example: I wrote a design program where you put items inside containers. The item had attr_reader :container
, but it didn’t make sense to offer a writer, since the only time the item’s container should change is when it’s placed in a new one, which also requires positioning information.
How do you tell a Ruby program to wait an arbitrary amount of time before moving on to the next line of code?
Like this:
The num_secs
value can be an integer or float.
Also, if you’re writing this within a Rails app, or have included the ActiveSupport library in your project, you can construct longer intervals using the following convenience syntax:
Use sleep like so:
That’ll sleep for 2 seconds.
Be careful to give an argument. If you just run sleep
, the process will sleep forever. (This is useful when you want a thread to sleep until it’s woken.)
I find until
very useful with sleep. example:
I was trying to use the following code to read lines from a file. But when reading a file, the contents are all in one line:
But this file prints each line separately.
I have to use stdin, like ruby my_prog.rb < file.txt
, where I can’t assume what the line-ending character is that the file uses. How can I handle it?
I believe my answer covers your new concerns about handling any type of line endings since both "\r\n"
and "\r"
are converted to Linux standard "\n"
before parsing the lines.
To support the "\r"
EOL character along with the regular "\n"
, and "\r\n"
from Windows, here’s what I would do:
line_num=0
text=File.open('xxx.txt').read
text.gsub!(/\r\n?/, "\n")
text.each_line do |line|
print "#{line_num += 1} #{line}"
end
Of course this could be a bad idea on very large files since it means loading the whole file into memory.
Ruby does have a method for this:
This will execute the given block for each line in the file without slurping the entire file into memory. See: IO::foreach.
Where can I find a list of data types that can be used in Ruby on Rails 4? Such as
text
string
integer
float
date
I keep learning about new ones and I’d love to have a list I could easily refer to.
Here are all the Rails 4 (ActiveRecord migration) datatypes:
:binary
:boolean
:date
:datetime
:decimal
:float
:integer
:bigint
:primary_key
:references
:string
:text
:time
:timestamp
Source: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_column
These are the same as with Rails 3.
If you use PostgreSQL, you can also take advantage of these:
:hstore
:json
:jsonb
:array
:cidr_address
:ip_address
:mac_address
They are stored as strings if you run your app with a not-PostgreSQL database.
Edit, 2016-Sep-19:
There’s a lot more postgres specific datatypes in Rails 4 and even more in Rails 5.
You might also find it useful to know generally what these data types are used for:
:string
- is for small data types such as a title. (Should you choose string or text?)
:text
- is for longer pieces of textual data, such as a paragraph of information
:binary
- is for storing data such as images, audio, or movies.
:boolean
- is for storing true or false values.
:date
- store only the date
:datetime
- store the date and time into a column.
:time
- is for time only
:timestamp
- for storing date and time into a column.(What’s the difference between datetime and timestamp?)
:decimal
- is for decimals (example of how to use decimals).
:float
- is for decimals. (What’s the difference between decimal and float?)
:integer
- is for whole numbers.
:primary_key
- unique key that can uniquely identify each row in a table
There’s also references used to create associations. But, I’m not sure this is an actual data type.
New Rails 4 datatypes available in PostgreSQL:
:hstore
- storing key/value pairs within a single value (learn more about this new data type)
:array
- an arrangement of numbers or strings in a particular row (learn more about it and see examples)
:cidr_address
- used for IPv4 or IPv6 host addresses
:inet_address
- used for IPv4 or IPv6 host addresses, same as cidr_address but it also accepts values with nonzero bits to the right of the netmask
:mac_address
- used for MAC host addresses
Learn more about the address datatypes here and here.
Also, here’s the official guide on migrations: http://edgeguides.rubyonrails.org/migrations.html
It is important to know not only the types but the mapping of these types to the database types, too:
Source added - Agile Web Development with Rails 4
Possible Duplicate:
How to remove rvm (ruby version manager) from my system?
How can I uninstall (or reinstall) RVM on Ubuntu 9.10? I messed up my current installation.
It’s easy; just do the following:
or
And don’t forget to remove the script calls in the following files:
~/.bashrc
~/.bash_profile
~/.profile
And maybe others depending on whatever shell you’re using.
rescue Exception => e
in Ruby? (score 276074 in 2017)Ryan Davis’s Ruby QuickRef says (without explanation):
Don’t rescue Exception. EVER. or I will stab you.
Why not? What’s the right thing to do?
TL;DR: Use StandardError
instead for general exception catching. When the original exception is re-raised (e.g. when rescuing to log the exception only), rescuing Exception
is probably okay.
Exception
is the root of Ruby’s exception hierarchy, so when you rescue Exception
you rescue from everything, including subclasses such as SyntaxError
, LoadError
, and Interrupt
.
Rescuing Interrupt
prevents the user from using CTRLC to exit the program.
Rescuing SignalException
prevents the program from responding correctly to signals. It will be unkillable except by kill -9
.
Rescuing SyntaxError
means that eval
s that fail will do so silently.
All of these can be shown by running this program, and trying to CTRLC or kill
it:
loop do
begin
sleep 1
eval "djsakru3924r9eiuorwju3498 += 5u84fior8u8t4ruyf8ihiure"
rescue Exception
puts "I refuse to fail or be stopped!"
end
end
Rescuing from Exception
isn’t even the default. Doing
does not rescue from Exception
, it rescues from StandardError
. You should generally specify something more specific than the default StandardError
, but rescuing from Exception
broadens the scope rather than narrowing it, and can have catastrophic results and make bug-hunting extremely difficult.
If you have a situation where you do want to rescue from StandardError
and you need a variable with the exception, you can use this form:
which is equivalent to:
One of the few common cases where it’s sane to rescue from Exception
is for logging/reporting purposes, in which case you should immediately re-raise the exception:
The real rule is: Don’t throw away exceptions. The objectivity of the author of your quote is questionable, as evidenced by the fact that it ends with
or I will stab you
Of course, be aware that signals (by default) throw exceptions, and normally long-running processes are terminated through a signal, so catching Exception and not terminating on signal exceptions will make your program very hard to stop. So don’t do this:
#! /usr/bin/ruby
while true do
begin
line = STDIN.gets
# heavy processing
rescue Exception => e
puts "caught exception #{e}! ohnoes!"
end
end
No, really, don’t do it. Don’t even run that to see if it works.
However, say you have a threaded server and you want all exceptions to not:
thread.abort_on_exception = true
).
Then this is perfectly acceptable in your connection handling thread:
begin
# do stuff
rescue Exception => e
myLogger.error("uncaught #{e} exception while handling connection: #{e.message}")
myLogger.error("Stack trace: #{backtrace.map {|l| " #{l}\n"}.join}")
end
The above works out to a variation of Ruby’s default exception handler, with the advantage that it doesn’t also kill your program. Rails does this in its request handler.
Signal exceptions are raised in the main thread. Background threads won’t get them, so there is no point in trying to catch them there.
This is particularly useful in a production environment, where you do not want your program to simply stop whenever something goes wrong. Then you can take the stack dumps in your logs and add to your code to deal with specific exception further down the call chain and in a more graceful manner.
Note also that there is another Ruby idiom which has much the same effect:
In this line, if do_something
raises an exception, it is caught by Ruby, thrown away, and a
is assigned "something else"
.
Generally, don’t do that, except in special cases where you know you don’t need to worry. One example:
The debugger
function is a rather nice way to set a breakpoint in your code, but if running outside a debugger, and Rails, it raises an exception. Now theoretically you shouldn’t be leaving debug code lying around in your program (pff! nobody does that!) but you might want to keep it there for a while for some reason, but not continually run your debugger.
Note:
If you’ve run someone else’s program that catches signal exceptions and ignores them, (say the code above) then:
pgrep ruby
, or ps | grep ruby
, look for your offending program’s PID, and then run kill -9 <PID>
.
If you are working with someone else’s program which is, for whatever reason, peppered with these ignore-exception blocks, then putting this at the top of the mainline is one possible cop-out:
This causes the program to respond to the normal termination signals by immediately terminating, bypassing exception handlers, with no cleanup. So it could cause data loss or similar. Be careful!If you need to do this:
you can actually do this:
In the second case,critical cleanup
will be called every time, whether or not an exception is thrown.
Let’s say you are in a car (running Ruby). You recently installed a new steering wheel with the over-the-air upgrade system (which uses eval
), but you didn’t know one of the programmers messed up on syntax.
You are on a bridge, and realize you are going a bit towards the railing, so you turn left.
oops! That’s probably Not Good™, luckily, Ruby raises a SyntaxError
.
The car should stop immediately - right?
Nope.
begin
#...
eval self.steering_wheel
#...
rescue Exception => e
self.beep
self.log "Caught #{e}.", :warn
self.log "Logged Error - Continuing Process.", :info
end
beep beep
Warning: Caught SyntaxError Exception.
Info: Logged Error - Continuing Process.
You notice something is wrong, and you slam on the emergency breaks (^C
: Interrupt
)
beep beep
Warning: Caught Interrupt Exception.
Info: Logged Error - Continuing Process.
Yeah - that didn’t help much. You’re pretty close to the rail, so you put the car in park (kill
ing: SignalException
).
beep beep
Warning: Caught SignalException Exception.
Info: Logged Error - Continuing Process.
At the last second, you pull out the keys (kill -9
), and the car stops, you slam forward into the steering wheel (the airbag can’t inflate because you didn’t gracefully stop the program - you terminated it), and the computer in the back of your car slams into the seat in front of it. A half-full can of Coke spills over the papers. The groceries in the back are crushed, and most are covered in egg yolk and milk. The car needs serious repair and cleaning. (Data Loss)
Hopefully you have insurance (Backups). Oh yeah - because the airbag didn’t inflate, you’re probably hurt (getting fired, etc).
But wait! There’s more reasons why you might want to use rescue Exception => e
!
Let’s say you’re that car, and you want to make sure the airbag inflates if the car is exceeding its safe stopping momentum.
begin
# do driving stuff
rescue Exception => e
self.airbags.inflate if self.exceeding_safe_stopping_momentum?
raise
end
Here’s the exception to the rule: You can catch Exception
only if you re-raise the exception. So, a better rule is to never swallow Exception
, and always re-raise the error.
But adding rescue is both easy to forget in a language like Ruby, and putting a rescue statement right before re-raising an issue feels a little non-DRY. And you do not want to forget the raise
statement. And if you do, good luck trying to find that error.
Thankfully, Ruby is awesome, you can just use the ensure
keyword, which makes sure the code runs. The ensure
keyword will run the code no matter what - if an exception is thrown, if one isn’t, the only exception being if the world ends (or other unlikely events).
Boom! And that code should run anyways. The only reason you should use rescue Exception => e
is if you need access to the exception, or if you only want code to run on an exception. And remember to re-raise the error. Every time.
Note: As @Niall pointed out, ensure always runs. This is good because sometimes your program can lie to you and not throw exceptions, even when issues occur. With critical tasks, like inflating airbags, you need to make sure it happens no matter what. Because of this, checking every time the car stops, whether an exception is thrown or not, is a good idea. Even though inflating airbags is a bit of an uncommon task in most programming contexts, this is actually pretty common with most cleanup tasks.
Don’t rescue Exception => e
(and not re-raise the exception) - or you might drive off a bridge.
When I attempt to install the latest version of compass (https://rubygems.org/gems/compass/versions/1.0.0.alpha.17), I get the following error.
ERROR: Error installing compass:
ERROR: Failed to build gem native extension.
ERROR: Error installing compass:
ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
checking for ffi.h... no
checking for ffi.h in /usr/local/include,/usr/include/ffi... yes
checking for ffi_call() in -lffi... yes
checking for ffi_prep_closure()... yes
checking for ffi_raw_call()... no
checking for rb_thread_blocking_region()... yes
checking for rb_thread_call_with_gvl()... yes
checking for rb_thread_call_without_gvl()... yes
checking for ffi_prep_cif_var()... no
creating extconf.h
creating Makefile
make "DESTDIR=" clean
make "DESTDIR="
compiling AbstractMemory.c
compiling ArrayType.c
compiling Buffer.c
compiling Call.c
Call.c:303:5: warning: implicit declaration of function 'rb_thread_call_without_gvl' is invalid in C99 [-Wimplicit-function-declaration]
rbffi_thread_blocking_region(call_blocking_function, data, (void *) -1, NULL);
^
./Thread.h:78:39: note: expanded from macro 'rbffi_thread_blocking_region'
# define rbffi_thread_blocking_region rb_thread_call_without_gvl
^
1 warning generated.
compiling ClosurePool.c
compiling DataConverter.c
DataConverter.c:43:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
1 warning generated.
compiling DynamicLibrary.c
compiling ffi.c
compiling Function.c
Function.c:479:33: warning: incompatible pointer types passing 'VALUE (void *)' to parameter of type 'void *(*)(void *)' [-Wincompatible-pointer-types]
rb_thread_call_with_gvl(callback_with_gvl, &cb);
^~~~~~~~~~~~~~~~~
Function.c:102:46: note: passing argument to parameter 'func' here
extern void *rb_thread_call_with_gvl(void *(*func)(void *), void *data1);
^
Function.c:563:9: warning: implicit declaration of function 'rb_thread_call_without_gvl' is invalid in C99 [-Wimplicit-function-declaration]
rb_thread_call_without_gvl(async_cb_wait, &w, async_cb_stop, &w);
^
Function.c:738:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
3 warnings generated.
compiling FunctionInfo.c
compiling LastError.c
compiling LongDouble.c
compiling MappedType.c
compiling MemoryPointer.c
compiling MethodHandle.c
compiling Platform.c
compiling Pointer.c
compiling Struct.c
compiling StructByReference.c
compiling StructByValue.c
compiling StructLayout.c
compiling Thread.c
compiling Type.c
compiling Types.c
compiling Variadic.c
linking shared-object ffi_c.bundle
clang: error: unknown argument: '-multiply_definedsuppress' [-Wunused-command-line-argument-hard-error-in-future]
clang: note: this will be a hard error (cannot be downgraded to a warning) in the future
make: *** [ffi_c.bundle] Error 1
make failed, exit code 2
Gem files will remain installed in /Library/Ruby/Gems/2.0.0/gems/ffi-1.9.3 for inspection.
Results logged to /Library/Ruby/Gems/2.0.0/extensions/universal-darwin-13/2.0.0/ffi-1.9.3/gem_make.out
What’s going on here? How do I install the latest compass without error?
Try this, then try to install compass again
In order to install compass On Mac OS X 10.10 (Yosemite)had to perform the following:
1. Set Up Ruby Environment
ruby -v
sudo gem update --system
2. Set Up MAC Environment
Install the Xcode Command Line Tools this is the key to install Compass.
Installing the Xcode Command Line Tools are the key to getting Compass working on OS X
3. Install Compass
I want to get a specific output iterating a Ruby Hash.
This is the Hash I want to iterate over:
This is the output I would like to get:
In Ruby, how can I get such an output with my Hash ?
Regarding order I should add, that in 1.8 the items will be iterated in random order (well, actually in an order defined by Fixnum’s hashing function), while in 1.9 it will be iterated in the order of the literal.
The most basic way to iterate over a hash is as follows:
I’m looking for a simple way to parse JSON, extract a value and write it into a database in Rails.
Specifically what I’m looking for, is a way to extract shortUrl
from the JSON returned from the bit.ly API:
{
"errorCode": 0,
"errorMessage": "",
"results":
{
"http://www.foo.com":
{
"hash": "e5TEd",
"shortKeywordUrl": "",
"shortUrl": "http://bit.ly/1a0p8G",
"userHash": "1a0p8G"
}
},
"statusCode": "OK"
}
And then take that shortUrl and write it into an ActiveRecord object associated with the long URL.
This is one of those things that I can think through entirely in concept and when I sit down to execute I realize I’ve got a lot to learn.
These answers are a bit dated. Therefore I give you:
Rails should automagically load the json
module for you, so you don’t need to add require 'json'
.
Parsing JSON in Rails is quite straightforward:
Let’s suppose, the object you want to associate the shortUrl with is a Site object, which has two attributes - short_url and long_url. Than, to get the shortUrl and associate it with the appropriate Site object, you can do something like:
parsed_json["results"].each do |longUrl, convertedUrl|
site = Site.find_by_long_url(longUrl)
site.short_url = convertedUrl["shortUrl"]
site.save
end
For example in this line of code I wrote, print
and puts
produce different results.
puts
adds a new line to the end of each argument if there is not one already.
print
does not add a new line.
For example:
puts [[1,2,3], [4,5,nil]]
Would return:
1 2 3 4 5
Whereas print [[1,2,3], [4,5,nil]]
would return:
[[1,2,3], [4,5,nil]]
Notice how puts does not output the nil value whereas print does.
A big difference is if you are displaying arrays. Especially ones with NIL. For example:
gives
but
gives
Note, no appearing nil item (just a blank line) and each item on a different line.
print
outputs each argument, followed by $,
, to $stdout
, followed by $\
. It is equivalent to args.join($,) + $\
puts
sets both $,
and $\
to “” and then does the same thing as print
. The key difference being that each argument is a new line with puts
.
You can require 'english'
to access those global variables with user-friendly names.
I’m new to Ruby and don’t know how to add new item to already existing hash. For example, first I construct hash:
after that a want to add item2 so after this I have hash like this:
I don’t know what method to do on hash, could someone help me?
Create the hash:
Add a new item to it:
If you want to add new items from another hash - use merge
method:
hash = {:item1 => 1}
another_hash = {:item2 => 2, :item3 => 3}
hash.merge(another_hash) # {:item1=>1, :item2=>2, :item3=>3}
In your specific case it could be:
but it’s not wise to use it when you should to add just one element more.
Pay attention that merge
will replace the values with the existing keys:
exactly like hash[:item1] = 2
Also you should pay attention that merge
method (of course) doesn’t effect the original value of hash variable - it returns a new merged hash. If you want to replace the value of the hash variable then use merge!
instead:
hash.store(key, value) - Stores a key-value pair in hash.
Example:
hash #=> {"a"=>9, "b"=>200, "c"=>4}
hash.store("d", 42) #=> 42
hash #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}
I would like my JSON output in Ruby on Rails to be “pretty” or nicely formatted.
Right now, I call to_json
and my JSON is all on one line. At times this can be difficult to see if there is a problem in the JSON output stream.
Is there way to configure or a method to make my JSON “pretty” or nicely formatted in Rails?
Use the pretty_generate()
function, built into later versions of JSON. For example:
require 'json'
my_object = { :array => [1, 2, 3, { :sample => "hash"} ], :foo => "bar" }
puts JSON.pretty_generate(my_object)
Which gets you:
Thanks to Rack Middleware and Rails 3 you can output pretty JSON for every request without changing any controller of your app. I have written such middleware snippet and I get nicely printed JSON in browser and curl
output.
class PrettyJsonResponse
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
if headers["Content-Type"] =~ /^application\/json/
obj = JSON.parse(response.body)
pretty_str = JSON.pretty_unparse(obj)
response = [pretty_str]
headers["Content-Length"] = pretty_str.bytesize.to_s
end
[status, headers, response]
end
end
The above code should be placed in app/middleware/pretty_json_response.rb
of your Rails project. And the final step is to register the middleware in config/environments/development.rb
:
I don’t recommend to use it in production.rb
. The JSON reparsing may degrade response time and throughput of your production app. Eventually extra logic such as ‘X-Pretty-Json: true’ header may be introduced to trigger formatting for manual curl requests on demand.
(Tested with Rails 3.2.8-5.0.0, Ruby 1.9.3-2.2.0, Linux)
The <pre>
tag in HTML, used with JSON.pretty_generate
, will render the JSON pretty in your view. I was so happy when my illustrious boss showed me this:
How to convert a ruby hash object to JSON? So I am trying this example below & it doesn’t work?
I was looking at the RubyDoc and obviously Hash
object doesn’t have a to_json
method. But I am reading on blogs that Rails supports active_record.to_json
and also supports hash#to_json
. I can understand ActiveRecord
is a Rails object, but Hash
is not native to Rails, it’s a pure Ruby object. So in Rails you can do a hash.to_json
, but not in pure Ruby??
One of the numerous niceties of Ruby is the possibility to extend existing classes with your own methods. That’s called “class reopening” or monkey-patching (the meaning of the latter can vary, though).
So, take a look here:
car = {:make => "bmw", :year => "2003"}
# => {:make=>"bmw", :year=>"2003"}
car.to_json
# NoMethodError: undefined method `to_json' for {:make=>"bmw", :year=>"2003"}:Hash
# from (irb):11
# from /usr/bin/irb:12:in `<main>'
require 'json'
# => true
car.to_json
# => "{"make":"bmw","year":"2003"}"
As you can see, requiring json
has magically brought method to_json
to our Hash
.
You can also use JSON.generate
:
Or its alias, JSON.unparse
:
I’m certainly no Ruby developer but I have an application on my server using Ruby, Gems, and Bundler. I am trying to install another Ruby on under a different user account but on the same VPS. When I go to run
I get the following error:
Could not locate Gemfile
I could remove the contents of the ./bundle directory so that all Gems are re-fetched to clear the error but will this have an impact on my other application using the same Gems and Bundler? I don’t want to risk taking the other app down.
You do not have Gemfile
in a directory where you run that command. Gemfile
is a file containing your gem
settings for a current program.
Make sure you are in the project directory before running bundle install
. For example, after running rails new myproject
, you will want to cd myproject
before running bundle install
.
I had the same problem and got it solved by using a different directory.
bash-4.2$ bundle install Could not locate Gemfile bash-4.2$ pwd /home/amit/redmine/redmine-2.2.2-0/apps/redmine bash-4.2$ cd htdocs/ bash-4.2$ ls app config db extra Gemfile lib plugins Rakefile script tmp bin config.ru doc files Gemfile.lock log public README.rdoc test vendor bash-4.2$ cd plugins/ bash-4.2$ bundle install Using rake (0.9.2.2) Using i18n (0.6.0) Using multi_json (1.3.6) Using activesupport (3.2.11) Using builder (3.0.0) Using activemodel (3.2.11) Using erubis (2.7.0) Using journey (1.0.4) Using rack (1.4.1) Using rack-cache (1.2) Using rack-test (0.6.1) Using hike (1.2.1) Using tilt (1.3.3) Using sprockets (2.2.1) Using actionpack (3.2.11) Using mime-types (1.19) Using polyglot (0.3.3) Using treetop (1.4.10) Using mail (2.4.4) Using actionmailer (3.2.11) Using arel (3.0.2) Using tzinfo (0.3.33) Using activerecord (3.2.11) Using activeresource (3.2.11) Using coderay (1.0.6) Using rack-ssl (1.3.2) Using json (1.7.5) Using rdoc (3.12) Using thor (0.15.4) Using railties (3.2.11) Using jquery-rails (2.0.3) Using mysql2 (0.3.11) Using net-ldap (0.3.1) Using ruby-openid (2.1.8) Using rack-openid (1.3.1) Using bundler (1.2.3) Using rails (3.2.11) Using rmagick (2.13.1) Your bundle i
I have several versions of a Ruby gem:
How can I remove old versions but keep the most recent?
For removing older versions of all installed gems, following 2 commands are useful:
Above command will preview what gems are going to be removed.
Above command will actually remove them.
Try something like gem uninstall rjb --version 1.3.4
.
I’ve installed RubyInstaller on Windows and I’m running IMAP Sync but I need to use it to sync hundreds of accounts. If I could pass these variables to it via command line I could automate the whole process better.
# Source server connection info.
SOURCE_NAME = 'username@example.com'
SOURCE_HOST = 'mail.example.com'
SOURCE_PORT = 143
SOURCE_SSL = false
SOURCE_USER = 'username'
SOURCE_PASS = 'password'
# Destination server connection info.
DEST_NAME = 'username@gmail.com'
DEST_HOST = 'imap.gmail.com'
DEST_PORT = 993
DEST_SSL = true
DEST_USER = 'username@gmail.com'
DEST_PASS = 'password'
Something like this:
then
or
Don’t reinvent the wheel; check out Ruby’s way-cool OptionParser library.
It offers parsing of flags/switches, parameters with optional or required values, can parse lists of parameters into a single option and can generate your help for you.
Also, if any of your information being passed in is pretty static, that doesn’t change between runs, put it into a YAML file that gets parsed. That way you can have things that change every time on the command-line, and things that change occasionally configured outside your code. That separation of data and code is nice for maintenance.
Here are some samples to play with:
require 'optparse'
require 'yaml'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on('-n', '--sourcename NAME', 'Source name') { |v| options[:source_name] = v }
opts.on('-h', '--sourcehost HOST', 'Source host') { |v| options[:source_host] = v }
opts.on('-p', '--sourceport PORT', 'Source port') { |v| options[:source_port] = v }
end.parse!
dest_options = YAML.load_file('destination_config.yaml')
puts dest_options['dest_name']
This is a sample YAML file if your destinations are pretty static:
---
dest_name: username@gmail.com
dest_host: imap.gmail.com
dest_port: 993
dest_ssl: true
dest_user: username@gmail.com
dest_pass: password
This will let you easily generate a YAML file:
Unfortunately, Ruby does not support such passing mechanism as e.g. AWK:
It means you cannot pass named values into your script directly.
Using cmd options may help:
> ruby script.rb val_0 val_1 val_2
# script.rb
puts ARGV[0] # => val_0
puts ARGV[1] # => val_1
puts ARGV[2] # => val_2
Ruby stores all cmd arguments in the ARGV
array, the scriptname itself can be captured using the $PROGRAM_NAME
variable.
The obvious disadvantage is that you depend on the order of values.
If you need only Boolean switches use the option -s
of the Ruby interpreter:
Please note the --
switch, otherwise Ruby will complain about a nonexistent option -agreed
, so pass it as a switch to your cmd invokation. You don’t need it in the following case:
The disadvantage is that you mess with global variables and have only logical true/false values.
You can access values from environment variables:
Drawbacks are present here to, you have to set all the variables before the script invocation (only for your ruby process) or to export them (shells like BASH):
In the latter case, your data will be readable for everybody in the same shell session and for all subprocesses, which can be a serious security implication.
And at least you can implement an option parser using getoptlong and optparse.
Happy hacking!
I’m using this code to let the user enter in names while the program stores them in an array until they enter an empty string (they must press enter after each name):
people = []
info = 'a' # must fill variable with something, otherwise loop won't execute
while not info.empty?
info = gets.chomp
people += [Person.new(info)] if not info.empty?
end
This code would look much nicer in a do … while loop:
people = []
do
info = gets.chomp
people += [Person.new(info)] if not info.empty?
while not info.empty?
In this code I don’t have to assign info to some random string.
Unfortunately this type of loop doesn’t seem to exist in Ruby. Can anybody suggest a better way of doing this?
CAUTION:
The begin <code> end while <condition>
is rejected by Ruby’s author Matz. Instead he suggests using Kernel#loop
, e.g.
Here’s an email exchange in 23 Nov 2005 where Matz states:
|> Don't use it please. I'm regretting this feature, and I'd like to
|> remove it in the future if it's possible.
|
|I'm surprised. What do you regret about it?
Because it's hard for users to tell
begin <code> end while <cond>
works differently from
<code> while <cond>
RosettaCode wiki has a similar story:
During November 2005, Yukihiro Matsumoto, the creator of Ruby, regretted this loop feature and suggested using Kernel#loop.
I found the following snippet while reading the source for
Tempfile#initialize
in the Ruby core library:begin tmpname = File.join(tmpdir, make_tmpname(basename, n)) lock = tmpname + '.lock' n += 1 end while @@cleanlist.include?(tmpname) or File.exist?(lock) or File.exist?(tmpname)
At first glance, I assumed the while modifier would be evaluated before the contents of begin…end, but that is not the case. Observe:
As you would expect, the loop will continue to execute while the modifier is true.
While I would be happy to never see this idiom again, begin…end is quite powerful. The following is a common idiom to memoize a one-liner method with no params:
Here is an ugly, but quick way to memoize something more complex:
Originally written by Jeremy Voorhis. The content has been copied here because it seems to have been taken down from the originating site. Copies can also be found in the Web Archive and at Ruby Buzz Forum. -Bill the Lizard
Like this:
people = []
begin
info = gets.chomp
people += [Person.new(info)] if not info.empty?
end while not info.empty?
Reference: Ruby’s Hidden do {} while () Loop
I am having some problems when trying to install mysql2
gem for Rails. When I try to install it by running bundle install
or gem install mysql2
it gives me the following error:
Error installing mysql2: ERROR: Failed to build gem native extension.
How can I fix this and successfully install mysql2
?
On Ubuntu/Debian and other distributions using aptitude:
Package libmysql-ruby
has been phased out and replaced by ruby-mysql
. This is where I found the solution.
If the above command doesn’t work because libmysql-ruby
cannot be found, the following should be sufficient:
On Red Hat/CentOS and other distributions using yum:
On Mac OS X with Homebrew:
I’m on a mac and use homebrew to install open source programs. I did have to install mac Dev tools in order to install homebrew, but after that it was a simple:
to install mysql. I haven’t had a mysql gem problem since.
here is a solution for the windows users, hope it helps!
Install railsinstaller -> www.railsinstaller.org (I installed it to c:)
Install MySQL (I used MySQL 5.5) -> dev.mysql.com/downloads/installer/
— for mySQL installation —
If you dont already have these two files installed you might need them to get your MySQL going
vcredist_x86.exe -> http://www.microsoft.com/download/en/details.aspx?id=5555 dotNetFx40_Full_x86_x64.exe -> http://www.microsoft.com/download/en/details.aspx?id=17718
Use default install Developer Machine-MySQL Server Config-
— for mySQL installation —
port: 3306
windows service name: MySQL55
mysql root pass: root (you can change this later)
(username: root)
-MySQL Server Config-
— Install the mysql2 Gem —
Important: Do this with Git Bash Command Line(this was installed with railsinstaller) -> start/Git Bash
gem install mysql2 – ’–with-mysql-lib=“c:FilesServer 5.5” –with-mysql-include="c:FilesServer 5.5 "’
Now the gem should have installed correctly
Lastly copy the libmysql.dll file from
C:FilesServer 5.5
to
C:.2
— Install the mysql2 Gem —
You will now be able to use your Rails app with MySQL, if you are not sure how to create a Rails 3 app with MySQL read on…
— Get a Rails 3 app going with MySQL —
Open command prompt(not Git Bash) -> start/cmd
Navigate to your folder (c:)
Create new rails app
Delete the file c:.html
Edit the file c:.rb
add this line -> root :to => ‘cities#index’
Open command prompt (generate views and controllers)
rails generate scaffold city ID:integer Name:string CountryCode:string District:string Population:integer
Edit the file c:.rb to look like this
Edit the file c:.yml to look like this
development:
adapter: mysql2
encoding: utf8
database: world
pool: 5
username: root
password: root
socket: /tmp/mysql.sock
add to gemfile
Open command prompt windows cmd, not Git Bash(run your app!)
Navigate to your app folder (c:)
Open your browser here -> http://localhost:3000
— Get a Rails 3 app going with MySQL —
I am using Authlogic-Connect for third party logins. After running appropriate migrations, Twitter/Google/yahoo logins seem to work fine but the facebook login throws exception:
The dev log shows
OpenSSL::SSL::SSLError (SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed):
app/controllers/users_controller.rb:37:in `update'
Please suggest..
I ran into a similar problem when trying to use the JQuery generator for Rails 3
I solved it like this:
Get the CURL Certificate Authority (CA) bundle. You can do this with:
sudo port install curl-ca-bundle
[if you are using MacPorts]
wget http://curl.haxx.se/ca/cacert.pem
Execute the ruby code that is trying to verify the SSL certification: SSL_CERT_FILE=/opt/local/etc/certs/cacert.pem rails generate jquery:install
. In your case, you want to either set this as an environment variable somewhere the server picks it up or add something like ENV['SSL_CERT_FILE'] = /path/to/your/new/cacert.pem
in your environment.rb file.
You can also just install the CA files (I haven’t tried this) to the OS – there are lengthy instructions here – this should work in a similar fashion, but I have not tried this personally.
Basically, the issue you are hitting is that some web service is responding with a certificate signed against a CA that OpenSSL cannot verify.
If you’re using RVM on OS X, you probably need to run this:
More information here: http://rvm.io/support/fixing-broken-ssl-certificates
And here is the full explanation: https://github.com/wayneeseguin/rvm/blob/master/help/osx-ssl-certs.md
On Ruby 2.2, you may have to reinstall Ruby from source to fix this. Here’s how (replace 2.2.3
with your Ruby version):
Credit to https://stackoverflow.com/a/32363597/4353 and Ian Connor.
I have a ruby array like ['12','34','35','231']
.
I want to convert it to a string like '12','34','35','231'
.
How can I do that?
I’ll join the fun with:
EDIT:
Some string interpolation to add the first and last single quote :P
try this code ['12','34','35','231']*","
will give you result “12,34,35,231”
I hope this is the result you, let me know
I want to know if there is a much cleaner way of doing this. Basically, I want to pick a random element from an array of variable length. Normally, I would do it like this:
myArray = ["stuff", "widget", "ruby", "goodies", "java", "emerald", "etc" ]
item = myArray[rand(myarray.length)]
Is there something that is more readable / simpler to replace the second line? Or is that the best way to do it. I suppose you could do myArray.shuffle.first
, but I only saw #shuffle
a few minutes ago on SO, I haven’t actually used it yet.
Just use Array#sample
:
It is available in Ruby 1.9.1+. To be also able to use it with an earlier version of Ruby, you could require "backports/1.9.1/array/sample"
.
Note that in Ruby 1.8.7 it exists under the unfortunate name choice
; it was renamed in later version so you shouldn’t use that.
Although not useful in this case, sample
accepts a number argument in case you want a number of distinct samples.
myArray.sample(x)
can also help you to get x random elements from the array.
If I call a command using Kernel#system in Ruby, how do I get its output?
I’d like to expand & clarify chaos’s answer a bit.
If you surround your command with backticks, then you don’t need to (explicitly) call system() at all. The backticks execute the command and return the output as a string. You can then assign the value to a variable like so:
or
Be aware that all the solutions where you pass a string containing user provided values to system
, %x[]
etc. are unsafe! Unsafe actually means: the user may trigger code to run in the context and with all permissions of the program.
As far as I can say only system
and Open3.popen3
do provide a secure/escaping variant in Ruby 1.8. In Ruby 1.9 IO::popen
also accepts an array.
Simply pass every option and argument as an array to one of these calls.
If you need not just the exit status but also the result you probably want to use Open3.popen3
:
require 'open3'
stdin, stdout, stderr, wait_thr = Open3.popen3('usermod', '-p', @options['shadow'], @options['username'])
stdout.gets(nil)
stdout.close
stderr.gets(nil)
stderr.close
exit_code = wait_thr.value
Note that the block form will auto-close stdin, stdout and stderr- otherwise they’d have to be closed explicitly.
More information here: Forming sanitary shell commands or system calls in Ruby
Just for the record, if you want both (output and operation result) you can do:
I would like to take information from another website. Therefore (maybe) I should make a request to that website (in my case a HTTP GET request) and receive the response.
How can I make this in Ruby on Rails?
If it is possible, is it a correct approach to use in my controllers?
You can use Ruby’s Net::HTTP
class:
Net::HTTP is built into Ruby, but let’s face it, often it’s easier not to use its cumbersome 1980s style and try a higher level alternative:
OpenURI is the best; it’s as simple as
I was thinking of something like this?
But it doesn’t work. Ruby has the ||=
but I"m not even sure how to use that thing.
Remove if
from if @item.rigged ? "Yes" : "No"
Ternary operator has form condition ? if_true : if_false
In Ruby, the condition and the then
part of an if
expression must be separated by either an expression separator (i.e. ;
or a newline) or the then
keyword.
So, all of these would work:
if @item.rigged then 'Yes' else 'No' end
if @item.rigged; 'Yes' else 'No' end
if @item.rigged
'Yes' else 'No' end
There is also a conditional operator in Ruby, but that is completely unnecessary. The conditional operator is needed in C, because it is an operator: in C, if
is a statement and thus cannot return a value, so if you want to return a value, you need to use something which can return a value. And the only things in C that can return a value are functions and operators, and since it is impossible to make if
a function in C, you need an operator.
In Ruby, however, if
is an expression. In fact, everything is an expression in Ruby, so it already can return a value. There is no need for the conditional operator to even exist, let alone use it.
BTW: it is customary to name methods which are used to ask a question with a question mark at the end, like this:
This shows another problem with using the conditional operator in Ruby:
It’s simply hard to read with the multiple question marks that close to each other.
One line if:
Your case:
I don’t use the RI or RDoc output from the gems I install in my machine or in the servers I handle (I use other means of documentation).
Every gem I install installs RI and RDoc documentation by default, because I forget to set --no-ri --no-rdoc
.
Is there a way to make those two flags the default?
You just add following line to your local ~/.gemrc
file (it is in your home folder)
or you can add this line to the global gemrc config file. Here is how to find it (in Linux)
From RVM’s documentation:
Just add this line to your~/.gemrc
or/etc/gemrc
:
Note: The original answer was:
This is no longer valid; the RVM docs have since been updated, thus the current answer to only include the gem
directive is the correct one.
Note that --no-ri
and --no-rdoc
have been deprecated according to the new guides. The recommended way is to use --no-document
in ~/.gemrc
or /etc/gemrc
.
or
The firewall I’m behind is running Microsoft ISA server in NTLM-only mode. Hash anyone have success getting their Ruby gems to install/update via Ruby SSPI gem or other method?
… or am I just being lazy?
Note: rubysspi-1.2.4 does not work.
This also works for “igem”, part of the IronRuby project
I wasn’t able to get mine working from the command-line switch but I have been able to do it just by setting my HTTP_PROXY
environment variable. (Note that case seems to be important). I have a batch file that has a line like this in it:
I set the four referenced variables before I get to this line obviously. As an example if my username is “wolfbyte”, my password is “secret” and my proxy is called “pigsy” and operates on port 8080:
You might want to be careful how you manage that because it stores your password in plain text in the machine’s session but I don’t think it should be too much of an issue.
For the Windows OS, I used Fiddler to work around the issue.
How can I get the class name from an ActiveRecord object?
I have:
I tried:
I need only the class name, in a string (User
in this case). Is there a method for that?
I know this is pretty basic, but I searched both Rails’ and Ruby’s docs, and I couldn’t find it.
You want to call .name on the object’s class:
Here’s the correct answer, extracted from comments by Daniel Rikowski and pseidemann. I’m tired of having to weed through comments to find the right answer…
If you use Rails (ActiveSupport):
If you use POR (plain-ol-Ruby):
Both result.class.to_s
and result.class.name
work.
I want to do something simple and straightforward, like min(5,10)
, or Math.max(4,7)
. Are there functions to this effect in Ruby?
You can do
or
They come from the Enumerable module, so anything that includes Enumerable
will have those methods available.
v2.4 introduces own Array#min
and Array#max
, which are way faster than Enumerable’s methods because they skip calling #each
.
EDIT
@nicholasklick mentions another option, Enumerable#minmax
, but this time returning an array of [min, max]
.
You can use
or
It’s a method for Arrays.
All those results generate garbage in a zealous attempt to handle more than two arguments. I’d be curious to see how they perform compared to good ’ol:
which is by-the-way my official answer to your question. :)
I am using Fedora 14 and I have MySQL and MySQL server 5.1.42 installed and running. Now I tried to do this as root user:
But I get this error:
Building native extensions. This could take a while...
ERROR: Error installing mysql:
ERROR: Failed to build gem native extension.
/usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/ruby.h
Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/mysql-2.8.1 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/mysql-2.8.1/ext/mysql_api/gem_make.out
What’s wrong here? In installed ruby 1.8.7. and the latest rubygems 1.3.7.
For those who may be confused by the accepted answer, as I was, you also need to have the ruby headers installed [ruby-devel].
The article that saved my hide is here.
And this is the revised solution (note that I’m on Fedora 13):
yum -y install gcc mysql-devel ruby-devel rubygems
gem install -y mysql -- --with-mysql-config=/usr/bin/mysql_config
For Debian, and other distributions using Debian style packaging the ruby development headers are installed by:
For Ubuntu the ruby development headers are installed by:
If you are using a earlier version of ruby (such as 2.2), then you will need to run:
(where 2.2 is your desired Ruby version)
Red Hat, Fedora:
yum -y install gcc mysql-devel ruby-devel rubygems
gem install -y mysql -- --with-mysql-config=/usr/bin/mysql_config
Debian, Ubuntu:
Arch Linux:
For anyone reading this in 2015: if you happened to install the package ruby2.0
, you need to install the matching ruby2.0-dev
to get the appropriate Ruby headers. The same goes for ruby2.1
and ruby2.2
, etc. For example:
I just had a quick question regarding loops in Ruby. Is there a difference between these two ways of iterating through a collection?
# way 1
@collection.each do |item|
# do whatever
end
# way 2
for item in @collection
# do whatever
end
Just wondering if these are exactly the same or if maybe there’s a subtle difference (possibly when @collection
is nil).
This is the only difference:
each:
irb> [1,2,3].each { |x| }
=> [1, 2, 3]
irb> x
NameError: undefined local variable or method `x' for main:Object
from (irb):2
from :0
for:
With the for
loop, the iterator variable still lives after the block is done. With the each
loop, it doesn’t, unless it was already defined as a local variable before the loop started.
Other than that, for
is just syntax sugar for the each
method.
When @collection
is nil
both loops throw an exception:
Exception: undefined local variable or method `@collection’ for main:Object
See “The Evils of the For Loop” for a good explanation (there’s one small difference considering variable scoping).
Using each
is considered more idiomatic use of Ruby.
Your first example,
is more idiomatic. While Ruby supports looping constructs like for
and while
, the block syntax is generally preferred.
Another subtle difference is that any variable you declare within a for
loop will be available outside the loop, whereas those within an iterator block are effectively private.
In C and many other languages, there is a continue
keyword that, when used inside of a loop, jumps to the next iteration of the loop. Is there any equivalent of this continue
keyword in Ruby?
Yes, it’s called next
.
This outputs the following:
next
also, look at redo
which redoes the current iteration.
Writing Ian Purton’s answer in a slightly more idiomatic way:
Prints:
My question is similar to “What is the difference between include and extend in Ruby?”.
What’s the difference between require
and include
in Ruby? If I just want to use the methods from a module in my class, should I require
it or include
it?
What’s the difference between “include” and “require” in Ruby?
Answer:
The include and require methods do very different things.
The require method does what include does in most other programming languages: run another file. It also tracks what you’ve required in the past and won’t require the same file twice. To run another file without this added functionality, you can use the load method.
The include method takes all the methods from another module and includes them into the current module. This is a language-level thing as opposed to a file-level thing as with require. The include method is the primary way to “extend” classes with other modules (usually referred to as mix-ins). For example, if your class defines the method “each”, you can include the mixin module Enumerable and it can act as a collection. This can be confusing as the include verb is used very differently in other languages.
So if you just want to use a module, rather than extend it or do a mix-in, then you’ll want to use require
.
Oddly enough, Ruby’s require
is analogous to C’s include
, while Ruby’s include
is almost nothing like C’s include
.
From the Metaprogramming Ruby book,
The
require()
method is quite similar toload()
, but it’s meant for a different purpose. You useload()
to execute code, and you userequire()
to import libraries.
If you’re using a module, that means you’re bringing all the methods into your class. If you extend
a class with a module, that means you’re “bringing in” the module’s methods as class methods. If you include
a class with a module, that means you’re “bringing in” the module’s methods as instance methods.
EX:
B.say
=> undefined method ‘say’ for B:Class
B.new.say
=> this is module A
C.say
=> this is module A
C.new.say
=> undefined method ‘say’ for C:Class
Is it bad to check if an array is not empty by using any?
method?
Or is it better to use unless a.empty?
?
any?
isn’t the same as not empty?
in some cases.
From the documentation:
If the block is not given, Ruby adds an implicit block of {|obj| obj} (that is any? will return true if at least one of the collection members is not false or nil).
The method empty?
comes from the Array class
http://ruby-doc.org/core-2.0.0/Array.html#method-i-empty-3F
Its used to check if the array contains something or not. This includes things that evaluate to false such as nil and false.
>> a = []
=> []
>> a.empty?
=> true
>> a = [nil, false]
=> [nil, false]
>> a.empty?
=> false
>> a = [nil]
=> [nil]
>> a.empty?
=> false
The method any?
comes from the Enumerable module.
http://ruby-doc.org/core-2.0.0/Enumerable.html#method-i-any-3F
Its used to evaluate if “any” values in the array evaluates to true. Similar methods to this are none? all? and one? where they all just check to see how many times true could be evaluated. which has nothing to do with the count of values found in a array.
case 1
case 2
>> a = [nil, true]
=> [nil, true]
>> a.any?
=> true
>> a.one?
=> true
>> a.all?
=> false
>> a.none?
=> false
case 3
Prefixing the statement with an exclamation mark will let you know whether the array is not empty. So in your case -
I want to get all file names from a folder using Ruby.
You also have the shortcut option of
and if you want to find all Ruby files in any folder or sub-folder:
example:
Source: http://ruby-doc.org/core/classes/Dir.html#method-c-entries
The following snippets exactly shows the name of the files inside a directory, skipping subdirectories and "."
, ".."
dotted folders:
Every time I run my rails 4.0 server, I get this output.
Started GET "/" for 127.0.0.1 at 2013-11-06 23:56:36 -0500
PG::ConnectionBad - could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (fe80::1) and accepting
TCP/IP connections on port 5432?
:
activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `connect'
activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:542:in `initialize'
activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
activerecord (4.0.0) lib/active_record/connection_handling.rb:79:in `retrieve_connection'
activerecord (4.0.0) lib/active_record/connection_handling.rb:53:in `connection'
activerecord (4.0.0) lib/active_record/migration.rb:792:in `current_version'
activerecord (4.0.0) lib/active_record/migration.rb:800:in `needs_migration?'
activerecord (4.0.0) lib/active_record/migration.rb:379:in `check_pending!'
activerecord (4.0.0) lib/active_record/migration.rb:366:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__1613334440513032208__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
better_errors (0.9.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (0.9.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (0.9.0) lib/better_errors/middleware.rb:56:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.1) lib/thin/connection.rb:54:in `process'
thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
thin (1.5.1) lib/thin/server.rb:159:in `start'
rack (1.5.2) lib/rack/handler/thin.rb:16:in `run'
rack (1.5.2) lib/rack/server.rb:264:in `start'
railties (4.0.0) lib/rails/commands/server.rb:84:in `start'
railties (4.0.0) lib/rails/commands.rb:78:in `block in <top (required)>'
railties (4.0.0) lib/rails/commands.rb:73:in `<top (required)>'
bin/rails:4:in `<main>'
I’m running Mavericks OS X 10.9 so I don’t know if that’s the problem. I’ve tried everything I could but nothing seems to work. I’ve uninstalled and install both postgres and the pg gem multiple times now.
This is my database.yml file
development:
adapter: postgresql
encoding: unicode
database: metals-directory_development
pool: 5
username:
password:
template: template0
host: localhost
port: 5432
test: &test
adapter: postgresql
encoding: unicode
database: metals-directory_test
pool: 5
username:
password:
template: template0
host: localhost
port: 5432
staging:
adapter: postgresql
encoding: unicode
database: metals-directory_production
pool: 5
username:
password:
template: template0
host: localhost
production:
adapter: postgresql
encoding: unicode
database: metals-directory_production
pool: 5
username:
password:
template: template0
host: localhost
cucumber:
<<: *test
It could be as simple as a stale PID file. It could be failing silently because your computer didn’t complete the shutdown process completely which means postgres didn’t delete the PID (process id) file.
The PID file is used by postgres to make sure only one instance of the server is running at a time. So when it goes to start again, it fails because there is already a PID file which tells postgres that another instance of the server was started (even though it isn’t running, it just didn’t get to shutdown and delete the PID).
/usr/local/var/postgres/
, or /usr/local/var/log/
other systems it might be /usr/var/postgres/
.
server.log
). On the last lines you will see:
FATAL: lock file “postmaster.pid” already exists
HINT: Is another postmaster (PID 347) running in data directory “/usr/local/var/postgres”?
rm postmaster.pid
Restart your server. On a mac using launchctl (with homebrew) the following commands will restart the server.
OR on newer versions of Brew
brew services restart postgresql
```</li>
</ol>
#### Answer 2 (score 41)
After a lot of searching and analysis, I found a solution if you are using ubuntu just write this command in your terminal and hit enter
```ruby
sudo service postgresql restart
This will restart your PostgreSQL, hope this would behelp for you.
I have managed to solve the problem by following the Chris Slade’s answer, but to restart the server, I had to use the following commands:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
that I found here (pjammer’s answer down at the bottom)
I’d like to ‘fake’ a 404 page in Rails. In PHP, I would just send a header with the error code as such:
How is that done with Rails?
Don’t render 404 yourself, there’s no reason to; Rails has this functionality built in already. If you want to show a 404 page, create a render_404
method (or not_found
as I called it) in ApplicationController
like this:
Rails also handles AbstractController::ActionNotFound
, and ActiveRecord::RecordNotFound
the same way.
This does two things better:
rescue_from
handler to render the 404 page, andwithout having to write ugly conditional statements.
As a bonus, it’s also super easy to handle in tests. For example, in an rspec integration test:
# RSpec 1
lambda {
visit '/something/you/want/to/404'
}.should raise_error(ActionController::RoutingError)
# RSpec 2+
expect {
get '/something/you/want/to/404'
}.to raise_error(ActionController::RoutingError)
And minitest:
OR refer more info from Rails render 404 not found from a controller action
To return a 404 header, just use the :status
option for the render method.
If you want to render the standard 404 page you can extract the feature in a method.
def render_404
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found }
format.xml { head :not_found }
format.any { head :not_found }
end
end
and call it in your action
If you want the action to render the error page and stop, simply use a return statement.
def action
render_404 and return if params[:something].blank?
# here the code that will never be executed
end
Also remember that Rails rescues some ActiveRecord errors, such as the ActiveRecord::RecordNotFound
displaying the 404 error page.
It means you don’t need to rescue this action yourself
User.find
raises an ActiveRecord::RecordNotFound
when the user doesn’t exist. This is a very powerful feature. Look at the following code
You can simplify it by delegating to Rails the check. Simply use the bang version.
The newly Selected answer submitted by Steven Soroka is close, but not complete. The test itself hides the fact that this is not returning a true 404 - it’s returning a status of 200 - “success”. The original answer was closer, but attempted to render the layout as if no failure had occurred. This fixes everything:
Here’s a typical test set of mine for something I expect to return 404, using RSpec and Shoulda matchers:
describe "user view" do
before do
get :show, :id => 'nonsense'
end
it { should_not assign_to :user }
it { should respond_with :not_found }
it { should respond_with_content_type :html }
it { should_not render_template :show }
it { should_not render_with_layout }
it { should_not set_the_flash }
end
This healthy paranoia allowed me to spot the content-type mismatch when everything else looked peachy :) I check for all these elements: assigned variables, response code, response content type, template rendered, layout rendered, flash messages.
I’ll skip the content type check on applications that are strictly html…sometimes. After all, “a skeptic checks ALL the drawers” :)
http://dilbert.com/strips/comic/1998-01-20/
FYI: I don’t recommend testing for things that are happening in the controller, ie “should_raise”. What you care about is the output. My tests above allowed me to try various solutions, and the tests remain the same whether the solution is raising an exception, special rendering, etc.
I am just wondering if there is any method to remove string from another string? Something like this:
You can use the slice method:
there is a non ‘!’ version as well. More info can be seen in the documentation about other versions as well: http://www.ruby-doc.org/core/classes/String.html#M001213
How about str.gsub("subString", "")
Check out the Ruby Doc
If it is a the end of the string, you can also use chomp
:
I’ve one file, main.rb with the following content:
The tokenizer.rb file is in the same directory and its content is:
If i try to run main.rb I get the following error:
C:\Documents and Settings\my\src\folder>ruby main.rb
C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- tokenizer.rb (LoadError)
from C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require '
from main.rb:1:in `<main>'
I just noticed that if I use load
instead of require
everything works fine. What may the problem be here?
I just tried and it works with require "./tokenizer"
. Hope this helps.
Just do this:
If you put this in a Ruby file that is in the same directory as tokenizer.rb
, it will work fine no matter what your current working directory (CWD) is.
The other answers claim you should use require './tokenizer'
, but that is the wrong answer, because it will only work if you run your Ruby process in the same directory that tokenizer.rb
is in. Pretty much the only reason to consider using require
like that would be if you need to support Ruby 1.8, which doesn’t have require_relative
.
The require './tokenizer'
answer might work for you today, but it unnecessarily limits the ways in which you can run your Ruby code. Tomorrow, if you want to move your files to a different directory, or just want to start your Ruby process from a different directory, you’ll have to rethink all of those require
statements.
Using require
to access files that are on the load path is a fine thing and Ruby gems do it all the time. But you shouldn’t start the argument to require
with a .
unless you are doing something very special and know what you are doing.
When you write code that makes assumptions about its environment, you should think carefully about what assumptions to make. In this case, there are up to three different ways to require the tokenizer
file, and each makes a different assumption:
require_relative 'path/to/tokenizer'
: Assumes that the relative path between the two Ruby source files will stay the same.
require 'path/to/tokenizer'
: Assumes that path/to/tokenizer
is inside one of the directories on the load path ($LOAD_PATH
). This generally requires extra setup, since you have to add something to the load path.
require './path/to/tokenizer'
: Assumes that the relative path from the Ruby process’s current working directory to tokenizer.rb
is going to stay the same.
I think that for most people and most situations, the assumptions made in options #1 and #2 are more likely to hold true over time.
Ruby 1.9 has removed the current directory from the load path, and so you will need to do a relative require on this file, as David Grayson says:
There’s no need to suffix it with .rb
, as Ruby’s smart enough to know that’s what you mean anyway.
I have a map which either changes a value or sets it to nil. I then want to remove the nil entries from the list. The list doesn’t need to be kept.
This is what I currently have:
# A simple example function, which returns a value or nil
def transform(n)
rand > 0.5 ? n * 10 : nil }
end
items.map! { |x| transform(x) } # [1, 2, 3, 4, 5] => [10, nil, 30, 40, nil]
items.reject! { |x| x.nil? } # [10, nil, 30, 40, nil] => [10, 30, 40]
I’m aware I could just do a loop and conditionally collect in another array like this:
new_items = []
items.each do |x|
x = transform(x)
new_items.append(x) unless x.nil?
end
items = new_items
But it doesn’t seem that idiomatic. Is there a nice way to map a function over a list, removing/excluding the nils as you go?
You could use compact
:
I’d like to remind people that if you’re getting an array containing nils as the output of a map
block, and that block tries to conditionally return values, then you’ve got code smell and need to rethink your logic.
For instance, if you’re doing something that does this:
Then don’t. Instead, prior to the map
, reject
the stuff you don’t want or select
what you do want:
I consider using compact
to clean up a mess as a last-ditch effort to get rid of things we didn’t handle correctly, usually because we didn’t know what was coming at us. We should always know what sort of data is being thrown around in our program; Unexpected/unknown data is bad. Anytime I see nils in an array I’m working on, I dig into why they exist, and see if I can improve the code generating the array, rather than allow Ruby to waste time and memory generating nils then sifting through the array to remove them later.
Try using #reduce
or #inject
!
I agree with the accepted answer that we shouldn’t map and compact, but not for the same reasons!
I feel deep inside that map-then-compact is equivalent to select-then-map. Consider: a map is a one-to-one function. If you are mapping from some set of values, and you map, then you want one value in the output set for each value in the input set. If you are having to select before-hand, then you probably don’t want a map on the set. If you are having to select afterwards (or compact) then you probably don’t want a map on the set. In either case you are iterating twice over the entire set, when a reduce only needs to go once.
Also, in English, you are trying to “reduce a set of integers into a set of even integers”.
In your example:
it does not look like the values have changed other than being replaced with nil
. If that is the case, then:
will suffice.
Given I have the below clients hash, is there a quick ruby way (without having to write a multi-line script) to obtain the key given I want to match the client_id? E.g. How to get the key for client_id == "2180"
?
You could use Enumerable#select:
Note that the result will be an array of all the matching values, where each is an array of the key and value.
Ruby 1.9 and greater:
Ruby 1.8:
You could use hash.index
hsh.index(value) => key
Returns the key for a given value. If not found, returns
nil
.
h = { "a" => 100, "b" => 200 }
h.index(200) #=> "b"
h.index(999) #=> nil
So to get "orange"
, you could just use:
You can invert the hash. clients.invert["client_id"=>"2180"]
returns "orange"
JRuby 1.6.x. How do you round a float to decimal places in jruby.
number = 1.1164
number.round(2)
The above shows the following error
wrong number of arguments (1 for 0)
How do I round this to 2 decimal places?
Float#round can take a parameter in Ruby 1.9, not in Ruby 1.8. JRuby defaults to 1.8, but it is capable of running in 1.9 mode.
sprintf('%.2f', number)
is a cryptic, but very powerful way of formatting numbers. The result is always a string, but since you’re rounding I assume you’re doing it for presentation purposes anyway. sprintf
can format any number almost any way you like, and lots more.
Full sprintf documentation: http://www.ruby-doc.org/core-2.0.0/Kernel.html#method-i-sprintf
I would like to know how to parse a YAML file with the following contents:
Currently I am trying to parse it this way:
@custom_asset_packages_yml = (File.exists?("#{RAILS_ROOT}/config/asset_packages.yml") ? YAML.load_file("#{RAILS_ROOT}/config/asset_packages.yml") : nil)
if !@custom_asset_packages_yml.nil?
@custom_asset_packages_yml['javascripts'].each{ |js|
js['fo_global'].each{ |script|
script
}
}
end
But it doesn’t seem to work and gives me an error that the value is nil.
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
If I try this, it puts out the entire string (fo_globallazyload-minholla-min):
Maybe I’m missing something, but why try to parse the file? Why not just load the YAML and examine the object(s) that result?
If your sample YAML is in some.yml
, then this:
gives me
I had the same problem but also wanted to get the content of the file (after the YAML front-matter).
This is the best solution I have found:
if (md = contents.match(/^(?<metadata>---\s*\n.*?\n?)^(---\s*$\n?)/m))
self.contents = md.post_match
self.metadata = YAML.load(md[:metadata])
end
Source and discussion: https://practicingruby.com/articles/tricks-for-working-with-text-and-files
Here is the one liner i use, from terminal, to test the content of yml file(s):
$ ruby -r yaml -r pp -e 'pp YAML.load_file("/Users/za/project/application.yml")'
{"logging"=>
{"path"=>"/var/logs/",
"file"=>"TacoCloud.log",
"level"=>
{"root"=>"WARN", "org"=>{"springframework"=>{"security"=>"DEBUG"}}}}}
I’m running Ruby on Windows though I don’t know if that should make a difference. All I want to do is get the current working directory’s absolute path. Is this possible from irb? Apparently from a script it’s possible using File.expand_path(__FILE__)
But from irb I tried the following and got a “Permission denied” error:
Dir.pwd
seems to do the trick.
File.expand_path File.dirname(__FILE__)
will return the directory relative to the file this command is called from.
But Dir.pwd
returns the working directory (results identical to executing pwd
in your terminal)
As for the path relative to the current executing script, since Ruby 2.0 you can also use
So this is basically the same as
I need to upgrade gems to 1.8 i tried installing the respective debian packages but it seems its not getting upgraded
anujm@test:~$ dpkg -l |grep -i rubygem
ii rubygems 1.3.5-1ubuntu2 package management framework for Ruby libraries/applications
ii rubygems-lwes 0.8.2-1323277262 LWES rubygems
ii rubygems1.8 1.3.5-1ubuntu2 package management framework for Ruby libraries/applications
ii rubygems1.9 1.3.5-1ubuntu2 package management framework for Ruby libraries/applications
anujm@test:~$
anujm@test:~$ gem
gem gem1.8 gem1.9
anujm@test:~$ sudo gem1.8 install serve
ERROR: Error installing serve:
multi_json requires RubyGems version >= 1.3.6
anujm@test:~$
Install rubygems-update
run this commands as root or use sudo.
You can update all gems by just performing:
sudo gem update
Or:
I need an easy way to take a tar file and convert it into a string (and vice versa). Is there a way to do this in Ruby? My best attempt was this:
I thought that would be enough to convert it to a string, but then when I try to write it back out like this…
It isn’t the same file. Doing ls -l
shows the files are of different sizes, although they are pretty close (and opening the file reveals most of the contents intact). Is there a small mistake I’m making or an entirely different (but workable) way to accomplish this?
First, you should open the file as a binary file. Then you can read the entire file in, in one command.
That will get you the entire file in a string.
After that, you probably want to file.close
. If you don’t do that, file
won’t be closed until it is garbage-collected, so it would be a slight waste of system resources while it is open.
If you need binary mode, you’ll need to do it the hard way:
If not, shorter and sweeter is:
To avoid leaving the file open, it is best to pass a block to File.open. This way, the file will be closed after the block executes.
How do you convert a Unix timestamp (seconds since epoch) to Ruby DateTime?
DateTime.strptime
can handle seconds since epoch. The number must be converted to a string:
Sorry, brief moment of synapse failure. Here’s the real answer.
Brief example (this takes into account the current system timezone):
$ date +%s
1318996912
$ irb
ruby-1.9.2-p180 :001 > require 'date'
=> true
ruby-1.9.2-p180 :002 > Time.at(1318996912).to_datetime
=> #<DateTime: 2011-10-18T23:01:52-05:00 (13261609807/5400,-5/24,2299161)>
Further update (for UTC):
ruby-1.9.2-p180 :003 > Time.at(1318996912).utc.to_datetime
=> #<DateTime: 2011-10-19T04:01:52+00:00 (13261609807/5400,0/1,2299161)>
Recent Update: I benchmarked the top solutions in this thread while working on a HA service a week or two ago, and was surprised to find that Time.at(..)
outperforms DateTime.strptime(..)
(update: added more benchmarks).
# ~ % ruby -v
# => ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0]
irb(main):038:0> Benchmark.measure do
irb(main):039:1* ["1318996912", "1318496912"].each do |s|
irb(main):040:2* DateTime.strptime(s, '%s')
irb(main):041:2> end
irb(main):042:1> end
=> #<Benchmark ... @real=2.9e-05 ... @total=0.0>
irb(main):044:0> Benchmark.measure do
irb(main):045:1> [1318996912, 1318496912].each do |i|
irb(main):046:2> DateTime.strptime(i.to_s, '%s')
irb(main):047:2> end
irb(main):048:1> end
=> #<Benchmark ... @real=2.0e-05 ... @total=0.0>
irb(main):050:0* Benchmark.measure do
irb(main):051:1* ["1318996912", "1318496912"].each do |s|
irb(main):052:2* Time.at(s.to_i).to_datetime
irb(main):053:2> end
irb(main):054:1> end
=> #<Benchmark ... @real=1.5e-05 ... @total=0.0>
irb(main):056:0* Benchmark.measure do
irb(main):057:1* [1318996912, 1318496912].each do |i|
irb(main):058:2* Time.at(i).to_datetime
irb(main):059:2> end
irb(main):060:1> end
=> #<Benchmark ... @real=2.0e-05 ... @total=0.0>
Time Zone Handling
I just want to clarify, even though this has been commented so future people don’t miss this very important distinction.
displays a return value in UTC and requires the seconds to be a String and outputs a UTC Time object, whereas
displays a return value in the LOCAL time zone, normally requires a FixNum argument, but the Time object itself is still in UTC even though the display is not.
So even though I passed the same integer to both methods, I seemingly two different results because of how the class’ #to_s
method works. However, as @Eero had to remind me twice of:
An equality comparison between the two return values still returns true. Again, this is because the values are basically the same (although different class’s, the #==
method takes care of this for you), but the #to_s
method prints drastically different strings. Although, if we look at the strings, we can see they are indeed the same time, just printed in different time zones.
Method Argument Clarification
The docs also say “If a numeric argument is given, the result is in local time.” which makes sense, but was a little confusing to me because they don’t give any examples of non-integer arguments in the docs. So, for some non-integer argument examples:
you can’t use a String argument, but you can use a Time argument into Time.at
and it will return the result in the time zone of the argument:
Benchmarks
After a discussion with @AdamEberlin on his answer, I decided to publish slightly changed benchmarks to make everything as equal as possible. Also, I never want to have to build these again so this is as good a place as any to save them.
Time.at(int).to_datetime ~ 2.8x faster
09:10:58-watsw018:~$ ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]
09:11:00-watsw018:~$ irb
irb(main):001:0> require 'benchmark'
=> true
irb(main):002:0> require 'date'
=> true
irb(main):003:0>
irb(main):004:0* format = '%s'
=> "%s"
irb(main):005:0> times = ['1318996912', '1318496913']
=> ["1318996912", "1318496913"]
irb(main):006:0> int_times = times.map(&:to_i)
=> [1318996912, 1318496913]
irb(main):007:0>
irb(main):008:0* datetime_from_strptime = DateTime.strptime(times.first, format)
=> #<DateTime: 2011-10-19T04:01:52+00:00 ((2455854j,14512s,0n),+0s,2299161j)>
irb(main):009:0> datetime_from_time = Time.at(int_times.first).to_datetime
=> #<DateTime: 2011-10-19T00:01:52-04:00 ((2455854j,14512s,0n),-14400s,2299161j)>
irb(main):010:0>
irb(main):011:0* datetime_from_strptime === datetime_from_time
=> true
irb(main):012:0>
irb(main):013:0* Benchmark.measure do
irb(main):014:1* 100_000.times {
irb(main):015:2* times.each do |i|
irb(main):016:3* DateTime.strptime(i, format)
irb(main):017:3> end
irb(main):018:2> }
irb(main):019:1> end
=> #<Benchmark::Tms:0x00007fbdc18f0d28 @label="", @real=0.8680500000045868, @cstime=0.0, @cutime=0.0, @stime=0.009999999999999998, @utime=0.86, @total=0.87>
irb(main):020:0>
irb(main):021:0* Benchmark.measure do
irb(main):022:1* 100_000.times {
irb(main):023:2* int_times.each do |i|
irb(main):024:3* Time.at(i).to_datetime
irb(main):025:3> end
irb(main):026:2> }
irb(main):027:1> end
=> #<Benchmark::Tms:0x00007fbdc3108be0 @label="", @real=0.33059399999910966, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.32000000000000006, @total=0.32000000000000006>
****edited to not be completely and totally incorrect in every way****
****added benchmarks****
I have an array of hashes like following
I am trying to sort above array in descending order according to the value of :bar
in each hash.
I am using sort_by
like following to sort above array.
However above sorts the array in ascending order. How do I make it sort in descending order?
One solution was to do following:
But that negative sign does not seem appropriate. Any views?
It’s always enlightening to do a benchmark on the various suggested answers. Here’s what I found out:
#!/usr/bin/ruby require 'benchmark' ary = [] 1000.times { ary << {:bar => rand(1000)} } n = 500 Benchmark.bm(20) do |x| x.report("sort") { n.times { ary.sort{ |a,b| b[:bar] <=> a[:bar] } } } x.report("sort reverse") { n.times { ary.sort{ |a,b| a[:bar] <=> b[:bar] }.reverse } } x.report("sort_by -a[:bar]") { n.times { ary.sort_by{ |a| -a[:bar] } } } x.report("sort_by a[:bar]*-1") { n.times { ary.sort_by{ |a| a[:bar]*-1 } } } x.report("sort_by.reverse!") { n.times { ary.sort_by{ |a| a[:bar] }.reverse } } end user system total real sort 3.960000 0.010000 3.970000 ( 3.990886) sort reverse 4.040000 0.000000 4.040000 ( 4.038849) sort_by -a[:bar] 0.690000 0.000000 0.690000 ( 0.692080) sort_by a[:bar]*-1 0.700000 0.000000 0.700000 ( 0.699735) sort_by.reverse! 0.650000 0.000000 0.650000 ( 0.654447)
I think it’s interesting that @Pablo’s sort_by{...}.reverse!
is fastest. Before running the test I thought it would be slower than “-a[:bar]
” but negating the value turns out to take longer than it does to reverse the entire array in one pass. It’s not much of a difference, but every little speed-up helps.
Please note that these results are different in Ruby 1.9
Here are results for Ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin10.8.0]:
user system total real
sort 1.340000 0.010000 1.350000 ( 1.346331)
sort reverse 1.300000 0.000000 1.300000 ( 1.310446)
sort_by -a[:bar] 0.430000 0.000000 0.430000 ( 0.429606)
sort_by a[:bar]*-1 0.420000 0.000000 0.420000 ( 0.414383)
sort_by.reverse! 0.400000 0.000000 0.400000 ( 0.401275)
These are on an old MacBook Pro. Newer, or faster machines, will have lower values, but the relative differences will remain.
Here’s a bit updated version on newer hardware and the 2.1.1 version of Ruby:
#!/usr/bin/ruby
require 'benchmark'
puts "Running Ruby #{RUBY_VERSION}"
ary = []
1000.times {
ary << {:bar => rand(1000)}
}
n = 500
puts "n=#{n}"
Benchmark.bm(20) do |x|
x.report("sort") { n.times { ary.dup.sort{ |a,b| b[:bar] <=> a[:bar] } } }
x.report("sort reverse") { n.times { ary.dup.sort{ |a,b| a[:bar] <=> b[:bar] }.reverse } }
x.report("sort_by -a[:bar]") { n.times { ary.dup.sort_by{ |a| -a[:bar] } } }
x.report("sort_by a[:bar]*-1") { n.times { ary.dup.sort_by{ |a| a[:bar]*-1 } } }
x.report("sort_by.reverse") { n.times { ary.dup.sort_by{ |a| a[:bar] }.reverse } }
x.report("sort_by.reverse!") { n.times { ary.dup.sort_by{ |a| a[:bar] }.reverse! } }
end
# >> Running Ruby 2.1.1
# >> n=500
# >> user system total real
# >> sort 0.670000 0.000000 0.670000 ( 0.667754)
# >> sort reverse 0.650000 0.000000 0.650000 ( 0.655582)
# >> sort_by -a[:bar] 0.260000 0.010000 0.270000 ( 0.255919)
# >> sort_by a[:bar]*-1 0.250000 0.000000 0.250000 ( 0.258924)
# >> sort_by.reverse 0.250000 0.000000 0.250000 ( 0.245179)
# >> sort_by.reverse! 0.240000 0.000000 0.240000 ( 0.242340)
New results running the above code using Ruby 2.2.1 on a more recent Macbook Pro. Again, the exact numbers aren’t important, it’s their relationships:
Running Ruby 2.2.1
n=500
user system total real
sort 0.650000 0.000000 0.650000 ( 0.653191)
sort reverse 0.650000 0.000000 0.650000 ( 0.648761)
sort_by -a[:bar] 0.240000 0.010000 0.250000 ( 0.245193)
sort_by a[:bar]*-1 0.240000 0.000000 0.240000 ( 0.240541)
sort_by.reverse 0.230000 0.000000 0.230000 ( 0.228571)
sort_by.reverse! 0.230000 0.000000 0.230000 ( 0.230040)
Just a quick thing, that denotes the intent of descending order.
(Will think of a better way in the mean time) ;)
You could do:
For context, it on a remote server which has a firewall. I’m setting up my environment through a proxy. I have ruby 1.8.7
. When I try to gem install..
I get the following error:
Building native extensions. This could take a while...
ERROR: Error installing json:
ERROR: Failed to build gem native extension.
/usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/ruby.h
Gem files will remain installed in /usr/lib64/ruby/gems/1.8/gems/json-1.8.1 for inspection.
Results logged to /usr/lib64/ruby/gems/1.8/gems/json-1.8.1/ext/json/ext/generator/gem_make.out
Since I was unsure what the problem is, I googled and found these
gem install: Failed to build gem native extension (can't find header files) - the instructions here seem to be specific to the gem being installed.
How to install json gem - Failed to build gem native extension This seems to be slightly different error.
Any hints? Thanks!
Modern era update, as stated by mimoralea:
In case that you are using ruby 2.0 or 2.2 (thanks @patrick-davey).
or, generic way:
The first link you’ve posted is exactly your case: there is no ruby development environment installed. Development env is needed to compile ruby extensions, which are mostly written in C
. Proxy has nothing to do with the problem: everything is downloaded fine, just compilation fails.
I would suggest you to install ruby-dev
(ruby-devel
for rpm-based distros) package onto you target machine. gcc
package might be needed as well.
Try:
Or, for Redhat distro:
Or, for [open]SuSE:
In case that you are using ruby 2.0 or 2.2 (thanks @patrick-davey) or 2.3 (thanks @juanitofatas).
sudo apt-get install ruby-dev
sudo apt-get install ruby2.0-dev
sudo apt-get install ruby2.2-dev
sudo apt-get install ruby2.3-dev
And you get the pattern here…
I also encountered this problem because I install Ruby on Ubuntu via brightbox, and I thought ruby-dev is the trunk of ruby. So I did not install. Install ruby2.3-dev fixes it:
This is what I have now - which looks too verbose for the work it is doing.
Assume tokens is a array obtained by splitting a CSV line. now the functions like strip! chomp! et. all return nil if the string was not modified
What is the Ruby way to say trim it if it contains extra leading or trailing spaces without creating copies?
Gets uglier if I want to do tokens[Title].chomp!.strip!
I guess what you want is:
The #strip!
method will return nil
if it didn’t strip anything, and the variable itself if it was stripped.
According to Ruby standards, a method suffixed with an exclamation mark changes the variable in place.
Hope this helps.
Update: This is output from irb
to demonstrate:
Btw, now ruby already supports just strip without “!”.
Compare:
p "abc".strip! == " abc ".strip! # false, because "abc".strip! will return nil
p "abc".strip == " abc ".strip # true
Also it’s impossible to strip
without duplicates. See sources in string.c:
ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
Update 1: As I see now – it was created in 1999 year (see rev #372 in SVN):
Update2: strip!
will not create duplicates — both in 1.9.x, 2.x and trunk versions.
There’s no need to both strip and chomp as strip will also remove trailing carriage returns - unless you’ve changed the default record separator and that’s what you’re chomping.
Olly’s answer already has the canonical way of doing this in Ruby, though if you find yourself doing this a lot you could always define a method for it:
Giving:
Also keep in mind that the if statement will prevent @title
from being assigned if the token is nil, which will result in it keeping its previous value. If you want or don’t mind @title
always being assigned you can move the check into the method and further reduce duplication:
As an alternative, if you’re feeling adventurous you can define a method on String itself:
Giving one of:
@title = tokens[Title].strip_or_self! if tokens[Title]
@title = tokens[Title] && tokens[Title].strip_or_self!
I can finds gems that are installed using gem list
, but it doesn’t show me where the gems are installed.
How can I find where the gems are, and how can I know before installing a gem where it will be 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
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/ttm/.rbenv/versions/2.0.0-p247/bin
- /Users/ttm/.rbenv/libexec
- /Users/ttm/.rbenv/plugins/ruby-build/bin
- /Users/ttm/perl5/perlbrew/bin
- /Users/ttm/perl5/perlbrew/perls/perl-5.18.1/bin
- /Users/ttm/.pyenv/shims
- /Users/ttm/.pyenv/bin
- /Users/ttm/.rbenv/shims
- /Users/ttm/.rbenv/bin
- /Users/ttm/bin
- /usr/local/mysql-5.6.12-osx10.7-x86_64/bin
- /Users/ttm/libsmi/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
- /usr/local/bin
Notice the two sections for:
INSTALLATION DIRECTORY
GEM PATHS
I found it useful to get a location of the library file with:
After installing the gems, if you want to know where a particular gem is. Try typing:
You will be able to see the list of gems you have installed. Now use bundle show
and name the gem you want to know the path for, like this:
What is the right way to:
or to get the count of items in it?
You probably want to use kind_of?()
.
Are you sure it needs to be an array? You may be able to use respond_to?(method)
so your code would work for similar things that aren’t necessarily arrays (maybe some other enumberable thing). If you do actually need an array
, then the post describing the Array#kind\_of?
method is best.
Instead of testing for an Array,
just convert whatever you get into a one-level Array,
so your code only needs to handle the one case.
Ruby has various ways to harmonize an API which can take an object or an Array of objects, so, taking a guess at why you want to know if something is an Array, I have a suggestion.
The splat operator contains lots of magic you can look up, or you can just call Array(something)
which will add an Array wrapper if needed. It’s similar to [*something]
in this one case.
Or, you could use the splat in the parameter declaration and then .flatten
, giving you a different sort of collector. (For that matter, you could call .flatten
above, too.)
def f *x
p x.flatten.inspect
end # => nil
f 1 # => "[1]"
f 1,2 # => "[1, 2]"
f [1] # => "[1]"
f [1,2] # => "[1, 2]"
f [1,2],3,4 # => "[1, 2, 3, 4]"
And, thanks gregschlom, it’s sometimes faster to just use Array(x)
because when it’s already an Array
it doesn’t need to create a new object.
I have a Ruby array which contains duplicate elements.
How can I remove all the duplicate elements from this array while retaining all unique elements without using for-loops and iteration?
The uniq method removes all duplicate elements and retains all unique elements in the array.
One of many beauties of Ruby language.
You can also return the intersection.
This will also delete duplicates.
You can remove the duplicate elements with the uniq method:
What might also be useful to know is that the uniq method takes a block, so e.g if you a have an array of keys like this:
and you want to know what are the unique files, you can find it out with:
I am trying to understand the difference between these four methods. I know by default that ==
calls the method equal?
which returns true when both operands refer to exactly the same object.
===
by default also calls ==
which calls equal?
… okay, so if all these three methods are not overridden, then I guess ===
, ==
and equal?
do exactly the same thing?
Now comes eql?
. What does this do (by default)? Does it make a call to the operand’s hash/id?
Why does Ruby have so many equality signs? Are they supposed to differ in semantics?
I’m going to heavily quote the Object documentation here, because I think it has some great explanations. I encourage you to read it, and also the documentation for these methods as they’re overridden in other classes, like String.
Side note: if you want to try these out for yourself on different objects, use something like this:
class Object
def all_equals(o)
ops = [:==, :===, :eql?, :equal?]
Hash[ops.map(&:to_s).zip(ops.map {|s| send(s, o) })]
end
end
"a".all_equals "a" # => {"=="=>true, "==="=>true, "eql?"=>true, "equal?"=>false}
==
— generic “equality”
At the Object level,==
returns true only ifobj
andother
are the same object. Typically, this method is overridden in descendant classes to provide class-specific meaning.
This is the most common comparison, and thus the most fundamental place where you (as the author of a class) get to decide if two objects are “equal” or not.
===
— case equality
For class Object, effectively the same as calling #==
, but typically overridden by descendants to provide meaningful semantics in case statements.
This is incredibly useful. Examples of things which have interesting ===
implementations:
So you can do things like:
case some_object
when /a regex/
# The regex matches
when 2..4
# some_object is in the range 2..4
when lambda {|x| some_crazy_custom_predicate }
# the lambda returned true
end
See my answer here for a neat example of how case
+Regex
can make code a lot cleaner. And of course, by providing your own ===
implementation, you can get custom case
semantics.
eql?
— Hash
equality
The
eql?
method returns true ifobj
andother
refer to the same hash key. This is used byHash
to test members for equality. For objects of classObject
,eql?
is synonymous with==
. Subclasses normally continue this tradition by aliasingeql?
to their overridden==
method, but there are exceptions.Numeric
types, for example, perform type conversion across==
, but not acrosseql?
, so:
So you’re free to override this for your own uses, or you can override ==
and use alias :eql? :==
so the two methods behave the same way.
equal?
— identity comparison
Unlike==
, theequal?
method should never be overridden by subclasses: it is used to determine object identity (that is,a.equal?(b)
iffa
is the same object asb
).
This is effectively pointer comparison.
I love jtbandes answer, but since it is pretty long, I will add my own compact answer:
==
, ===
, eql?
, equal?
are 4 comparators, ie. 4 ways to compare 2 objects, in Ruby.
As, in Ruby, all comparators (and most operators) are actually method-calls, you can change, overwrite, and define the semantics of these comparing methods yourself. However, it is important to understand, when Ruby’s internal language constructs use which comparator:
==
(value comparison)
Ruby uses :== everywhere to compare the values of 2 objects, eg. Hash-values:
===
(case comparison)
Ruby uses :=== in case/when constructs. The following code snippets are logically identical:
eql?
(Hash-key comparison)
Ruby uses :eql? (in combination with the method hash) to compare Hash-keys. In most classes :eql? is identical with :==.
Knowledge about :eql? is only important, when you want to create your own special classes:
class Equ
attr_accessor :val
alias_method :initialize, :val=
def hash() self.val % 2 end
def eql?(other) self.hash == other.hash end
end
h = {Equ.new(3) => 3, Equ.new(8) => 8, Equ.new(15) => 15} #3 entries, but 2 are :eql?
h.size # => 2
h[Equ.new(27)] # => 15
Note: The commonly used Ruby-class Set also relies on Hash-key-comparison.
equal?
(object identity comparison)
Ruby uses :equal? to check if two objects are identical. This method (of class BasicObject) is not supposed to be overwritten.
The == operator, also known as equality or double equal, will return true if both objects are equal and false if they are not.
The != operator, also known as inequality, is the opposite of ==. It will return true if both objects are not equal and false if they are equal.
Note that two arrays with the same elements in a different order are not equal, uppercase and lowercase versions of the same letter are not equal and so on.
When comparing numbers of different types (e.g., integer and float), if their numeric value is the same, == will return true.
Unlike the == operator which tests if both operands are equal, the equal method checks if the two operands refer to the same object. This is the strictest form of equality in Ruby.
Example: a = “zen” b = “zen”
In the example above, we have two strings with the same value. However, they are two distinct objects, with different object IDs. Hence, the equal? method will return false.
Let’s try again, only this time b will be a reference to a. Notice that the object ID is the same for both variables, as they point to the same object.
a = "zen"
b = a
a.object_id # Output: => 18637360
b.object_id # Output: => 18637360
a.equal? b # Output: => true
In the Hash class, the eql? method it is used to test keys for equality. Some background is required to explain this. In the general context of computing, a hash function takes a string (or a file) of any size and generates a string or integer of fixed size called hashcode, commonly referred to as only hash. Some commonly used hashcode types are MD5, SHA-1, and CRC. They are used in encryption algorithms, database indexing, file integrity checking, etc. Some programming languages, such as Ruby, provide a collection type called hash table. Hash tables are dictionary-like collections which store data in pairs, consisting of unique keys and their corresponding values. Under the hood, those keys are stored as hashcodes. Hash tables are commonly referred to as just hashes. Notice how the word hashcan refer to a hashcode or to a hash table. In the context of Ruby programming, the word hash almost always refers to the dictionary-like collection.
Ruby provides a built-in method called hash for generating hashcodes. In the example below, it takes a string and returns a hashcode. Notice how strings with the same value always have the same hashcode, even though they are distinct objects (with different object IDs).
"meditation".hash # Output: => 1396080688894079547
"meditation".hash # Output: => 1396080688894079547
"meditation".hash # Output: => 1396080688894079547
The hash method is implemented in the Kernel module, included in the Object class, which is the default root of all Ruby objects. Some classes such as Symbol and Integer use the default implementation, others like String and Hash provide their own implementations.
Symbol.instance_method(:hash).owner # Output: => Kernel
Integer.instance_method(:hash).owner # Output: => Kernel
String.instance_method(:hash).owner # Output: => String
Hash.instance_method(:hash).owner # Output: => Hash
In Ruby, when we store something in a hash (collection), the object provided as a key (e.g., string or symbol) is converted into and stored as a hashcode. Later, when retrieving an element from the hash (collection), we provide an object as a key, which is converted into a hashcode and compared to the existing keys. If there is a match, the value of the corresponding item is returned. The comparison is made using the eql? method under the hood.
In most cases, the eql? method behaves similarly to the == method. However, there are a few exceptions. For instance, eql? does not perform implicit type conversion when comparing an integer to a float.
Many of Ruby’s built-in classes, such as String, Range, and Regexp, provide their own implementations of the === operator, also known as case-equality, triple equals or threequals. Because it’s implemented differently in each class, it will behave differently depending on the type of object it was called on. Generally, it returns true if the object on the right “belongs to” or “is a member of” the object on the left. For instance, it can be used to test if an object is an instance of a class (or one of its subclasses).
String === "zen" # Output: => true
Range === (1..2) # Output: => true
Array === [1,2,3] # Output: => true
Integer === 2 # Output: => true
The same result can be achieved with other methods which are probably best suited for the job. It’s usually better to write code that is easy to read by being as explicit as possible, without sacrificing efficiency and conciseness.
2.is_a? Integer # Output: => true
2.kind_of? Integer # Output: => true
2.instance_of? Integer # Output: => false
Notice the last example returned false because integers such as 2 are instances of the Fixnum class, which is a subclass of the Integer class. The ===, is_a? and instance_of? methods return true if the object is an instance of the given class or any subclasses. The instance_of method is stricter and only returns true if the object is an instance of that exact class, not a subclass.
The is_a? and kind_of? methods are implemented in the Kernel module, which is mixed in by the Object class. Both are aliases to the same method. Let’s verify:
Kernel.instance_method(:kind_of?) == Kernel.instance_method(:is_a?) # Output: => true
When the === operator is called on a range object, it returns true if the value on the right falls within the range on the left.
(1..4) === 3 # Output: => true
(1..4) === 2.345 # Output: => true
(1..4) === 6 # Output: => false
("a".."d") === "c" # Output: => true
("a".."d") === "e" # Output: => false
Remember that the === operator invokes the === method of the left-hand object. So (1..4) === 3 is equivalent to (1..4).=== 3. In other words, the class of the left-hand operand will define which implementation of the === method will be called, so the operand positions are not interchangeable.
Returns true if the string on the right matches the regular expression on the left. /zen/ === “practice zazen today” # Output: => true # is the same as “practice zazen today”=~ /zen/
This operator is also used under the hood on case/when statements. That is its most common use.
In the example above, if Ruby had implicitly used the double equal operator (==), the range 10..20 would not be considered equal to an integer such as 15. They match because the triple equal operator (===) is implicitly used in all case/when statements. The code in the example above is equivalent to:
The =~ (equal-tilde) and !~ (bang-tilde) operators are used to match strings and symbols against regex patterns.
The implementation of the =~ method in the String and Symbol classes expects a regular expression (an instance of the Regexp class) as an argument.
"practice zazen" =~ /zen/ # Output: => 11
"practice zazen" =~ /discursive thought/ # Output: => nil
:zazen =~ /zen/ # Output: => 2
:zazen =~ /discursive thought/ # Output: => nil
The implementation in the Regexp class expects a string or a symbol as an argument.
In all implementations, when the string or symbol matches the Regexp pattern, it returns an integer which is the position (index) of the match. If there is no match, it returns nil. Remember that, in Ruby, any integer value is “truthy” and nil is “falsy”, so the =~ operator can be used in if statements and ternary operators.
Pattern-matching operators are also useful for writing shorter if statements. Example:
if meditation_type == "zazen" || meditation_type == "shikantaza" || meditation_type == "kinhin"
true
end
Can be rewritten as:
if meditation_type =~ /^(zazen|shikantaza|kinhin)$/
true
end
The !~ operator is the opposite of =~, it returns true when there is no match and false if there is a match.
More info is available at this blog post.
I want to be able to run a single spec file’s tests — for the one file I’m editing, for example. rake spec
executes all the specs. My project is not a Rails project, so rake spec:doc
doesn’t work.
Don’t know if this matters, but here is my directory structure.
./Rakefile ./lib ./lib/cushion.rb ./lib/cushion ./lib/cushion/doc.rb ./lib/cushion/db.rb ./spec ./spec/spec.opts ./spec/spec_helper.rb ./spec/db_spec.rb
Or you can skip rake and use the ‘rspec’ command:
In your case I think as long as your ./spec/db_spec.rb file includes the appropriate helpers, it should work fine.
If you’re using an older version of rspec it is:
The raw invocation:
rake spec SPEC=spec/controllers/sessions_controller_spec.rb \ SPEC_OPTS="-e \"should log in with cookie\""
Now figure out how to embed this into your editor.
This question is an old one, but it shows up at the top of Google when searching for how to run a single test. I don’t know if it’s a recent addition, but to run a single test out of a spec you can do the following:
where -line number- is a line number that contains part of your test. For example, if you had a spec like:
Let’s say it’s saved in spec/models/foo_spec.rb. Then you would run:
and it would just run that one spec. In fact, that number could be anything from 2 to 5.
Hope this helps!
What is the easiest way to convert
to
If you’re using ruby 1.8.7 or 1.9, you can use the fact that iterator methods like each_with_index
, when called without a block, return an Enumerator
object, which you can call Enumerable
methods like map
on. So you can do:
In 1.8.6 you can do:
Ruby has Enumerator#with_index(offset = 0), so first convert the array to an enumerator using Object#to_enum or Array#map:
In ruby 1.9.3 there is a chainable method called with_index
which can be chained to map.
For example:
As it stands now, I’m a Java and C# developer. The more and more I look at Ruby on Rails, the more I really want to learn it.
What have you found to be the best route to learn RoR? Would it be easier to develop on Windows, or should I just run a virtual machine with Linux?
Is there an IDE that can match the robustness of Visual Studio? Any programs to develop that give a good overhead of what to do? Any good books?
Seriously, any tips/tricks/rants would be awesome.
I’ve been moving from C# in my professional career to looking at Ruby and RoR in my personal life, and I’ve found linux to be slightly more appealing personally for development. Particularly now that I’ve started using git, the implementation is cleaner on linux.
Currently I’m dual booting and getting closer to running Ubuntu full time. I’m using gedit with various plugins for the development environment. And as of late 2010, I’m making the push to use Vim for development, even over Textmate on OS X.
A large amount of the Rails developers are using (gasp) Macs, which has actually got me thinking in that direction.
Although I haven’t tried it, Ruby in Steel gives you a Ruby IDE inside the Visual Studio world, and IronRuby is the .NET flavor of Ruby, if you’re interested.
As far as books are concerned, the Programming Ruby (also known as the Pickaxe) book from the Pragmatic Programmers is the de-facto for learning Ruby. I bit the bullet and purchased that book and Agile Web Development with Rails; both books have been excellent.
Peepcode screencasts and PDF books have also been great for getting started; at $9 per screencast it’s hard to go wrong. I actually bought a 5-pack.
Also check out the following:
I’ve burned through the backlog of Rails and Rails Envy podcasts in the past month and they have provided wonderful insight into lots of topics, even regarding software development in general.
Beware, the rails world is a massively frustrating mess of outdated and inconsistent documentation and examples. It is maybe one of the fastest moving and most faddish development communities there is. By the time you learn something it will already have changed. Even the books are not consistent in which version of rails they are talking about. Documentation by blogging! enough said.
I currently do RoR on windows. My advice is to avoid windows if you can. Lots of things don’t work and the rails community really really doesn’t care about you. The move to Git has really messed me up since it doesn’t work very well on windows. A lot of gems will fail because of this (Heroku looks like a cool tool - too bad for me it can’t handle window’s Git setup). Capistrano is out. It goes on and annoyingly on.
Plus, in the back of your mind, you always wonder when something doesn’t work “Is it a rails/windows problem?” I am not sure this is solved by using linux because linux brings its own hassles like constantly having to upgrade all those different dependencies, etc…If that’s the kind of thing you enjoy it might be an okay choice for you. Those days of enjoying system fiddling are behind me and I just want to get on with doing my work. I am planning on installing ubuntu on a home machine just so i can get familiar with things like capistrano so maybe my opinion will change.
I’d highly suggest if you are going to do rails dev for any amount of time you seriously consider getting a Mac. If you value your time and sanity it will pay for itself almost instantly. Depending on how you value your time 10 hours of debugging windows/linux setup problems and you have spend as much as a Mac costs anyway.
Rails is a joy compared to what it replaces but it is a bit of a pain in that its proponents skip right past a lot of the boring but important stuff like documentation, compatibility issues and community building. It is way more powerful than other frameworks like Django but I sometimes look over at the Django documentation and community and sigh like a guy with a wild sexy girlfriend looking at his friend’s plain but sane and stable wife. But then rails adds a feature and I go “Ohhh shiny!”
IMO the Rails Screencasts are better than the Peepcode screencasts. RubyPlus also has screencasts, mind you, they are bit rough around the edges. BuildingWebApps has a free online course that starts doing screencasts halfway through.
Path of least resistance:
How can I remove RVM (Ruby Version Manager) from my system?
There’s a simple command built-in that will pull it:
This will remove the rvm/
directory and all the rubies built within it. In order to remove the final trace of rvm, you need to remove the rvm gem, too:
If you’ve made modifications to your PATH
you might want to pull those, too. Check your .bashrc
, .profile
and .bash_profile
files, among other things.
You may also have an /etc/rvmrc
file, or one in your home directory ~/.rvmrc
that may need to be removed as well.
If the other answers don’t remove RVM throughly enough for you, RVM’s Troubleshooting page contains this section:
How do I completely clean out all traces of RVM from my system, including for system wide installs?
Here is a custom script which we name as
cleanout-rvm
. While you can definitely uservm implode
as a regular user orrvmsudo rvm implode
for a system wide install, this script is useful as it steps completely outside of RVM and cleans out RVM without using RVM itself, leaving no traces.#!/bin/bash /usr/bin/sudo rm -rf $HOME/.rvm $HOME/.rvmrc /etc/rvmrc /etc/profile.d/rvm.sh /usr/local/rvm /usr/local/bin/rvm /usr/bin/sudo /usr/sbin/groupdel rvm /bin/echo "RVM is removed. Please check all .bashrc|.bash_profile|.profile|.zshrc for RVM source lines and delete or comment out if this was a Per-User installation."
When using implode and you see:
Then you may want to use –force
Then remove rvm from following locations:
Check the following files and remove or comment out references to rvm
Comment-out / Remove the following lines from /etc/profile
/etc/profile is a readonly file so use
And after making the change write using a bang!
Finally re-login / restart your terminal.