Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 316 Vote(s) - 3.34 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hidden features of Ruby

#1
Continuing the "Hidden features of ..." meme, let's share the lesser-known but useful features of Ruby programming language.

Try to limit this discussion with core Ruby, without any Ruby on Rails stuff.

See also:

* [Hidden features of C#](

[To see links please register here]

)
* [Hidden features of Java](

[To see links please register here]

)
* [Hidden features of JavaScript](

[To see links please register here]

)
* [Hidden features of Ruby on Rails](

[To see links please register here]

)
* [Hidden features of Python][1]

(Please, just _one_ hidden feature per answer.)

Thank you


[1]:

[To see links please register here]

Reply

#2
Download Ruby 1.9 source, and issue `make golf`, then you can do things like this:

make golf

./goruby -e 'h'
# => Hello, world!

./goruby -e 'p St'
# => StandardError

./goruby -e 'p 1.tf'
# => 1.0

./goruby19 -e 'p Fil.exp(".")'
"/home/manveru/pkgbuilds/ruby-svn/src/trunk"

Read the `golf_prelude.c` for more neat things hiding away.
Reply

#3
The **send()** method is a general-purpose method that can be used on any Class or Object in Ruby. If not overridden, send() accepts a string and calls the name of the method whose string it is passed. For example, if the user clicks the “Clr” button, the ‘press_clear’ string will be sent to the send() method and the ‘press_clear’ method will be called. The send() method allows for a fun and dynamic way to call functions in Ruby.

%w(7 8 9 / 4 5 6 * 1 2 3 - 0 Clr = +).each do |btn|
button btn, :width => 46, :height => 46 do
method = case btn
when /[0-9]/: 'press_'+btn
when 'Clr': 'press_clear'
when '=': 'press_equals'
when '+': 'press_add'
when '-': 'press_sub'
when '*': 'press_times'
when '/': 'press_div'
end

number.send(method)
number_field.replace strong(number)
end
end

I talk more about this feature in [Blogging Shoes: The Simple-Calc Application][1]


[1]:

[To see links please register here]

Reply

#4
I find using the **define_method** command to dynamically generate methods to be quite interesting and not as well known. For example:

((0..9).each do |n|
define_method "press_#{n}" do
@number = @number.to_i * 10 + n
end
end

The above code uses the 'define_method' command to dynamically create the methods "press1" through "press9." Rather then typing all 10 methods which essentailly contain the same code, the define method command is used to generate these methods on the fly as needed.
Reply

#5
Peter Cooper has a [good list][1] of Ruby tricks. Perhaps my favorite of his is allowing both single items and collections to be enumerated. (That is, treat a non-collection object as a collection containing just that object.) It looks like this:

[*items].each do |item|
# ...
end


[1]:

[To see links please register here]

Reply

#6
Fool some class or module telling it has required something that it really hasn't required:

$" << "something"

This is useful for example when requiring A that in turns requires B but we don't need B in our code (and A won't use it either through our code):

For example, Backgroundrb's `bdrb_test_helper requires` `'test/spec'`, but you don't use it at all, so in your code:

$" << "test/spec"
require File.join(File.dirname(__FILE__) + "/../bdrb_test_helper")

Reply

#7
use anything that responds to `===(obj)` for case comparisons:

case foo
when /baz/
do_something_with_the_string_matching_baz
when 12..15
do_something_with_the_integer_between_12_and_15
when lambda { |x| x % 5 == 0 }
# only works in Ruby 1.9 or if you alias Proc#call as Proc#===
do_something_with_the_integer_that_is_a_multiple_of_5
when Bar
do_something_with_the_instance_of_Bar
when some_object
do_something_with_the_thing_that_matches_some_object
end

`Module` (and thus `Class`), `Regexp`, `Date`, and many other classes define an instance method :===(other), and can all be used.

Thanks to [Farrel][1] for the reminder of `Proc#call` being aliased as `Proc#===` in Ruby 1.9.


[1]:

[To see links please register here]

Reply

#8
The Symbol#to_proc function that Rails provides is really cool.

Instead of

Employee.collect { |emp| emp.name }

You can write:

Employee.collect(&:name)
Reply

#9
Warning: this item was voted #1 **_Most Horrendous Hack of 2008_**, so use with care. Actually, avoid it like the plague, but it is most certainly Hidden Ruby.

## Superators Add New Operators to Ruby

Ever want a super-secret handshake operator for some unique operation in your code? Like playing code golf? Try operators like
-~+~-
or
<---
That last one is used in the examples for reversing the order of an item.

I have nothing to do with the [Superators Project](

[To see links please register here]

) beyond admiring it.


Reply

#10
Another fun addition in 1.9 Proc functionality is Proc#curry which allows you to turn a Proc accepting n arguments into one accepting n-1. Here it is combined with the Proc#=== tip I mentioned above:

it_is_day_of_week = lambda{ |day_of_week, date| date.wday == day_of_week }
it_is_saturday = it_is_day_of_week.curry[6]
it_is_sunday = it_is_day_of_week.curry[0]

case Time.now
when it_is_saturday
puts "Saturday!"
when it_is_sunday
puts "Sunday!"
else
puts "Not the weekend"
end
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through