Does Ruby 1.9.2 Have an Is_A? Function

Does ruby 1.9.2 have an is_a? function?

There's not a built in function to say if a string is effectively an integer, but you can easily make your own:

class String
def int
Integer(self) rescue nil
end
end

This works because the Kernel method Integer() throws an error if the string can't be converted to an integer, and the inline rescue nil turns that error into a nil.

Integer("1") -> 1
Integer("1x") -> nil
Integer("x") -> nil

and thus:

"1".int -> 1 (which in boolean terms is `true`)
"1x".int -> nil
"x".int -> nil

You could alter the function to return true in the true cases, instead of the integer itself, but if you're testing the string to see if it's an integer, chances are you want to use that integer for something! I very commonly do stuff like this:

if i = str.int
# do stuff with the integer i
else
# error handling for non-integer strings
end

Although if the assignment in a test position offends you, you can always do it like this:

i = str.int
if i
# do stuff with the integer i
else
# error handling for non-integer strings
end

Either way, this method only does the conversion once, which if you have to do a lot of these, may be a significant speed advantage.

[Changed function name from int? to int to avoid implying it should return just true/false.]

Can't find Ruby 1.9.2 after installing RVM

RVM is a version manager which enables you to decide which of multiple installed Ruby versions you want to use in your current shell session. RVM does this by providing a shell function named rvm which can be used to switch between versions in the current session. This changes environment variables, especially the GEM_HOME and PATH, to match the currently selected Ruby installation.

Every installed Ruby version can be selected by a specific identifier string. If you had a system-wide Ruby installation already before you installed RVM, that one should be referenced by the string system. The newly installed version in your case should be called 1.9.2.

To make RVM work as intended, it is necessary to load the rvm shell function into your shell. How to do this is described in the RVM Installation Documentation in section 2 - "Load RVM into your shell sessions as a function".

You can see if the shell function is correctly loaded when the command type rvm | head -n1 responds with:

rvm is a shell function

If not correctly loaded it will tell you something like this:

rvm is /home/someone/.rvm/bin/rvm

If you finally have it working you can switch your active Ruby version with commands like rvm 1.9.2 or rvm system. You can get a list of all Ruby versions and their identifier strings recognized by RVM by the command rvm list.

You can also specify which Ruby version shall be enabled in all new shell sessions from the beginning by issuing the following command once:

rvm --default 1.9.2

Why begin/rescue/else behaves differently on 1.9.2 and 1.8.7

It's an open bug in Ruby. There is a discussion, though, whether it should behave like it behaved in 1.8 or as it does in 1.9.

Matz, the author of Ruby, believes that it should behave as in 1.8.

Not able to update ruby version from 1.8.7 to 1.9.2 using rvm

you have two installations of rvm, and one of them is broken, please use this answer: Installed Ruby 1.9.3 with RVM but command line doesn't show ruby -v

Make error installing Ruby 1.9.2 with RVM and Readline under OSX Lion

I had a very similar issue. I eventually found that adding this to my .bash_profile stopped my initial make errors:

export ARCHFLAGS="-arch x86_64"

Also from the command line run the following:

brew install readline
brew link readline
brew install libxml2
brew link libxml2

Then when you install ruby use this command:

rvm install 1.9.2 -C --with-readline-dir=/usr/local/Cellar/readline/6.2.1/ --with-libxml2-dir=/usr/local/Cellar/xml2/2.7.8

Hope that helps

Ruby 1.8.7 - Ruby 1.9.2 and Rails 3.0.0 'Encoding' Model Name Conflict

Encoding is a class that was introduced in ruby 1.9. Change your model name.

Should I use Ruby 1.9.2 with my new web app?

I have been successfully converting all my Rails projects (except one, but I'm working on it) from Ruby 1.8.7/Rails 2.3.5 to Rails 3.0.0 and Ruby 1.8.7/1.9.2 RC2 and both environments are pretty stable.

Fortunately, things changed since I posted this question.

Rails

Unless you really have something that prevents you to do that, I strongly encourage you to start with Rails 3.
The effort required to upgrade an application from Rails 2 to Rails 3 should discourage you from starting from Rails 2.

Talking about plugins and Gems, many developers are starting to convert their libraries to Rails 3. Currently there's a very high level of compatibility.
Furthermore, Rails 3 focused plugins tends to be quite more powerful to the Rails 2.3 ones, thanks to the new Rails plugin API. They can load tasks, they no longer abuse monkey patching or rely on internal hacks.

Also, Rails 3 is just around the corner. Unless your project will be deployed in 1 week, the stable version will probably available before you deploy your code.
I this would not happe, consider that I'm currently managing a couple of Rails 3 project in a production environment and they are pretty stable (Rails 3 RC1, the Beta 4 has a really weird bug in the caching environment).

Ruby 1.9.2

Ruby 1.9.2 is way more powerful than Ruby 1.8.7. If this is a brand new project, I suggest you to use the 1.9 branch.

Usually, it's more easy to start a new project in Ruby 1.9 than converting an existing one.

Ruby 1.9.2 is faster, even more faster than REE. The most part of the common Ruby 1.8.7 Gems work with Ruby 1.9 except a few ones, such as RCov.
Again, it's very hard you're going to need a library which doesn't work with Ruby 1.9.2.

If it happens, chances are this is an outdated library and a better replacement is probably available in the Ruby ecosystem.

If you can't find an alternative, remember that Rails 3 provides an excellent way to use custom libraries, thanks to Bundler.
You can fork the project and ask Bundler to use your fork. You can even integrate the library in your repos and ask Bundler to load the library from a path.

Conclusion

From my personal experience, I've been very happy with Ruby 1.9.2 and Rails 3.
This is by far my favorite environment and my default environment for new projects.

If you can't use Ruby 1.9.2 try with Ruby 1.8.7.
On the other side, I strongly encourage you to start with Rails 3.



Related Topics



Leave a reply



Submit